ASP. NET Forms authentication (custom, role provider) 2

Source: Internet
Author: User

2. Custom role providers

If you want to verify by role, it must involve the role provider. By default, it will connect to the default database, we can write a role provider to implement our own logic.
First, add the configuration in Web. config:

Code
<Rolemanager defaultprovider = "myroleprovider"
Enabled = "true"
Cacherolesincookie = "true"
Cookiename = ". asproles"
Cookietimeout = "30"
Cookiepath = "/"
Cookierequiressl = "false"
Cookieslidingexpiration = "true"
Cookieprotection = "all">
<Providers>
<Clear/>
<Add name = "myroleprovider"
Type = "myroleprovider"
Writeexceptionstoeventlog = "false"/>
</Providers>
</Rolemanager>

This is to specify the class myroleprovider provided by our role.

This class must be inherited from system. Web. Security. roleprovider. You only need to implement a method by reloading it (Other Methods return exceptions ):

 

Public override string [] getrolesforuser (string username)
{
Formsidentity id = httpcontext. Current. User. Identity as formsidentity;
If (ID! = NULL)
{
Return ID. Ticket. userdata. Split (New char [] {','});
}
Return NULL;
}

That is, the user role is obtained from the value previously saved to the cookie (formsauthentication automatically converts the saved cookie to the value in the user)

Then we can configure the role verification rules in Web. config:

<Location Path = "admin">
<System. Web>
<Authorization>
<Allow roles = "Admins"/>
<Deny users = "*"/>
</Authorization>
</System. Web>
</Location>

Or you can judge in the Code:

Bool a = user. isinrole ("testt ");

It is easy to judge.

Iv. Single Sign-on

The Single Sign-On using forms mainly uses the machinekey configuration and the machinekey element to configure the key so that it can be used to encrypt and decrypt the cookie data for Forms authentication and view status data,It is used to verify the non-process session status identity.

Currently, single-point logon in this way can only achieve synchronous logon between the same host or between sites in the same subdomain, such as www.cnblogs.com and firstyi.cnblogs.com, but www.cnblogs.com and www.sina.com.cn cannot, cross-Site logon is not allowed for domain names under different parent domain names

The main configuration is as follows:

<Machinekey validationkey = "encrypt" decryptionkey = "8b6697227cbca902b1a0925d40faa00b425f2df4359d2099" validation = "sha1"/>
<Authentication mode = "forms">
<Forms loginurl = "login. aspx" name = ". aspxauth1" Domain = ".cnblogs.com"/>
</Authentication>

The machinekey of multiple web sites for single-point logon must be the same, and the name and domain in forms must be the same.

After the configuration is completed, you do not need to log on again after logging on to one site and then transferring to another site.

Note:If the moss website adopts Forms authentication, you only need to change the corresponding configuration of the moss website to your own ASP.. Net site, you can directly access the moss site from your own site, and do not need to log on again (the moss site and its own site must have the same user name)

Others:

After Forms authentication, you can log out using the following methods:

Formsauthentication. signout ();

In addition, the background modules for these logins are configured inC:/Windows/Microsoft. NET/framework/v2.0.50727/config/Web. configFile:

<Add name = "windowsauthentication" type = "system. Web. Security. windowsauthenticationmodule"/>
<Add name = "formsauthentication" type = "system. Web. Security. formsauthenticationmodule"/>
<Add name = "passportauthentication" type = "system. Web. Security. passportauthenticationmodule"/>

Appendix:

The final web. config file
<? XML version = "1.0"?>
<Configuration xmlns = "http://schemas.microsoft.com/.NetConfiguration/v2.0">
<System. Web>
<Authorization>
<Deny users = "? "/>
</Authorization>
<Authentication mode = "forms">
<Forms loginurl = "login. aspx" name = ". aspxauth1" Domain = ".cnblogs.com"/>
</Authentication>
<Machinekey validationkey = "f9a61f796a204d9945889b64d9da5086e89cec5200f0ced4" decryptionkey = "encryption" validation = "sha1"/>
<Compilation DEBUG = "true"/>

<Rolemanager defaultprovider = "myroleprovider"
Enabled = "true"
Cacherolesincookie = "true"
Cookiename = ". asproles"
Cookietimeout = "30"
Cookiepath = "/"
Cookierequiressl = "false"
Cookieslidingexpiration = "true"
Cookieprotection = "all">
<Providers>
<Clear/>
<Add name = "myroleprovider"
Type = "myroleprovider"
Writeexceptionstoeventlog = "false"/>
</Providers>
</Rolemanager>

</System. Web>
<Location Path = "admin">
<System. Web>
<Authorization>
<Allow roles = "Admins"/>
<Deny users = "*"/>
</Authorization>
</System. Web>
</Location>
</Configuration>

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.