一般的管理系统使用的是Session或Cookies来对进行对用户的身份验证的,而Asp.Net本身就提供了一种验证机制:FormsAuthenticationTicket,本人觉得这种机制是基于Cookies的。
定义一个用户信息类:
登录:
注销:FormsAuthentication.SignOut();
判断:User.Identity.IsAuthenticated;
使用:ShopManageShop user = ShopManageShop.GetCurrent();
定义一个用户信息类:
using System;
using System.Web;
using System.Web.UI;
using System.Web.Security;
public class ShopManageUser
{
private string m_UserName;
private int m_Id;
private int m_Type;
private string m_Name;
private string m_ShopCode;
public ShopManageUser()
{
}
/// <summary>
/// 登录用户名
/// </summary>
public string UserName
{
get{return m_UserName;}
set{m_UserName = value;}
}
/// <summary>
/// ID
/// </summary>
public int Id
{
get{return m_Id;}
set{m_Id = value;}
}
/// <summary>
/// 类型
/// </summary>
public int Type
{
get{return m_Type;}
set{m_Type = value;}
}
/// <summary>
/// 姓名
/// </summary>
public string Name
{
get{return m_Name;}
set{m_Name = value;}
}
/// <summary>
/// 商铺编码
/// </summary>
public string ShopCode
{
get{return m_ShopCode;}
set{m_ShopCode = value;}
}
public override string ToString()
{
string[] strUser = new string[]{this.Id.ToString(), this.UserName, this.Type.ToString(), this.Name, this.ShopCode};
return string.Join("\t", strUser);
}
/// <summary>
/// 获取当前用户
/// </summary>
/// <returns></returns>
public static ShopManageUser GetCurrent()
{
ShopManageUser u = new ShopManageUser();
HttpCookie authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
if(authCookie == null)
{
return null;
}
FormsAuthenticationTicket authTicket = null;
try
{
authTicket = FormsAuthentication.Decrypt(authCookie.Value);
}
catch
{
throw;
}
if (authTicket == null)
{
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;
}
}
using System.Web;
using System.Web.UI;
using System.Web.Security;
public class ShopManageUser
{
private string m_UserName;
private int m_Id;
private int m_Type;
private string m_Name;
private string m_ShopCode;
public ShopManageUser()
{
}
/// <summary>
/// 登录用户名
/// </summary>
public string UserName
{
get{return m_UserName;}
set{m_UserName = value;}
}
/// <summary>
/// ID
/// </summary>
public int Id
{
get{return m_Id;}
set{m_Id = value;}
}
/// <summary>
/// 类型
/// </summary>
public int Type
{
get{return m_Type;}
set{m_Type = value;}
}
/// <summary>
/// 姓名
/// </summary>
public string Name
{
get{return m_Name;}
set{m_Name = value;}
}
/// <summary>
/// 商铺编码
/// </summary>
public string ShopCode
{
get{return m_ShopCode;}
set{m_ShopCode = value;}
}
public override string ToString()
{
string[] strUser = new string[]{this.Id.ToString(), this.UserName, this.Type.ToString(), this.Name, this.ShopCode};
return string.Join("\t", strUser);
}
/// <summary>
/// 获取当前用户
/// </summary>
/// <returns></returns>
public static ShopManageUser GetCurrent()
{
ShopManageUser u = new ShopManageUser();
HttpCookie authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
if(authCookie == null)
{
return null;
}
FormsAuthenticationTicket authTicket = null;
try
{
authTicket = FormsAuthentication.Decrypt(authCookie.Value);
}
catch
{
throw;
}
if (authTicket == null)
{
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 = "";
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);
user.Id = 1;
user.UserName = "faib";
user.Name = "";
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);
注销:FormsAuthentication.SignOut();
判断:User.Identity.IsAuthenticated;
使用:ShopManageShop user = ShopManageShop.GetCurrent();