ASP.NET: Directing the user to the login page, after logging in, send the user back to the page originally requested?

I am trying to manually implement a login system in ASP.NET 3.5. Basically, when loading, I would like the site to check and see if the user object is active, if not, than I want the login page to appear.

After successfully logging in, I would like the user to be able to access the very same page that they requested originally.

eg:

  • custom request: MyPage.aspx - not logged in
  • login page appears instead of MyPage.aspx
  • user logs in successfully
  • MyPage.aspx is displayed instead of Default.aspx like

Looking into the System.Net namespace, I see that there is a "HttpWebRequest" class that has a "HttpWebRequest.AllowAutoRedirect" property, but I'm not sure how this will get me back from the login page.

NOTE. I know ASP.NET has automatic configuration of authentication systems set up, but I would like to have manual control over the database.

- Tomek

+2


source to share


3 answers


What you could do if you don't want to use the built-in Autodesk Forms Authentication feature, follow these steps.

Check if the user is verified on every page you want to hide from anonymous users. If they are not authenticated then redirect them to your login page with the url in the query string.

if(!HttpContext.Current.User.Identity.IsAuthenticated) {
    Response.Redirect(~/login.aspx?redirect=this_page.aspx");
}

      

Then on the login page, after the user logs in. Check your query string to see if there is a redirect parameter.



if(!String.IsNullorEmpty(Request.QueryString["redirect"]) {
  string url = ResolveClientURL(redirect);
  Response.Redirect(url);
}

      

Of course, this is all built into .NET using Authentication, where you can deny anonymous access to certain directories, and when you do, .NET will redirect your login page (which is set in web.config) and include "ReturnURL = blahblah "on your login page.

Just FYI.

+2


source


Just keep the originally requested url in the session or hidden field on the login page

After successful login, use Server.Transfer or Response.Redirect to navigate to this page.



+1


source


It looks like a different method is described here . It seems that you can use the following object to return from the login page:

FormsAuthentication.RedirectFromLoginPage

      

However, according to the article, the best way is to use the described JackM, but with overload:

Response.Redirect("~/default.aspx", false);

      

By doing this, you prevent the session from ending when the page is redirected.

0


source







All Articles