Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

I want to redirects the user to the page they were just visiting.
If the page they were just visiting does not allow anonymous users, then the
FormsAuthenticationModule will automatically redirect the user to the login
page.

I used this code but it always redirects user to Login page in all cases.

MIDL
FormsAuthentication.SignOut();

FormsAuthentication.RedirectToLoginPage();


Then, what can i do please ?
Posted
Comments
strogg 1-May-11 5:03am    
You're using FormsAuthentication.RedirectToLoginPage()
It always takes you to the login page. That is not what you want. Use Response.Redirect() to the required page instead
MrLonely_2 1-May-11 16:12pm    
I want to If the page that user was just visiting does not allow anonymous users, then it will automatically redirect the user to the login
page.

Like strogg said, use Response.Redirect("http://your.url.here/", true); instead. If the page is not accessible to anonymous users, the redirect to the login page will happen anyway.

on another note, isn't the following method name descriptive enough?

C#
FormsAuthentication.RedirectToLoginPage();
 
Share this answer
 
try this .
FormsAuthentication.RedirectFromLoginPage();
 
Share this answer
 
From what I understand from the question: you want the application to remember the authentication required page. If a user visits that page without authentication, the application redirects to the login page and after login, it redirects automatically to the area where the user is coming from. If this is the case, you can do the following.

Example:

1. MyReports.aspx requires authentication.
2. User visits MyReports.aspx without authentication

In the page load for MyReports.aspx, put the following code:

<br />
    if (!IsPostBack)<br />
    {<br />
        try<br />
        {<br />
           string tempUserName = Session["UserName"].ToString();<br />
        }<br />
        catch(Exception ex)<br />
        {<br />
           Session["LastVisitedURL"] = Request.Url.ToString();<br />
           Response.Redirect("LoginPage.aspx");<br />
        }<br />
<br />
    }<br />


On the LoginPage.aspx Authenticate Button Click Event put the following code

<br />
    Button Click Event<br />
    {<br />
        //Validate user<br />
        if Validated<br />
        {<br />
             try<br />
             {<br />
                 string URLRedirect = Session["LastVisitedURL"].ToString();<br />
                 Response.Redirect(URLRedirect);<br />
             } <br />
             catch(Exception ex)<br />
             {<br />
                 Response.Redirect("Dashboard or landing page after authentication");<br />
             }<br />
        }<br />
    }<br />


Sorry if this is not the case for you.

-Nayan
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900