其他分享
首页 > 其他分享> > 解密Cookie是否会将其转换为本地时间?

解密Cookie是否会将其转换为本地时间?

作者:互联网

我有这样创建自己的asp.net cookie:

var authTicket = new FormsAuthenticationTicket(
    version,
    userName,
    DateTime.UtcNow,
    DateTime.UtcNow.AddMinutes(30),
    createPersistentCookie,
    userData,
    "/");

string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);

如您所见,一切都在UTC时间.

当我解密它时:

var cookie = HttpContext.Current.Request.Cookies.Get(FormsAuthentication.FormsCookieName);

if (cookie != null)
{
    var ticket = FormsAuthentication.Decrypt(cookie.Value);
    return ticket.Expiration.Ticks;
}
else
{
    return 0;
}

它返回本地时间.那么,它会自动转换还是其他?如果是这样,如何将其恢复为UTC时间?

解决方法:

MSDN开始:

FormsAuthenticationTicket.Expiration Property

Gets the local date and time at which the forms-authentication ticket expires.

您可以使用DateTime.ToUniversalTime方法将DateTime转换为UTC:

return ticket.Expiration.ToUniversalTime().Ticks;

标签:asp-net,c,net,asp-net-mvc
来源: https://codeday.me/bug/20191210/2101591.html