However, during my use, I found that controlling the role was not so easy. By searching for information online, I finally solved this problem. The main precautions are listed below.
1. In the configuration file, the allow item of the role should be placed in front of the deny item, and users should be configured as *, not?
Code
<location path="Doctors">
<>
<authorization>
<allow roles="doctors"//This one is in front
<deny users="*"/>
</authorization>
</>
</location>
2. Write roles to tickets
Code
string role="doctors";
FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket(1, username, , (30), false, role, "/");//Create an authentication ticket object
string HashTicket = (Ticket);// Encryption serialization verification ticket is a string
HttpCookie UserCookie = new HttpCookie(, HashTicket);
//Generate cookies
(UserCookie);//Output Cookie
("");//Redirect to the initial page of the user's application
3. Identity tickets do not directly provide direct support for roles, and roles need to be parsed in Application_AuthenticateRequest
Code
string[] roles = (new char[] { '|' });
FormsIdentity id = new FormsIdentity(authTicket);
principal = new (id, roles);
= principal;
Just figure out these three points roughly.
Code Packaging
1. In the configuration file, the allow item of the role should be placed in front of the deny item, and users should be configured as *, not?
Code
Copy the codeThe code is as follows:
<location path="Doctors">
<>
<authorization>
<allow roles="doctors"//This one is in front
<deny users="*"/>
</authorization>
</>
</location>
2. Write roles to tickets
Code
Copy the codeThe code is as follows:
string role="doctors";
FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket(1, username, , (30), false, role, "/");//Create an authentication ticket object
string HashTicket = (Ticket);// Encryption serialization verification ticket is a string
HttpCookie UserCookie = new HttpCookie(, HashTicket);
//Generate cookies
(UserCookie);//Output Cookie
("");//Redirect to the initial page of the user's application
3. Identity tickets do not directly provide direct support for roles, and roles need to be parsed in Application_AuthenticateRequest
Code
Copy the codeThe code is as follows:
string[] roles = (new char[] { '|' });
FormsIdentity id = new FormsIdentity(authTicket);
principal = new (id, roles);
= principal;
Just figure out these three points roughly.
Code Packaging