C#中FormsAuthentication用法執行個體

來源:互聯網
上載者:User

標籤:time   ted   normal   line   form   分享圖片   pass   lin   公眾   

using System;using System.Web;using System.Web.Security;namespace AuthTest{  public class Authentication  {    /// <summary>    /// 設定使用者登陸成功憑據(Cookie儲存)    /// </summary>    /// <param name="UserName">使用者名稱</param>    /// <param name="PassWord">密碼</param>    /// <param name="Rights">許可權</param>    public static void SetCookie(string UserName,string PassWord,string Rights)    {      //      //String PassWord="test";      //      String UserData = UserName + "#" + PassWord+"#"+Rights;      if (true)      {        //資料放入ticket        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, UserName, DateTime.Now, DateTime.Now.AddMinutes(60), false, UserData);        //資料加密        string enyTicket = FormsAuthentication.Encrypt(ticket);        HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, enyTicket);        HttpContext.Current.Response.Cookies.Add(cookie);      }    }    /// <summary>    /// 判斷使用者是否登陸    /// </summary>    /// <returns>True,Fales</returns>    public static bool isLogin()    {      return HttpContext.Current.User.Identity.IsAuthenticated;    }    /// <summary>    /// 登出登陸    /// </summary>    public static void logOut()    {      FormsAuthentication.SignOut();    }    /// <summary>    /// 擷取憑據中的使用者名稱    /// </summary>    /// <returns>使用者名稱</returns>    public static string getUserName()    {      if (isLogin())      {        string strUserData = ((FormsIdentity)(HttpContext.Current.User.Identity)).Ticket.UserData;        string[] UserData = strUserData.Split(‘#‘);        if (UserData.Length != 0)        {          return UserData[0].ToString();        }        else        {          return "";        }      }      else      {        return "";      }    }    /// <summary>    /// 擷取憑據中的密碼    /// </summary>    /// <returns>密碼</returns>    public static string getPassWord()    {      if (isLogin())      {        string strUserData = ((FormsIdentity)(HttpContext.Current.User.Identity)).Ticket.UserData;        string[] UserData = strUserData.Split(‘#‘);        if (UserData.Length!=0)        {          return UserData[1].ToString();        }        else        {          return "";        }      }      else      {        return "";      }    }    /// <summary>    /// 擷取憑據中的使用者權限    /// </summary>    /// <returns>使用者權限</returns>    public static string getRights()    {      if (isLogin())      {        string strUserData = ((FormsIdentity)(HttpContext.Current.User.Identity)).Ticket.UserData;        string[] UserData = strUserData.Split(‘#‘);        if (UserData.Length!=0)        {          return UserData[2].ToString();        }        else        {          return "";        }      }      else      {        return "";      }    }  }}

除聲明外, 跑步客文章均為原創,轉載請以連結形式標明本文地址
  C#中FormsAuthentication用法執行個體

本文地址:  http://www.paobuke.com/develop/c-develop/pbk23142.html






相關內容同時相容JS和C#的RSA加密解密演算法詳解(對web提交的資料加密傳輸)C#公眾號開發 事件互動詳解C#實現MD5加密的範例程式碼C#環形隊列的實現方法詳解
winform分頁控制項 附源碼下載C#使用GDI繪製矩形的方法理解C#中的Lambda運算式WinForm實現按名稱遞迴尋找控制項的方法

C#中FormsAuthentication用法執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.