If Response. Redirect ("default. aspx") is used directly, the authentication fails because you have not created an authentication ticket. The FormsAuthentication. RedirectFromLoginPage method automatically completes many functions. Such as generating authentication tickets, writing back to the client, browser redirection, and other actions. Of course, only the FormsAuthentication. RedirectFromLoginPage method can complete these functions. On the contrary, if you need to verify the role information, you can only use other methods.
You can manually add an authentication ticket
1.
Copy codeThe Code is as follows:
FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket (1, "coffee", DateTime. Now, DateTime. Now. AddMinutes (20), false, UserRoles ,"/");
NOTE: If UserRoles does not know, write ""
2. Encrypted serialization
Copy codeThe Code is as follows:
String HashTicket = FormsAuthentication. Encrypt (Ticket );
3. Generate cookie
Copy codeThe Code is as follows:
HttpCookie UserCookie = new HttpCookie (FormsAuthentication. FormsCookieName, HashTicket );
Cookie. Domain = ".jb51.net ";
4. the authentication ticket Cookie is output to the client.
Copy codeThe Code is as follows:
Response. Cookies. Add (UserCookie)
5. Redirection
Copy codeThe Code is as follows:
Response. Redirect (Context. Request ["ReturnUrl"]);