加密cookies
EnCookies()
...{
UserIdentity user = dal.CheckLogin(Request.Params[ " userName " ].ToString(), Request.Params[ " passWord " ].ToString());
if (user != null )
...{
int saveCookiesDayNum = Convert.ToInt16(ConfigurationManager.AppSettings[ " saveCookiesDayNum " ]);
string cookstr;
FormsAuthenticationTicket ft = new FormsAuthenticationTicket( 1 , user.UserId.ToString(), DateTime.Now, DateTime.Now.AddDays(saveCookiesDayNum), Chb.Checked, user.UserName.ToString() + " , " + user.UserRole.ToString());
cookstr = FormsAuthentication.Encrypt(ft);
HttpCookie hc = new HttpCookie(FormsAuthentication.FormsCookieName, cookstr);
hc.Expires = ft.Expiration;
Response.Cookies.Add(hc);
}
获取用户身份获取用户身份 #region 获取用户身份
/**//**//**/ /// <summary>
///
/// </summary>
/// <returns></returns>
public static UserIdentity GetUserIdentity()
......{
UserIdentity userIdentity = new UserIdentity();
if (HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName] == null )
......{
return null ;
}
else
......{
System.Web.Security.FormsIdentity userFormsIdentity = (System.Web.Security.FormsIdentity)HttpContext.Current.User.Identity;
FormsAuthenticationTicket ticket = userFormsIdentity.Ticket;
userIdentity.UserId = Convert.ToInt32(ticket.Name);
userIdentity.UserName = ticket.UserData.Split( ' , ' )[ 0 ];
userIdentity.UserRole = Convert.ToInt32(ticket.UserData.Split( ' , ' )[ 1 ]);
return userIdentity;
}
}
/**//**//**/ /// <summary>
/// 用户身份
/// </summary>
public class UserIdentity
......{
public UserIdentity()
......{
}
private int _userid;
private string _username;
private int _userrole;
/**//**//**/ /// <summary>
///
/// </summary>
public int UserId
......{
set
......{
_userid = value;
}
get
......{
return _userid;
}
}
/**//**//**/ /// <summary>
///
/// </summary>
public string UserName
......{
set
......{
_username = value;
}
get
......{
return _username;
}
}
/**//**//**/ /// <summary>
///
/// </summary>
public int UserRole
......{
set
......{
_userrole = value;
}
get
......{
return _userrole;
}
}
}
EnCookies()
...{
UserIdentity user = dal.CheckLogin(Request.Params[ " userName " ].ToString(), Request.Params[ " passWord " ].ToString());
if (user != null )
...{
int saveCookiesDayNum = Convert.ToInt16(ConfigurationManager.AppSettings[ " saveCookiesDayNum " ]);
string cookstr;
FormsAuthenticationTicket ft = new FormsAuthenticationTicket( 1 , user.UserId.ToString(), DateTime.Now, DateTime.Now.AddDays(saveCookiesDayNum), Chb.Checked, user.UserName.ToString() + " , " + user.UserRole.ToString());
cookstr = FormsAuthentication.Encrypt(ft);
HttpCookie hc = new HttpCookie(FormsAuthentication.FormsCookieName, cookstr);
hc.Expires = ft.Expiration;
Response.Cookies.Add(hc);
}
获取用户身份获取用户身份 #region 获取用户身份
/**//**//**/ /// <summary>
///
/// </summary>
/// <returns></returns>
public static UserIdentity GetUserIdentity()
......{
UserIdentity userIdentity = new UserIdentity();
if (HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName] == null )
......{
return null ;
}
else
......{
System.Web.Security.FormsIdentity userFormsIdentity = (System.Web.Security.FormsIdentity)HttpContext.Current.User.Identity;
FormsAuthenticationTicket ticket = userFormsIdentity.Ticket;
userIdentity.UserId = Convert.ToInt32(ticket.Name);
userIdentity.UserName = ticket.UserData.Split( ' , ' )[ 0 ];
userIdentity.UserRole = Convert.ToInt32(ticket.UserData.Split( ' , ' )[ 1 ]);
return userIdentity;
}
}
/**//**//**/ /// <summary>
/// 用户身份
/// </summary>
public class UserIdentity
......{
public UserIdentity()
......{
}
private int _userid;
private string _username;
private int _userrole;
/**//**//**/ /// <summary>
///
/// </summary>
public int UserId
......{
set
......{
_userid = value;
}
get
......{
return _userid;
}
}
/**//**//**/ /// <summary>
///
/// </summary>
public string UserName
......{
set
......{
_username = value;
}
get
......{
return _username;
}
}
/**//**//**/ /// <summary>
///
/// </summary>
public int UserRole
......{
set
......{
_userrole = value;
}
get
......{
return _userrole;
}
}
}