In traditional ASP. NET WebForm, if form authentication is used. To redirect to the originally requested page after login, simply use
FormsAuthentication.RedirectFromLoginPage
But in MVC, MVC does not eat this set. Try a lot of methods can not. But eventually it was done. "FormsAuthentication.RedirectFromLoginPage" is written in the title. Because I started looking for the keyword "formsauthentication.redirectfromloginpage" in Google to find out how to apply it to MVC. Or there is no alternative to MVC. Did not succeed but was inspired by some.
The login page is redirected to the login page because there are no logged-on users if they are not directly accessed. And the URL is followed by the "ReturnUrl" parameter record from which page to jump over.
Then we can do this.
1. Receive the "RETURNURL" parameter in the "Action" method of the login.
2. After the login succeeds in verifying the "Action" method of the login, it is determined that if "ReturnUrl" is not empty, it jumps to the page that "RETURNURL" points to.
Code:
1. Entities:
The "RETURNURL" parameter is added to the login entity to receive the address of the page before the login
Public classLogininfo { Public stringLoginName {Get;Set; } Public stringloginpwd {Get;Set; } Public stringSecuritycode {Get;Set; } Public string RETURNURL{Get;Set; } }
2. Action on the login page
[allowanonymous] public actionresult Login (string returnUrl ) { = returnUrl; return View (); }
3. Login Page view
Auxiliary method "@Html. Hidden (" ReturnUrl ")" will automatically generate "<input id=" ReturnUrl "name=" ReturnUrl "type=" Hidden ">" Html Code, and bind " Viewbag.returnurl "property.
<formAction= "/user/login"Method= "POST"> <inputtype= "text"name= "LoginName" /> <inputtype= "Password"name= "Loginpwd" /> <inputtype= "text"name= "Securitycode" /> <imgID= "Veri_code"src= "@Url. Action ("Authenticationcode ", new {R= newRandom (). Nextdouble ()}) "Width= " +"Height= "All"class= "Identify_code"alt= "Verification Code" /> <ahref= "javascript:void (0);"onclick= "Refreshcode ()">Refresh</a> <inputtype= "Submit"value= "Login" />@Html. Hidden ("ReturnUrl")</form>
4. Action to perform login verification
[HttpPost] [allowanonymous] Public voidLogin (logininfo login) {if(Login. Securitycode! =Sessionutil.authenticationcode)Throw NewBusinessexception ("Verification Code Error"); varuser =UserService.Instance.Login (Login. LoginName, Login. LOGINPWD); Sessionutil.current=user; FormsAuthenticationTicket ticket=NewFormsAuthenticationTicket (1, user. Id.tostring (), DateTime.Now, DateTime.Now.AddYears (1), true,string. Empty, Formsauthentication.formscookiepath); stringTicstring =Formsauthentication.encrypt (ticket); HttpCookie COO=NewHttpCookie (Formsauthentication.formscookiename, ticstring); if(ticket. ispersistent) {coo. Expires=ticket. expiration; } Response.Cookies.Add (COO); If (string. Isnullorwhitespace (login. RETURNURL)) Response.Redirect ("/publicaccountwater/index"); Else Response.Redirect (login. RETURNURL); }
After MVC login redirects back to the originally requested URL FormsAuthentication.RedirectFromLoginPage