C#中FormsAuthentication用法實例
更新時間:2015年02月15日 09:56:49 投稿:junjie
這篇文章主要介紹了C#中FormsAuthentication用法實例,本文直接給出實現(xiàn)代碼,需要的朋友可以參考下
using System; using System.Web; using System.Web.Security; namespace AuthTest { public class Authentication { /// <summary> /// 設(shè)置用戶登陸成功憑據(jù)(Cookie存儲) /// </summary> /// <param name="UserName">用戶名</param> /// <param name="PassWord">密碼</param> /// <param name="Rights">權(quán)限</param> public static void SetCookie(string UserName,string PassWord,string Rights) { // //String PassWord="test"; // String UserData = UserName + "#" + PassWord+"#"+Rights; if (true) { //數(shù)據(jù)放入ticket FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, UserName, DateTime.Now, DateTime.Now.AddMinutes(60), false, UserData); //數(shù)據(jù)加密 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> /// 獲取憑據(jù)中的用戶名 /// </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> /// 獲取憑據(jù)中的密碼 /// </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> /// 獲取憑據(jù)中的用戶權(quán)限 /// </summary> /// <returns>用戶權(quán)限</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 ""; } } } }
相關(guān)文章
C#數(shù)據(jù)綁定(DataBinding)簡單實現(xiàn)方法
這篇文章主要介紹了C#數(shù)據(jù)綁定(DataBinding)簡單實現(xiàn)方法,以簡單實例形式簡單分析了C#實現(xiàn)數(shù)據(jù)綁定與讀取的方法,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08C#使用Region對圖形區(qū)域構(gòu)造和填充的方法
這篇文章主要介紹了C#使用Region對圖形區(qū)域構(gòu)造和填充的方法,實例分析了Region類圖形操作的相關(guān)技巧,需要的朋友可以參考下2015-06-06