usingSystem;
usingSystem.Web;
usingSystem.Web.Security;
namespaceAuthTest
{
publicclassAuthentication
{
///<summary>
///设置用户登陆成功凭据(Cookie存储)
///</summary>
///<paramname="UserName">用户名</param>
///<paramname="PassWord">密码</param>
///<paramname="Rights">权限</param>
publicstaticvoidSetCookie(stringUserName,stringPassWord,stringRights)
{
//
//StringPassWord="test";
//
StringUserData=UserName+"#"+PassWord+"#"+Rights;
if(true)
{
//数据放入ticket
FormsAuthenticationTicketticket=newFormsAuthenticationTicket(1,UserName,DateTime.Now,DateTime.Now.AddMinutes(60),false,UserData);
//数据加密
stringenyTicket=FormsAuthentication.Encrypt(ticket);
HttpCookiecookie=newHttpCookie(FormsAuthentication.FormsCookieName,enyTicket);
HttpContext.Current.Response.Cookies.Add(cookie);
}
}
///<summary>
///判断用户是否登陆
///</summary>
///<returns>True,Fales</returns>
publicstaticboolisLogin()
{
returnHttpContext.Current.User.Identity.IsAuthenticated;
}
///<summary>
///注销登陆
///</summary>
publicstaticvoidlogOut()
{
FormsAuthentication.SignOut();
}
///<summary>
///获取凭据中的用户名
///</summary>
///<returns>用户名</returns>
publicstaticstringgetUserName()
{
if(isLogin())
{
stringstrUserData=((FormsIdentity)(HttpContext.Current.User.Identity)).Ticket.UserData;
string[]UserData=strUserData.Split('#');
if(UserData.Length!=0)
{
returnUserData[0].ToString();
}
else
{
return"";
}
}
else
{
return"";
}
}
///<summary>
///获取凭据中的密码
///</summary>
///<returns>密码</returns>
publicstaticstringgetPassWord()
{
if(isLogin())
{
stringstrUserData=((FormsIdentity)(HttpContext.Current.User.Identity)).Ticket.UserData;
string[]UserData=strUserData.Split('#');
if(UserData.Length!=0)
{
returnUserData[1].ToString();
}
else
{
return"";
}
}
else
{
return"";
}
}
///<summary>
///获取凭据中的用户权限
///</summary>
///<returns>用户权限</returns>
publicstaticstringgetRights()
{
if(isLogin())
{
stringstrUserData=((FormsIdentity)(HttpContext.Current.User.Identity)).Ticket.UserData;
string[]UserData=strUserData.Split('#');
if(UserData.Length!=0)
{
returnUserData[2].ToString();
}
else
{
return"";
}
}
else
{
return"";
}
}
}
}