Asp.Net2.0学习笔记(五):角色控制与管理

来源:互联网 发布:北京联合大学 知乎 编辑:程序博客网 时间:2024/04/29 02:26

权限管理和身份验证

1、认证与授权机制
查询acls列表或者许可证来判定该用户是否有访问权限
通过Url认证

2、有四种认证方式:
None:不进行身份认证
Windows:基于Windows身份验证
Form:基于Cookie身份认证
Passport:使用Passport SDK进行二次开发

3、认证方式
<configuration>
 <system.web>
  <authentication mode="Forms" >
 </system.web>
</configuration>

4、Form认证方式
<configuration>
 <system.web>
  <authentication mode="Forms" />
   <forms name="crm" loginUrl="Login.aspx" protection="All" timeout="30" path="/">
   </forms>
   <deny users="?"/>
  </authentication>
 </system.web>
</configuration>
其中loginUrl:登陆页面
name:cookie的名字
timeout:存活的时间
protection:保存的方式,None:不进行保存;Encryption:进行des加密,不判断是否被监听;Validation:监视都不加密:
All:同时使用Encryption和Validation。
Path:cookie保存的路径

5、可以访问的用户名和密码
<authentication >
 <credentials passwordFormat="SHA1">
  <user name="Mary" password="..." />
  <user name="John" password="..." />
 </credentials>
</authentication>
其中passwordFormat:Clear:不加密;SHA1:使用SHA1进行加密;MD5:用MD5进行加密。

6、授权用户和角色
<authentication >
 <allow users="someone@www.frontfree.com" />
 <allow roles="Admins" />
 <deny users="*"/>
</authentication>

<allow VERB="POST" users="John,Mary" />
<allow VERB="POST" users="*" />
<allow VERB="GET" users="*" />

其中*:所有用户;?:匿名用户

7、控件和使用的类
LoginStatus、Login、CreateUserMizard

membership membershipUser
建立一个新的的membershipUser类
可以对用户身份进行验证
找回一个membershipUser实例
更新一个membershipUser实例
通过不同条件寻找一个用户
获得当前在线用户数量
删除一个不再需要的帐户

访问一个membership示例的属性
找回一个用户的密码
修改一个用户的密码
修改一个用户的密码问题以及密码问题的答案
为一个已经因为多次尝试密码失败而锁定的用户解除锁定

web.config,指定什么角色和用户指定所能访问的path。
<roleManager enable="true" />

<location path="secured">
 <system.web>
  <authorization>
   <deny users="?"/>
   <allow users="*" />
  </authorization>
 </system.web>
</location>

<location path="administrator_role">
 <system.web>
  <authorization>
   <allow roles="Administrator" />
   <deny users="?"/>
  </authorization>
 </system.web>
</location>

membership、membershipUser、roles、user类


CreateUser,中间有个返回值:创建User类
validateUser:判断是否合理
GetUser:返回membershipUser,可以获得属性信息。
UnlockUser等等

roles
createrole
deleterole
AddUserToRole
RemoveUserFromRole
(CType(User,RolePrincipal)).GetRoles()
GetAllRoles()
User.IsInRole
Roles.IsUserInRole