简体   繁体   中英

Redirect user back to the original action after login

The sequence of actions that I am trying to accomplish is below.

Context: user can add products to its own account.

  1. User tries to add a specific product. (He/she is not login at this point.)
  2. In the code behind, I need to redirect the user to login page before I can add the product to user's account.
  3. After login, how do I take the system back to the logic to finish up the action in step 1.

Thanks.

When you redirect to the login page, add the original URL in the querystring.
After a successful login, send a to the URL from the querystring.

Assuming you are using FormsAuthentication, if you are not, or don't know what to use, check it out here: http://msdn.microsoft.com/en-us/library/xdt4thhy.aspx

OK, so when your user tries to access a page that is protected (ie they have to be logged it), ASP.NET's forms authentication will send them to the login page, which is best set up in the web config:

<authentication mode="Forms">
   <forms loginUrl="~/Public/Login.aspx" defaultUrl="~/Public/Default.aspx"/>
</authentication>

It will also attach a ReturnUrl query string parameter to the that, so the login page knows where they came from: http://www.example.com/Public/Login.aspx ?ReturnUrl=%2fprivate%2forderproduct.aspx

Then, in your login page, assuming that they have successfully authenticated, you set the authentication cookie and redirect them back from whence they came:

FormsAuthentication.SetAuthCookie(someUserIdentifier,isRememberMeSet);
FormsAuthentication.RedirectFromLoginPage(someUserIdentifier, isRememberMeSet);

Of course, if you don't want to use FormsAuthentication, you can still use the concept of the return URL. Forms authentication just gives you a lot of that for free.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM