• ASP.NET 2.0 成员管理


    Form认证
    web.config配置属性说明
     A。loginUrl:指定一个用于登录的页面。
     B。name:Cookie的名字,注意,如果一个服务器有很多应用的话(即IIS里运行多个网站时),要给cookie取不同的名字。
     C。TimeOut:Cookie的存活时间默认是30分钟。
     D。Protection:Cookie被保存的方式(防盗用和监听)。
     E。Path:Cookie的保存时间

    Protection属性
     A。None:不使用任何方法保护cookie
     B。Encryption:使用des或者三层des对cookie进行加密,但是并不对cookie传输中是否被监听或篡改进行监视。(本地cookie是加密的)
     C。Validation:监视cookie,保证传输过程中不会被监听或者篡改。但是并不对cookie进行加密。(cookie发给谁就只接收从谁发回的信息)
     D。All:同时使用Encryption和Validation(建议使用)

    使用文件记录用户的帐户和密码
     <authentication>
      <credentials passwordFormat="SHA1">
       <user name="Terry" password="84K38495EECC..."/>
       <user name="David" password="949F65C9374D..."/>
      </credentials>
     </authentication>
     保存方式
     A。Clear:不加密(明文保存)
     B。SHA1:使用SHA1进行加密
     C。MD5:使用MD5进行加密
     
    授权用户与角色
    用户访问还可以通过定制访问规则来实现对用户的角色分配:
     <authentication>
      <allow users="Terry"/>
      <allow roles="Admins"/>
      <deny users="*"/>
     </authentication>
     只有Terry这个用户可以访问,并且Terry必须具有管理员权限。
    其它配置方式:
     <allow VERB="POST" users="Terry,David"/>
     <deny VERB="POST" users="*"/>
     <allow VERB="GET" users="*"/>
    users属性:*表示所有用户,?表示匿名用户

    成员管理

    Membership中的信息保存在:App_Data下的ASPNETDB.MDF数据库(不建议手工对它进行任何修改)。

    成员管理特性基于membership、membershipuser两个类。可以使用membership类为asp.net创建用户。
    membership类还可以完成以下工作
     A。建立一个新的membershipuser(Membership.CreateUser)
     B。可以对用户身份进行验证(Membership.ValidateUser)
     C。找回一个membershipuser实例
     D。更新一个membershipuser实例
     E。通过不同条件寻找一个用户(Membership.GetUser)
     F。获得当前在线用户数量
     G。删除一个已经不再需要的帐户
     H。找回/修改一个用户的密码
     I。修改一个用户的密码问题以及密码问题的答案
     J。为一个已经因为多次尝试密码失败而锁定的用户解除锁定(UnlockUser)

    Membership Demo:
    web.config:
     <roleManager enabled="true" />  <!-- 在程序中对角色进行管理 -->
     <authentication mode="Forms"/>
     <location path="secured">  <!-- 用户登录后可访问secured文件夹里的页 -->
      <system.web>
       <authorization>
        <deny users="?"/>
        <allow users="*"/>
       </authorization>
      </system.web>
     </location>
     <location path="administrators_role">  <!-- 只有Administrators角色的用户才能访问 -->
      <system.web>
       <authorization>
        <allow roles="Administrators" />
        <deny users="*"/>
       </authorization>
      </system.web>
     </location>

    代码片段

    登录验证:
     if(Membership.ValidateUser(tbUserName.Text,tbPassword.Text))
     { FormsAuthentication.RedirectFromLoginPage(tbUserName.Text,cbRememberMe.check); }
    密码尝试次数:
     MembershipUser user = Membership.GetUser(tbUserName.Text);
     if(... user.Comment...>...) user.IsApproved = false;
    激活用户:
     MembershipUser user = Membership.GetUser(tbUserName.Text);
     user.IsApproved = true;
     Membership.UpdateUser(user);
    登出操作:
     FormsAuthentication.SignOut()
     Roles.DeleteCookie()
     FormsAuthentication.RedirectToLoginPage()
    绑定角色:
     Roles.GetAllRoles();
    创建角色:
     Roles.CreateRole(myRoleName);
    删除角色:
     Roles.DeleteRole(myRoleName);
    关联角色与用户:
     Roles.IsUserInRole(myRoleName); //判断当前用户是否属于myRoleName角色
     Roles.AddUserToRole(myUserName,myRoleName);
     Roles.RemoveUserFromRole(myUserName,myRoleName);

  • 相关阅读:
    DC(四)——笔试题
    验证&system verilog笔试题
    Centos和redhat6.0后关于虚拟机克隆后无法启用网卡问题
    搭建 CentOS 6 服务器(1)
    搭建 CentOS 6 服务器(16)
    搭建 CentOS 6 服务器(14)
    搭建 CentOS 6 服务器(15)
    搭建 CentOS 6 服务器
    搭建 CentOS 6 服务器(2)
    搭建 CentOS 6 服务器(3)
  • 原文地址:https://www.cnblogs.com/vipcjob/p/1557495.html
Copyright © 2020-2023  润新知