最近整理了下笔记:发现有个ASP.NET用户权限设置笔记,就翻出来分享下~~~
      利用web.config文件对整个程序集进行配置

单个页面授权设置代码如下:
<!-- 只对Default.aspx这个页面授权访问-->
<location path="Default.aspx">     <!--location指定了配置权限对应的页面-->
  <system.web>
    <authorization>
       <allow users="*"/>       <!--允许所有的人访问-->
     </authorization>
   </system.web>
<location>

         在Web程序里,每个根目录都有一个web.config。但一般的程序只有一个web.config配置文件。有多个文件夹里,子目录的web.config继承了母目录中的web.config。如果子母有冲突的话,则以子目录的web.config的规则为准。

        一个网站不允许两种访问方式。

        Context.User.Identity.Name;   //用于获取当前用户名

        Form的验证方式不理会Windows用户,全取决于你是否登录,写入Cookie。
                using System.Web.Security;
                FormAuthentication.RedirectFormLoginPage(string userName,bool createPersistentCookie)
          //此方法将经过身份验证的用户重定向回最初请求的URL或默认的URL。             
          createPersistentCookie为true时则会创建一个Cookie。在Windows系统里C:\Documents and Settings\Administrator\Cookies路径可以找到。
Forms验证配置说明
<authentication mode="Forms">
<forms  name=".ASPXAUTH" [string]     用于身份验证的Cookie的名称。如果需要为多个应用程序提供验证,则必须在每个应用程序中配置Cookie名称。
   loginUrl="login.aspx" [string]  指向登录页面。
   defaultUrl="default.aspx" [string]  指定用户通过验证之后默认重定向的URL地址。
   protection="All" [All | None |Encryption |Validation]  针对Cookie信息进行加密设置。
   timeout="30" [in Minutes][number]  指定Cookie的有效期,以分钟为单位。
   path="/" [string]    为由应用程序发出的Cookie指定路径。
   requireSSL="false" [true | false]  指定是否使用安全套阶层(SSL)连接来传输身份验证Cookie。
   slidingExpiration="true" [true |false]  指定是否启用滑动到期策略。
   enableCrossAppRedirects="false" [true | false]  指定是否需要将通过身份验证的用户重定向到其他Web应用程序中URL。
   Cookieless="UseDeviceProfile" [UseUri:指示不使用Cookie; |UseCookies:指示一定使用Cookie; |AutoDetect:指示根据浏览器具体情况使用Cookie或不使用;                                |UseDeviceProfile:与AutoDetect功能类似,但不会对浏览器进行检测其是否启用了Cookie。]  指定是否使用Cookie以及使用的方式。
   domain=" " [string]
>
  <credentials passwordFormat="SHA1" [Clear | SHA1 |MD5]>
    <user
        name="" [string,Required,Collection Key]
        password="" [string,Required]
    />
  </credentials>
</forms>
</authentication>
 

FormsAuthenticationTicket
FormsAuthentication.Encrypt()
写入Cookie时,一定要写入时限。
如:
  Cookie cook1=new Cookie();
  cook1.Expires=DateTime.Now.AddDays(1);

网站有不同角色的用户,前台的用户不能访问后台,管理员哪个页面都可以访问。

在子文件Web.config里面如果有<deny>和<allow>同时存在时,<allow>必须在前面,否则deny会冲掉allow。