天天看点

ASP.NET Forms验证(自定义、角色提供程序)2

2、自定义角色提供程序

如果要按照角色进行验证的话,肯定要涉及到角色提供程序,在默认情况下也是会去连接默认的数据库的,我们可以自己写一个角色提供程序来实现自己的逻辑。

首先在web.config中加入配置:

ASP.NET Forms验证(自定义、角色提供程序)2
ASP.NET Forms验证(自定义、角色提供程序)2

Code

ASP.NET Forms验证(自定义、角色提供程序)2

    <roleManager defaultProvider="MyRoleProvider"

ASP.NET Forms验证(自定义、角色提供程序)2

       enabled="true"

ASP.NET Forms验证(自定义、角色提供程序)2

       cacheRolesInCookie="true"

ASP.NET Forms验证(自定义、角色提供程序)2

       cookieName=".ASPROLES"

ASP.NET Forms验证(自定义、角色提供程序)2

       cookieTimeout="30"

ASP.NET Forms验证(自定义、角色提供程序)2

       cookiePath="/"

ASP.NET Forms验证(自定义、角色提供程序)2

       cookieRequireSSL="false"

ASP.NET Forms验证(自定义、角色提供程序)2

       cookieSlidingExpiration="true"

ASP.NET Forms验证(自定义、角色提供程序)2

       cookieProtection="All" >

ASP.NET Forms验证(自定义、角色提供程序)2

      <providers>

ASP.NET Forms验证(自定义、角色提供程序)2

        <clear />

ASP.NET Forms验证(自定义、角色提供程序)2

        <add name="MyRoleProvider"

ASP.NET Forms验证(自定义、角色提供程序)2

           type="MyRoleProvider"

ASP.NET Forms验证(自定义、角色提供程序)2

           writeExceptionsToEventLog="false" />

ASP.NET Forms验证(自定义、角色提供程序)2

      </providers>

ASP.NET Forms验证(自定义、角色提供程序)2

    </roleManager>

这个就是指定我们的角色提供类MyRoleProvider。

这个类必须从System.Web.Security.RoleProvider继承,只要重载实现一个方法就可以了(其他方法返回异常):

ASP.NET Forms验证(自定义、角色提供程序)2

     public override string [] GetRolesForUser( string username)

ASP.NET Forms验证(自定义、角色提供程序)2
ASP.NET Forms验证(自定义、角色提供程序)2
ASP.NET Forms验证(自定义、角色提供程序)2

{

ASP.NET Forms验证(自定义、角色提供程序)2

         FormsIdentity Id = HttpContext.Current.User.Identity as FormsIdentity;

ASP.NET Forms验证(自定义、角色提供程序)2

        if (Id != null)

ASP.NET Forms验证(自定义、角色提供程序)2
ASP.NET Forms验证(自定义、角色提供程序)2
ASP.NET Forms验证(自定义、角色提供程序)2

{

ASP.NET Forms验证(自定义、角色提供程序)2
ASP.NET Forms验证(自定义、角色提供程序)2

            return Id.Ticket.UserData.Split(new Char[]

ASP.NET Forms验证(自定义、角色提供程序)2

{ ',' });

ASP.NET Forms验证(自定义、角色提供程序)2

         }

ASP.NET Forms验证(自定义、角色提供程序)2

        return null;

ASP.NET Forms验证(自定义、角色提供程序)2

     }

也就是从我们之前保存到Cookie中的值取得用户角色(FormsAuthentication会自动把保存的cookie转化成User内的值)

之后我们就可以在web.config中配置角色验证规则了:

ASP.NET Forms验证(自定义、角色提供程序)2

   < location path ="admin" >

ASP.NET Forms验证(自定义、角色提供程序)2

     < system.web >

ASP.NET Forms验证(自定义、角色提供程序)2

       < authorization >

ASP.NET Forms验证(自定义、角色提供程序)2

         < allow roles ="Admins" />

ASP.NET Forms验证(自定义、角色提供程序)2

         < deny users ="*" />

ASP.NET Forms验证(自定义、角色提供程序)2

       </ authorization >

ASP.NET Forms验证(自定义、角色提供程序)2

     </ system.web >

ASP.NET Forms验证(自定义、角色提供程序)2

   </ location >

或者也可以在代码中判断:

ASP.NET Forms验证(自定义、角色提供程序)2

bool a = User.IsInRole( " testt " );

判断起来还是很方便的。

四、单点登录

使用Forms的单点登录主要是通过machineKey的配置,machineKey 元素对密钥进行配置,以便将其用于对 Forms 身份验证 Cookie 数据和视图状态数据进行加密和解密,并将其用于对进程外会话状态标识进行验证

使用这种方式的单点登录目前只能实现相同主机或相同子域站点之间的同步登录,比如www.cnblogs.com和firstyi.cnblogs.com可以实现,但是www.cnblogs.com和www.sina.com.cn就不能实现了,对于非同一父域名下的域名间不能跨站登录

主要配置如下:

ASP.NET Forms验证(自定义、角色提供程序)2

   < machineKey validationKey ="282487E295028E59B8F411ACB689CCD6F39DDD21E6055A3EE480424315994760ADF21B580D8587DB675FA02F79167413044E25309CCCDB647174D5B3D0DD9141" decryptionKey ="8B6697227CBCA902B1A0925D40FAA00B353F2DF4359D2099" validation ="SHA1" />

ASP.NET Forms验证(自定义、角色提供程序)2

   < authentication mode ="Forms" >

ASP.NET Forms验证(自定义、角色提供程序)2

       < forms loginUrl ="login.aspx" name =".ASPXAUTH1" domain =".cnblogs.com" />

ASP.NET Forms验证(自定义、角色提供程序)2

   </ authentication >

要实现单点登录的多个web站点的machineKey必须一样,forms里面的name和domain也必须一样

这样配置好之后,在其中一个站点登录后再调转到另一个站点就不需要再次登录了。

注:如果MOSS网站采用Forms验证方式的话,只要把MOSS站点的对应配置改成和自己的Asp.Net站点一致,那么可以从自己的站点直接进入MOSS站点,也不需要重新登录(MOSS站点和自己的站点要有相同的用户名)

其他:

Forms验证之后可以使用以下方法退出登录:

ASP.NET Forms验证(自定义、角色提供程序)2

FormsAuthentication.SignOut();

另外这些登录的后台Module是配置在C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/CONFIG/web.config文件中的:

ASP.NET Forms验证(自定义、角色提供程序)2

       < add name ="WindowsAuthentication" type ="System.Web.Security.WindowsAuthenticationModule" />

ASP.NET Forms验证(自定义、角色提供程序)2

       < add name ="FormsAuthentication" type ="System.Web.Security.FormsAuthenticationModule" />

ASP.NET Forms验证(自定义、角色提供程序)2

       < add name ="PassportAuthentication" type ="System.Web.Security.PassportAuthenticationModule" />

ASP.NET Forms验证(自定义、角色提供程序)2

附:

ASP.NET Forms验证(自定义、角色提供程序)2
ASP.NET Forms验证(自定义、角色提供程序)2

最后的web.config文件

ASP.NET Forms验证(自定义、角色提供程序)2

<?xml version="1.0"?>

ASP.NET Forms验证(自定义、角色提供程序)2

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">

ASP.NET Forms验证(自定义、角色提供程序)2

  <system.web>

ASP.NET Forms验证(自定义、角色提供程序)2

        <authorization>

ASP.NET Forms验证(自定义、角色提供程序)2

            <deny users="?"/>

ASP.NET Forms验证(自定义、角色提供程序)2

        </authorization>

ASP.NET Forms验证(自定义、角色提供程序)2

        <authentication mode="Forms">

ASP.NET Forms验证(自定义、角色提供程序)2

      <forms loginUrl="login.aspx" name=".ASPXAUTH1" domain=".cnblogs.com" />

ASP.NET Forms验证(自定义、角色提供程序)2

    </authentication>

ASP.NET Forms验证(自定义、角色提供程序)2

    <machineKey validationKey="F9A61F796A204D9945889B64D9DA5086E89CEC5200F0CED4" decryptionKey="D679BCF2A76DEBB04D7FED5E5967F46C92FEF2B31AD5D7C9" validation="SHA1" />

ASP.NET Forms验证(自定义、角色提供程序)2

    <compilation debug="true"/>

ASP.NET Forms验证(自定义、角色提供程序)2
ASP.NET Forms验证(自定义、角色提供程序)2

    <roleManager defaultProvider="MyRoleProvider"

ASP.NET Forms验证(自定义、角色提供程序)2

       enabled="true"

ASP.NET Forms验证(自定义、角色提供程序)2

       cacheRolesInCookie="true"

ASP.NET Forms验证(自定义、角色提供程序)2

       cookieName=".ASPROLES"

ASP.NET Forms验证(自定义、角色提供程序)2

       cookieTimeout="30"

ASP.NET Forms验证(自定义、角色提供程序)2

       cookiePath="/"

ASP.NET Forms验证(自定义、角色提供程序)2

       cookieRequireSSL="false"

ASP.NET Forms验证(自定义、角色提供程序)2

       cookieSlidingExpiration="true"

ASP.NET Forms验证(自定义、角色提供程序)2

       cookieProtection="All" >

ASP.NET Forms验证(自定义、角色提供程序)2

      <providers>

ASP.NET Forms验证(自定义、角色提供程序)2

        <clear />

ASP.NET Forms验证(自定义、角色提供程序)2

        <add name="MyRoleProvider"

ASP.NET Forms验证(自定义、角色提供程序)2

           type="MyRoleProvider"

ASP.NET Forms验证(自定义、角色提供程序)2

           writeExceptionsToEventLog="false" />

ASP.NET Forms验证(自定义、角色提供程序)2

      </providers>

ASP.NET Forms验证(自定义、角色提供程序)2

    </roleManager>

ASP.NET Forms验证(自定义、角色提供程序)2
ASP.NET Forms验证(自定义、角色提供程序)2

  </system.web>

ASP.NET Forms验证(自定义、角色提供程序)2

  <location path="admin">

ASP.NET Forms验证(自定义、角色提供程序)2

    <system.web>

ASP.NET Forms验证(自定义、角色提供程序)2

      <authorization>

ASP.NET Forms验证(自定义、角色提供程序)2

        <allow roles="Admins"/>

ASP.NET Forms验证(自定义、角色提供程序)2

        <deny users="*"/>

ASP.NET Forms验证(自定义、角色提供程序)2

      </authorization>

ASP.NET Forms验证(自定义、角色提供程序)2

    </system.web>

ASP.NET Forms验证(自定义、角色提供程序)2

  </location>

ASP.NET Forms验证(自定义、角色提供程序)2

</configuration>

ASP.NET Forms验证(自定义、角色提供程序)2