开发者

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 (i.e. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜