晴朗笔记

努力做好自已,其他交给时间

ASP.NET 登录验证模式

登录验证模式:

  1.配置

        <authentication mode="Forms">
            <forms loginUrl="~\Membership\authing\Login.aspx" timeout="30000">
            </forms>
        </authentication>

   在此处配置了loginUrl后,不需要在代码中手工判断是否登录,如未登录导至登录页面。

        <authorization>
            <deny users="?"/> <!--拒绝未认证用户,如不加此节点默认为允许所有用户访问-->
        </authorization>

   要想上面的那个FROMS 认证起用,还需配置上面这个节点,用以拒绝未认证用户的访问。

   2.如何实现登录与退出

   authentication 节点对应System.Security.FormsAuthentication类。

   在转到Login页后,验证完用户,可使用System.Security.FormsAuthentication类进行登录成功。

   例:FormsAuthentication.RedirectFromLoginPage("UserName", true);

   再使用Context.User.Identity.Name 来取得当前登录用户名

   登录成功后可使用FormsAuthentication.SignOut(); 来退出登录,但需注意,此处需要手工重导到登录页,或者再触发一次页面。

   3.跨应用实现Forms认证

   启用了跨多个 ASP.NET 应用程序的 Forms 身份验证,则当用户在应用程序之间切换时,不需要对他们重新进行身份验证。实现单点登录功能。

   要配置跨应用程序的 Forms 身份验证,请在forms和machineKey配置节中设置若干属性,以便值对于参与共享 Forms 身份验证的所有应用程序都是相同的。

下面的示例演示了 Web.config 文件的authentication 节。除非另行说明,否则 name、protection、path、validationKey、validation、decryptionKey 和 decryption 属性必须在所有应用程序中都完全相同。同样,用于 Cookie 数据的加密和验证密钥以及加密方案和验证方案也必须完全相同。如果设置不匹配,则不能共享 Cookie。

<configuration>
  <system.web>
    <authentication mode="Forms" >
      <!-- The name, protection, and path attributes must match
           exactly in each Web.config file. -->
      <forms loginUrl="login.aspx"
        name=".ASPXFORMSAUTH"
        protection="All" 
        path="/"
        timeout="30" />
    </authentication>

    <!-- Validation and decryption keys must exactly match and cannot
         be set to "AutoGenerate". The validation and decryption
         algorithms must also be the same. -->
    <machineKey
      validationKey="C50B3C89CB21F4F1422FF158A5B42D0E8DB8CB5CDA1742572A487D9401E3400267682B202B746511891C1BAF47F8D25C07F6C39A104696DB51F17C529AD3CABE"
      decryptionKey="8A9BE8FD67AF6979E7D20198CFEA50DD3D3799C77AF2B72F"
      validation="SHA1" />
  </system.web>
</configuration>

posted on 2009-11-03 11:02  Mr__BRIGHT  阅读(2243)  评论(0编辑  收藏  举报

导航