gpt4 book ai didi

android - Cookie 不适用于移动浏览器(使用 ASP.NET MVC )

转载 作者:行者123 更新时间:2023-12-04 18:02:37 24 4
gpt4 key购买 nike

我有一个 ASP.NET C# MVC4 网站,大部分时间都运行良好。但是,当我们在移动设备上进行测试时,我用于身份验证的 cookie 将不起作用。我在我的 Controller 操作中设置了 Auth cookie,但是在下次调用时尝试访问它们时,它们不存在。再一次,这只是移动设备上的问题。在 IE、Chrome 和 Firefox 的桌面版本中运行良好。不适用于 Android 上的 Chrome。

写入 cookie 的代码(在 Controller 操作中):

    //Set information into object that can be read out of the cookie later
FormsAuthModel UserDataObj = new FormsAuthModel
{
UserID = dmUser.ID,
PasswordChange = dmUser.PasswordChange
};
string UserData = Convert.ToBase64String(clsShared.Serialize(UserDataObj));

//Create the ticket
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, dmUser.UserName, DateTime.Now, DateTime.Now.AddDays(1), false, UserData, FormsAuthentication.FormsCookiePath);

// Encrypt the ticket
string encTicket = FormsAuthentication.Encrypt(ticket);

// Create the cookie
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
System.Web.HttpContext.Current.Response.Cookies.Add(cookie);

读取 cookie 的代码(在 Global.asax.cs - Application_PostAuthenticateRequest 中):

    HttpCookie authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null)
{
try
{
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
UserDataObj = (FormsAuthModel)clsShared.Deserialize(Convert.FromBase64String(authTicket.UserData), typeof(FormsAuthModel));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
//WriteEvent(string.Format("Error deserializing auth ticket - {0}", ex.Message), EventLogEntryType.Error);
}
}

AuthCookie 在后续请求中始终为 null。用户看到的是一个登录屏幕,他们填写了它,然后他们被重定向回登录屏幕。

我在搜索中找不到任何有助于解释为什么所有移动请求(我的手机、平板电脑和其他用户的手机)的行为与桌面浏览器不同的原因。

如有任何帮助,我们将不胜感激。

谢谢!!

最佳答案

好的,我找到了解决方案,但我不确定原因。我按如下方式更改了 cookie 创建代码并且它起作用了。

    //Set information into object that can be read out of the cookie later
FormsAuthModel UserDataObj = new FormsAuthModel
{
UserID = dmUser.ID,
PasswordChange = dmUser.PasswordChange
};
string UserData = Convert.ToBase64String(clsShared.Serialize(UserDataObj));

//Create the ticket
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, dmUser.UserName, DateTime.Now, DateTime.Now.AddDays(1), false, UserData, FormsAuthentication.FormsCookiePath);

// Encrypt the ticket
string encTicket = FormsAuthentication.Encrypt(ticket);

// Create the cookie - FIX IS HERE!!!
Response.Cookies[FormsAuthentication.FormsCookieName].Value = encTicket;
//HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
//Response.Cookies.Add(cookie);

请注意,唯一的变化是通过直接设置值来添加 cookie,而不是创建 cookie 对象并将其添加到集合中。

即- Response.Cookies["Name"] = Value;

我从这篇 MS 文章中得到了这个想法:https://msdn.microsoft.com/en-us/library/ms178194.aspx .

那么有人知道为什么这会有所作为吗?我以前多次使用cookie实例方法,从来没有遇到过这个问题。

关于android - Cookie 不适用于移动浏览器(使用 ASP.NET MVC ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32487822/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com