The namespace WebApplication. App_Start
{
Public class WFFormPrincipal: IPrincipal
{
Public IIdentity Identity {get; Private set; }
//user data
Public WFFormsAuthentication UserData {get; Private set; }
Public WFFormPrincipal (FormsAuthenticationTicket ticket, WFFormsAuthentication userData)
{
If (ticket==null)
Throw new ArgumentNullException (" ticket ");
If (userData=null https://bbs.csdn.net/topics/=
Throw new ArgumentNullException (" userData ");
Identity=new FormsIdentity (ticket);
The UserData=https://bbs.csdn.net/topics/userData;
}
//character validation
Public bool IsInRole (string role)
{
return false;
}
}
Public class WFFormsAuthentication
{
Public string SessionId {get; The set; }
Public string UserId {get; The set; }
Public string Name {get; The set; }
Public string TrueName {get; The set; }
Public string LoginTime {get; The set; }
Private const int CookieSaveDays=20;
Public static string SetAuthCookie (string username, WFFormsAuthentication userData, bool remember)
{
If (userData=null https://bbs.csdn.net/topics/=
Throw new ArgumentNullException (" userData ");
Var data=https://bbs.csdn.net/topics/Newtonsoft.Json.JsonConvert.SerializeObject (userData);
Var expires=remember? DateTime. Now. AddDays (30) : DateTime. Now the AddDays (1);
Var ticket=new FormsAuthenticationTicket (1, username, DateTime. Now, expires, true, data);
Var cookieValue=https://bbs.csdn.net/topics/FormsAuthentication.Encrypt (ticket);
Var cookie=new HttpCookie (FormsAuthentication FormsCookieName, cookieValue)
{
HttpOnly=false,
Secure=FormsAuthentication RequireSSL,
Domain=FormsAuthentication CookieDomain,
Path=FormsAuthentication FormsCookiePath,
};
Cookies. Expires=Expires;
HttpContext context=HttpContext. Current;
If (the context==null)
Throw new InvalidOperationException ();
Context. The Response. Cookies. Remove (cookie. The Name);
The context. The Response. Cookies. Add (Cookies);
Return cookieValue;
//the context. The Response. Cookies. The Add (new HttpCookie (" test ", a DateTime. Now. ToString (" yyyyMMdd HHmmss ")) {Expires=DateTime. Now. AddDays (1)});
}
Public static void SignOut ()
{
Var cookie=new HttpCookie (FormsAuthentication FormsCookieName, "")
{
HttpOnly=true,
Secure=FormsAuthentication RequireSSL,
Domain=FormsAuthentication CookieDomain,
Path=FormsAuthentication FormsCookiePath,
Expires=DateTime. Now. AddDays (1)
};
HttpContext context=HttpContext. Current;
If (the context==null)
Throw new InvalidOperationException ();
The context. The Response. Cookies. Add (Cookies);
}
Public static WFFormPrincipal TryParsePrincipal (HttpContext context)
{
If (the context==null | | context. Request==null)
Throw new ArgumentNullException (" context ");
Var cookie=context. Request. Cookies [FormsAuthentication. FormsCookieName];
If (cookies!=null & amp; & ! String. IsNullOrEmpty (cookies. Value))
{
Try
{
Var ticket=FormsAuthentication. Decrypt (cookies. Value);
If (ticket!=null & amp; & ! String. IsNullOrEmpty (ticket. UserData))
{
Var userData=https://bbs.csdn.net/topics/Newtonsoft.Json.JsonConvert.DeserializeObject
If (userData!=null)
{
Return new WFFormPrincipal (ticket, userData);
//return IsValid (userData SessionId)? New WFFormPrincipal (ticket, userData) : null;
}
}
}
Catch
{
}
}
return null;
}
}
}
//I'm here to write cookies, by implementing the user login but did after the load balancing server has been login will not come
CodePudding user response:
How to use cookies, should use the session