一般的管理系统使用的是Session或Cookies来对进行对用户的身份验证的,而Asp.Net本身就提供了一种验证机制:FormsAuthenticationTicket,本人觉得这种机制是基于Cookies的。
定义一个用户信息类:
using System;
using System.Web;
using System.Web.UI;
using System.Web.Security;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public class ShopManageUser
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
private string m_UserName;
private int m_Id;
private int m_Type;
private string m_Name;
private string m_ShopCode;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public ShopManageUser()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 登录用户名
/// </summary>
public string UserName
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return m_UserName;}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{m_UserName = value;}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// ID
/// </summary>
public int Id
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return m_Id;}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{m_Id = value;}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 类型
/// </summary>
public int Type
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return m_Type;}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{m_Type = value;}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 姓名
/// </summary>
public string Name
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return m_Name;}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{m_Name = value;}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 商铺编码
/// </summary>
public string ShopCode
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return m_ShopCode;}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{m_ShopCode = value;}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override string ToString()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string[] strUser = new string[]
{this.Id.ToString(), this.UserName, this.Type.ToString(), this.Name, this.ShopCode};
return string.Join("\t", strUser);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获取当前用户
/// </summary>
/// <returns></returns>
public static ShopManageUser GetCurrent()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ShopManageUser u = new ShopManageUser();
HttpCookie authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
if(authCookie == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return null;
}
FormsAuthenticationTicket authTicket = null;
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
authTicket = FormsAuthentication.Decrypt(authCookie.Value);
}
catch
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw;
}
if (authTicket == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return null;
}
string[] strUser = authTicket.UserData.Split('\t');
u.Id = int.Parse(strUser[0]);
u.UserName = strUser[1];
u.Type = int.Parse(strUser[2]);
u.Name = strUser[3];
u.ShopCode = strUser[4];
return u;
}
}
登录:
ShopManageUser user = new ShopManageUser();
user.Id = 1;
user.UserName = "faib";
user.Name = "";
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, user.UserName, DateTime.Now, DateTime.Now.AddMinutes(30), false, user.ToString());
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
HttpContext.Current.Response.Cookies.Add(authCookie);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
注销:FormsAuthentication.SignOut();
判断:User.Identity.IsAuthenticated;
使用:ShopManageShop user = ShopManageShop.GetCurrent();