[英]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. 上下文:用户可以将产品添加到自己的帐户。
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.