简体   繁体   English

登录后将用户重定向回原始操作

[英]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. 登录后,如何使系统返回逻辑以完成步骤1中的操作。

Thanks. 谢谢。

When you redirect to the login page, add the original URL in the querystring. 重定向到登录页面时,在查询字符串中添加原始URL。
After a successful login, send a to the URL from the querystring. 成功登录后,从查询字符串发送到URL。

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 假设您正在使用FormsAuthentication,如果您不使用或不知道要使用什么,请在此处查看: 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: 好的,所以当你的用户试图访问受保护的页面时(即必须将其记录下来),ASP.NET的表单身份验证会将它们发送到登录页面,最好在Web配置中设置:

<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 它还会将ReturnUrl查询字符串参数附加到该参数,因此登录页面知道它们来自何处: 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: 然后,在您的登录页面中,假设它们已成功通过身份验证,您可以设置身份验证cookie并将它们从它们的来源重定向:

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. 当然,如果您不想使用FormsAuthentication,您仍然可以使用返回URL的概念。 Forms authentication just gives you a lot of that for free. 表单身份验证只是免费提供了很多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 登录后如何让用户回到原来的页面? - How to return user back to the original page after login? 重定向到登录后重定向回位置 - Redirect back to location after being redirected to login 在asp.net中成功登录后,将用户重定向到源页面 - Redirect the user back to the source page from where it has come after succesfull login in asp.net 如何在asp.net mvc 3登录后将用户重定向回到他所在的位置 - How to redirect a user back to where he was after login in asp.net mvc 3 成功登录后无法重定向到原始页面 - Can't redirect to original page after successful login 拒绝用户登录后返回 - Resist User to go back after Login 登录后将用户重定向到特定页面 - Redirect user to a specific page after login 注销后,如何在ASP.NET MVC3中单击浏览器的后退按钮时将用户重定向到登录页面 - After logout,how to redirect user to login page when he/she will click back button of browser in ASP.NET MVC3 验证用户身份后将用户重定向回上一页 - redirect user back to previous page after authenticating user 如何使用后退按钮成功登录后阻止用户返回登录页面 - How to prevent user from going back to the login-page after successful login using back button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM