ASP. NET Role-Based Authentication

Source: Internet
Author: User
I. Problem description: There are two types of users: buyer and seller. There are two user tables in the database. Requirements: the root directory of the website contains two directories: DW directory and GR directory. All under the Directory

There is a login. aspx file, which is dwlogin. aspx and grlogin. aspx respectively. Do not use your own login. aspx file during logon. You cannot access the other party's directory after logon.

II. General idea: create a custom identity authentication ticket and put the roles information in the userdata of the ticket.

3. Set web. config
<Configuration>
<System. Web>
<Authentication mode = "forms">
<Forms name = ". aspxauth"
Loginurl = "login. aspx" // In the root directory, it is the sixth
Protection = "all"
Path = "/"/>
</Authentication>
</System. Web>
<Location Path = "DW">
<System. Web>
<Authorization>
<Allow roles = "DW"/>
<Deny users = "*"/> // this field must be used. *, use? Unable to meet requirements
</Authorization>
</System. Web>
</Location>
<Location Path = "GR">
<System. Web>
<Authorization>
<Allow roles = "GR"/>
<Deny users = "*"/>
</Authorization>
</System. Web>
</Location>
<Location Path = "/DW/dwlogin. aspx">
<System. Web>
<Authorization>
<Allow users = "*"/>
</Authorization>
</System. Web>
</Location>
<Location Path = "gr/grlogin. aspx">
<System. Web>
<Authorization>
<Allow users = "*"/>
</Authorization>
</System. Web>
</Location>
4. In dwlogin. aspx

Private void button#click (Object sender, system. eventargs E)
{
Httpcookie objcookie;
String strreturnurl;
If (isvalid)
{
Switch (getuser (username. Text, password. Text) // The getuser function obtains the username and password. The correct return value is 0, and the return value is 2 if the password is incorrect.

, Username error returns 1
{
Case 0:
Formsauthenticationticket ticket = new formsauthenticationticket (
1,
Username. Text,
Datetime. Now,
Datetime. Now. addminutes (30 ),
False,
"DW"); // I wrote it directly, and there may be better methods.
Objcookie = new httpcookie (= (". aspxauth ");
Objcookie. value = formsauthentication. Encrypt (ticket );
Response. Cookies. Add (objcookie );
Strreturnurl = request. Params ["returnurl"];
If (strreturnurl! = NULL)
{
Response. Redirect (strreturnurl); // return to the original accessed page
}
Else
{
Response. Redirect ("default. aspx"); // you can set the page you want to set here.
}
Break;
Case 1:
This. Page. registerstartupscript ("info", "<SCRIPT> alert ('this user does not exist !! '); </SCRIPT> ");
Break;
Case 2:
This. Page. registerstartupscript ("info", "<SCRIPT> alert ('wrong Password !! '); </SCRIPT> ");
Break;
}
}
}

The personal logon page grlogin. aspx is written in the same way as above.



5. Add the following to global. asax: Code :

Protected void application_authenticaterequest (Object sender, eventargs E)
{
If (httpcontext. Current. User! = NULL)
{
If (httpcontext. Current. User. Identity. isauthenticated)
{
If (httpcontext. Current. User. Identity is formsidentity)
{
Formsidentity id = (formsidentity) httpcontext. Current. User. identity;
Formsauthenticationticket ticket = ID. ticket;
String userdata = ticket. userdata;
String [] roles = userdata. Split (',');
Httpcontext. Current. User = new genericprincipal (ID, roles );
}
}
}
}

6. Create login. aspx in the root directory, and add the following code: (This login. aspx does not have a user name or password input box. It is mainly used to relocate the respective login pages)

Private void page_load (Object sender, system. eventargs E)
{
String paramname = "returnurl ";
String returnurl = request. querystring [paramname];
If (returnurl. tolower (). indexof ("DW")> = 0)
{
Response. Redirect ("DW/dwlogin. aspx? "+ Paramname +" = "+ returnurl );
}
Else if (returnurl. tolower (). indexof ("GR")> = 0)
{
Response. Redirect ("gr/grlogin. aspx? "+ Paramname +" = "+ returnurl );
}
Else
{
// Add some prompts here. You can also specify the page
}
}
Reprinted from: http://topic.csdn.net/t/20050613/17/4079365.html

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.