饼干很快就会死掉
我在服务器上遇到 cookie 问题。饼干很快就死了。在 localhost cookie 做得很好。但对方的陌生之处。 cookie 本身存在,但该代码看不到它
对于创建 cookie,我使用此代码
public void SignIn(BaseUser user, bool isRememberMe)
{
string data = string.Format("{0},{1}, {2}", user.Id, user.Role.RoleName, user.Role.IsSuperRole);
if (string.IsNullOrEmpty(user.UserName))
{
throw new ArgumentException("Error", "userName");
}
var ticket = new FormsAuthenticationTicket(
1,
user.UserName,
DateTime.Now.ToLocalTime(),
DateTime.Now.ToLocalTime().AddDays(30),
isRememberMe,
data,
FormsAuthentication.FormsCookiePath);
var encryptedTicket = FormsAuthentication.Encrypt(ticket);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket)
{
HttpOnly = true,
Secure = FormsAuthentication.RequireSSL,
Path = FormsAuthentication.FormsCookiePath,
};
if (FormsAuthentication.CookieDomain != null)
{
cookie.Domain = FormsAuthentication.CookieDomain;
}
HttpContext.Current.Response.Cookies.Add(cookie);
}
对于读取和更改 cookie,我使用此代码
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
Identity id = null;
if (authCookie != null)
{
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
id = this.GetUserIdentity(authTicket);
authCookie.Expires = DateTime.Now.AddDays(30);
HttpContext.Current.Response.Cookies.Add(authCookie);
}
else
{
id = new Identity();
}
var user = new Principal(id, id.RoleName);
Context.User = user;
}
private Identity GetUserIdentity(FormsAuthenticationTicket ticket)
{
string[] userDetal = ticket.UserData.Split(Convert.ToChar(","));
int id = int.Parse(userDetal[0]);
string roleName = userDetal[1];
bool isSuperRole;
bool.TryParse(userDetal[2], out isSuperRole);
string userName = ticket.Name;
return new Identity(id, userName, true, isSuperRole, roleName);
}
I have problem with cookie at server. Cookie quickly die. At localhost cookie good work. But the strangeness of the other. Itself cookie is present, but that code does not see it
For create cookie I used this code
public void SignIn(BaseUser user, bool isRememberMe)
{
string data = string.Format("{0},{1}, {2}", user.Id, user.Role.RoleName, user.Role.IsSuperRole);
if (string.IsNullOrEmpty(user.UserName))
{
throw new ArgumentException("Error", "userName");
}
var ticket = new FormsAuthenticationTicket(
1,
user.UserName,
DateTime.Now.ToLocalTime(),
DateTime.Now.ToLocalTime().AddDays(30),
isRememberMe,
data,
FormsAuthentication.FormsCookiePath);
var encryptedTicket = FormsAuthentication.Encrypt(ticket);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket)
{
HttpOnly = true,
Secure = FormsAuthentication.RequireSSL,
Path = FormsAuthentication.FormsCookiePath,
};
if (FormsAuthentication.CookieDomain != null)
{
cookie.Domain = FormsAuthentication.CookieDomain;
}
HttpContext.Current.Response.Cookies.Add(cookie);
}
For read and change cookie, I use this code
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
Identity id = null;
if (authCookie != null)
{
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
id = this.GetUserIdentity(authTicket);
authCookie.Expires = DateTime.Now.AddDays(30);
HttpContext.Current.Response.Cookies.Add(authCookie);
}
else
{
id = new Identity();
}
var user = new Principal(id, id.RoleName);
Context.User = user;
}
private Identity GetUserIdentity(FormsAuthenticationTicket ticket)
{
string[] userDetal = ticket.UserData.Split(Convert.ToChar(","));
int id = int.Parse(userDetal[0]);
string roleName = userDetal[1];
bool isSuperRole;
bool.TryParse(userDetal[2], out isSuperRole);
string userName = ticket.Name;
return new Identity(id, userName, true, isSuperRole, roleName);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论