ASP.NET based on Forms-based verification mechanism

zhaozj2021-02-16  59

Recently, I'm watching ASP.NET Forum, and the verification mechanism can be used to see the model mode. After reading the form of the format authentication in the ASP.NET application, the idea is very clear, and I have done a little record in order to check out : Build the Forms-based verification mechanism as follows: 1. Set IIS to set to Form Verification in Anonymous Access and ASP.NET Web.config 2, retrieve data storage verification users, and retrieve roles (if not based on roles) 3 using FormsAuthenticationTicket create a Cookie and sent back to the client, and the role of the votes stored, such as: FormsAuthentication.SetAuthCookie (Username, true | false) cookies to save time: HttpContext.Current.Response.Cookies [FormsAuthentication.FormsCookieName] .Expires = DATETIME.NOW.ADDDAYS (1)

If you need to store character, using: FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket (1, // version txtUserName.Text, // user name DateTime.Now, // creation DateTime.Now.AddMinutes (20), // Expiration false, // Persistent Roles); // User Data Roles is a role string array string encryptedticket = formsauthentication.encrypt (authticket); // encryption

Deposit cookie httpcookie authcookie = new httpcookie (Formsauthentication.formscookiename, EncryptedTicket);

Response.cookies.add (authcookie);

4, the processing procedure (the Global.asax), a ticket to create objects in the IPrincipal Application_AuthenticateRequest event code and present HttpContext.User: HttpCookie authCookie = Context.Request.Cookies [FormsAuthentication.FormsCookieName]; FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt ( AuthCookie.Value); // Decryption String [] Roles = Authticket.userData.split (new char [] {';'}); // Decompose according to the format in the deposit,; or | .... context.user = New genericprincipal (context.user.Identity, Roles); // There is httpContext.user

转载请注明原文地址:https://www.9cbs.com/read-19015.html

New Post(0)