[英]Cookies not saved between browser sessions on iOS Safari
我有一個MVC 4網站,用戶可以登錄,我保存一個cookie及其會話信息,這樣他們就不必再次登錄了。
public void SetCookie(HttpCookie cookie)
{
HttpContext.Current.Response.Cookies.Set(cookie);
}
這適用於桌面設備,但是當我在iOS上運行它時,它在100%的時間內都不起作用。 如果我在移動瀏覽器(Chrome或Safari)中打開至少1頁並導航回我的網站,則會找到我的會話cookie,而不必登錄。 但是,如果我關閉該瀏覽器的所有窗口,那么下次我導航回我的網站時,找不到會話cookie。 我在我的cookie上設置了1年的持續時間/到期時間,因此它沒有到期。
到目前為止,我唯一發現的是這是.NET 4.0中的一個錯誤,並在.NET 4.5中得到修復。 我相信我已經在運行.NET 4.5,通過查看我的* .csproj和TargetFrameworkVersion元素:
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>
</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{xxxxxxxxxxxxxxxxxx}</ProjectGuid>
<ProjectTypeGuids>{xxxxxxxx};{xxxxxxxxxxxxx};{xxxxxxxxxxxxxxxx}</ProjectTypeGuids>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>MyApp</RootNamespace>
<AssemblyName>MyApp</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<MvcBuildViews>false</MvcBuildViews>
<UseIISExpress>true</UseIISExpress>
<IISExpressSSLPort />
<IISExpressAnonymousAuthentication />
<IISExpressWindowsAuthentication />
<IISExpressUseClassicPipelineMode />
</PropertyGroup>
這是.NET(或MVC)的錯誤,還是我的編碼? 我是否需要升級到MVC 5(而不是升級到.NET 4.5的原始建議)?
這真的讓我煩惱,因為未來幾個月我的網站的大部分流量將來自移動用戶,如果他們每次都必須繼續登錄,我可能會失去一些(我知道我討厭不得不在移動設備上登錄...)
編輯:
順便說一句 - 這是關於同一主題的另一個頁面: 使用iPhone UIWebView時的Asp.Net Forms身份驗證
我也試過實現這個,它也沒有用:
http://www.bloggersworld.com/index.php/asp-net-forms-authentication-iphone-cookies/
以上內容包括Scott Hanselmans建議修復:
http://www.hanselman.com/blog/FormsAuthenticationOnASPNETSitesWithTheGoogleChromeBrowserOnIOS.aspx
這是我的cookie創建代碼,以防它有用:
private void CreateAndSetAuthenticationCookie(int loginId, string username)
{
HttpCookie cookie = CreateAuthenticationCookie(loginId, username);
_cookieService.SetCookie(cookie);
}
private HttpCookie CreateAuthenticationCookie(int loginId, string username)
{
string userData = string.Format("loginId:{0},username:{1}", loginId, username);
var ticket = new FormsAuthenticationTicket(loginId, username, DateTime.Now, DateTime.Now.AddYears(1), false, userData, FormsAuthentication.FormsCookiePath);
string encryptedTicket = FormsAuthentication.Encrypt(ticket);
return new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
}
我在評論中指出的一件事是,我將“PersistentCookie”設置為假......不確定這是否有所作為,我會做更多的研究。
更新:
首先,PersistentCookie現在設置為true,並且沒有對行為進行任何更改。
下面的評論之一表明我在從iPhone訪問網站時運行Fiddler。 經過非常簡單的步驟來完成設置后,這就是我發現的內容。
我試過的第一個請求指出(我相信)實際問題是什么。 當我檢查第一個請求時,iOS Safari正在發送DNT標頭。 不知道那是什么,我查了一下,這是一個“不跟蹤”標題。
接下來,我去檢查我的Safari設置以關閉它。 猜猜看,它已經關閉了。 當設置(設置 - > Safari - >不跟蹤)設置未設置時,為什么Safari(iOS)發送DNT標頭? 此外,位於其下方的Block Cookies設置為“從不”。
在對此感到沮喪之后,我去檢查Chrome for iOS以查看它是否仍然無效。 有用! 據我所知,我將關閉所有標簽,再次嘗試,關閉所有標簽,然后終止瀏覽器,它仍然有效。 萬歲!
現在,我需要弄清楚為什么iOS Safari會發送DNT標頭...
這是令人難以置信的尷尬。 我發現我已經在“私人瀏覽模式”中使用了我的iOS Safari瀏覽器了!
我覺得我的“軟件開發人員”職稱需要刪除一段時間。 很抱歉浪費每個人的時間。
我認為你用持久性cookie回答了你自己的問題。 常規cookie在瀏覽器會話結束時到期(這通常是關閉瀏覽器)。 持久性cookie具有應該被刪除的日期,並且可以跨瀏覽器會話生存。
您的代碼應如下所示:
private HttpCookie CreateAuthenticationCookie(int loginId, string username)
{
string userData = string.Format("loginId:{0},username:{1}", loginId, username);
var ticket = new FormsAuthenticationTicket(loginId, username,
DateTime.Now, DateTime.Now.AddYears(1),
false, userData, FormsAuthentication.FormsCookiePath);
string encryptedTicket = FormsAuthentication.Encrypt(ticket);
return new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket){{
Expires = DateTime.Now.AddYears(1);
}};
}
這樣,您的cookie將在瀏覽器會話中保留一年。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.