zoukankan      html  css  js  c++  java
  • asp.net登录验证FormsAuthenticationTicket和FormsAuthentication类

    登录部分使用的类

    FormsAuthentication   为 Web 应用程序管理 Forms 身份验证服务。 

    配置启用身份验证,WEB.config配置:

    <system.web>  
      <authentication mode="Forms">  
        <forms loginUrl="login.aspx" />  
      </authentication>  
      <authorization>  
        <deny users="?" />  
      </authorization>  
    </system.web>

    属性: FormsCookieName,用于存储 Forms 身份验证票证的 Cookie 名称。 默认值是“.ASPXAUTH”。

    下面的代码示例设置FormsCookieName属性值,使用Web.config 文件中的name属性。

    <authentication mode="Forms">  
      <forms loginUrl="member_login.aspx"  
        cookieless="UseCookies"  
        name=".ASPXFORMSAUTH" />  
    </authentication>

    FormsCookieName使用 ASP.NET 应用程序配置文件name属性设置属性值。

    FormsCookieName用于引用存储的 cookieFormsAuthenticationTicket信息。

    FormsAuthenticationTicket  提供对票证的属性和值的访问,这些票证用于 Forms 身份验证对用户进行标识。

    FormsAuthenticationTicket类用于创建一个对象,表示窗体身份验证用于标识身份验证的用户的身份验证票证。

     private void Login_Click(Object sender, EventArgs e)
      {
        // Create a custom FormsAuthenticationTicket containing
        // application specific data for the user.
    
        string username     = UserNameTextBox.Text;
        string password     = UserPassTextBox.Text;
        bool   isPersistent = false;
    
        if (Membership.ValidateUser(username, password))
        {
          string userData = "ApplicationSpecific data for this user.";
    
          FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
            username,
            DateTime.Now,
            DateTime.Now.AddMinutes(30),
            isPersistent,
            userData,
            FormsAuthentication.FormsCookiePath);
    
          // Encrypt the ticket.
          string encTicket = FormsAuthentication.Encrypt(ticket);
    
          // Create the cookie.
          Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));
    
          // Redirect back to original URL.
          Response.Redirect(FormsAuthentication.GetRedirectUrl(username, isPersistent));
        }
        else
        {
          Msg.Text = "Login failed. Please check your user name and password and try again.";
        }
      }

    验证部分

    FormsIdentity和FormsAuthenticationTicket

    string cookieName = FormsAuthentication.FormsCookieName;
    HttpCookie authCookie = App.Context.Request.Cookies[cookieName];
     if (null == authCookie)
       {
         // 沒有驗證 Cookie。
         return;
       }
     if (authCookie.Value == null)
      {
        // 沒有驗證 Cookie。
        return;
      }
     FormsAuthenticationTicket authTicket = null;
     try
      {
        authTicket = FormsAuthentication.Decrypt(authCookie.Value);
      }
      catch (Exception ex)
      {
        // 記錄例外狀況詳細資料 (為簡單起見已省略)
        FileTxtLogs.WriteLog(ex.ToString());
        return;
      }
    
      if (null == authTicket)
      {
         // Cookie 無法解密。
         return;
      }
    
      // 建立 Identity 物件
      FormsIdentity id = new FormsIdentity(authTicket);
      App.Context.User = new PermissionPrincipal(id);

    IsAuthenticated  获取一个值,该值指示是否进行身份验证。

    文章:How to: Implement Simple Forms Authentication

  • 相关阅读:
    Android 高仿微信支付密码输入控件
    ListView用法总结
    我的感悟
    Android Scroll分析——滑动效果产生
    如何查看Android的Keystore文件的SHA1值
    Android 事件拦截机制一种粗鄙的解释
    Android 自定义ViewGroup
    Android 自定义View 总结
    Android 自定义View 三板斧之三——重写View来实现全新控件
    Android 自定义View 三板斧之二——组合现有控件
  • 原文地址:https://www.cnblogs.com/Tpf386/p/10143237.html
Copyright © 2011-2022 走看看