I wrote an example program. The first use of FROMs-based verification is used. First, assume that the user will succeed (this is generally verified from the database). Then write the authentication ticket Authentication. Deconsive whether the user is verified in the later page, if not, redirect to the user login page. If you have already logged in, you will perform business logic. This article focuses on discussing the use of Authentication in role verification. Not paying attention to other respects. The steps are as follows: 1. Add the following code in the user login button: This code is mainly written after the user is logged in, writes cookie.private void button1_click (object sender, system.eventargs e) {string username = TextBox1.Text.Trim (); String roles = "admin";
/ / Generate a verification ticket object. FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket (1, username, DateTime.Now, DateTime.Now.AddMinutes (20), false, roles); // encrypted authentication ticket string encrytedTicket = FormsAuthentication.Encrypt (authTicket); // generate Cookie object. //FormSauthentication.FormScookiename acquires the value of Name in the
IF (null == authcookie) {return;} formsauthenticationticket autocket = null; // Get verification ticket. Authticket = formsauthentication.Decrypt (authcookie.value);
IF (null == authticket) {return;}
// Verify that the user's role information is stored in the userData of the ticket. // USERDATA This is originally stored user custom information. This is used to store user roles. String [] roles = authticket.userdata.split (new char [] {','});
Formsidentity id = new formsident; genericprincipal principal = new genericprincipal (id, roles);
// Assign the generated verification ticket information and the role information to the current user. CONTEXT.USER = Principal; Third. The user ID is judged by httpContext.current.user.Identity.Name in each page, and httpContext.current.user.isinrole ("admin") determines whether the user belongs to a role (or a group) four. WebConfig modification: