asp.net implementation of directory role permissions based on forms validation _ practical skills

Source: Internet
Author: User
But in the course of my use, I found that the control of the role is not so easy, through the Internet to find information, and finally solve the problem. The main considerations are listed below.
1, in the configuration file, the role of the allow item to be placed in the front of the deny entry, the users to configure to *, not?

Code
Copy Code code as follows:

<location path= "Doctors" >
<system.web>
<authorization>
<allow roles= "doctors"/>//This in the former
<deny users= "*"/>
</authorization>
</system.web>
</location>

2. Write the role to the bill

Code
Copy Code code as follows:

String role= "doctors";
FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket (1, username, DateTime.Now, DateTime.Now.AddMinutes ( , false, role, "/");//Establish an authentication ticket object
String hashticket = Formsauthentication.encrypt (Ticket);//cryptographic Serialization verification ticket
HttpCookie Usercookie = new HttpCookie (Formsauthentication.formscookiename, Hashticket);
Generate cookies
RESPONSE.COOKIES.ADD (Usercookie);//Output cookie
Response.Redirect ("")//Redirect to initial page of user request

3, identity paper does not directly provide the role of direct support, need to be in the application_authenticaterequest to analyze

Code
Copy Code code as follows:

string[] roles = AuthTicket.UserData.Split (new char[] {' | '});
FormsIdentity id = new FormsIdentity (AuthTicket);
System.Security.Principal.GenericPrincipal Principal = new System.Security.Principal.GenericPrincipal (ID, roles);
Context.User = Principal;

Make a general understanding of these three points.
Code Packaging

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.