iOS上的Safari浏览器不会保存浏览器会话之间饼干 [英] Cookies not saved between browser sessions on iOS Safari

查看:656
本文介绍了iOS上的Safari浏览器不会保存浏览器会话之间饼干的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC 4网站,用户可以登录我保存的会话信息的cookie,使他们不必再次登录。

I have an MVC 4 website where a user can login and I save a cookie with their session information so they don't have to login again.

public void SetCookie(HttpCookie cookie)
{
    HttpContext.Current.Response.Cookies.Set(cookie);
}

这适用于桌面设备,但是当我在iOS上运行它,它不工作的时候100%。如果我离开至少1页在移动浏览器(无论是Chrome或Safari)打开,然后返回到我的网站,我的会话cookie被发现,我没有登录。但是,如果我关闭浏览器的所有窗口,那么下一次我导航回到我的网站,会话cookie是找不到的。我对我的cookie设置1年的时间/到期,所以它不会过期。

This works on desktop devices, but when I run this on iOS, it doesn't work 100% of the time. If I leave at least 1 page open in the mobile browser (either Chrome or Safari), and navigate back to my site, my session cookie is found and I don't have to login. But if I close ALL windows of that browser, then the next time I navigate back to my site, the session cookie is not found. I'm setting a 1 year duration/expiration on my cookie so it's not expiring.

我到目前为止发现的唯一的事情是,这是在.NET 4.0中的错误,并固定在.NET 4.5。我相信我已经在运行.NET 4.5,通过看我的*的.csproj和TargetFrameworkVersion元素:

The only thing I've found so far is that this is a bug in .NET 4.0 and is fixed in .NET 4.5. I believe I'm already running .NET 4.5, by looking at my *.csproj and the TargetFrameworkVersion element:

<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的原推荐)?

Is this a bug with .NET (or MVC), or is it my coding? Do I need to upgrade to MVC 5 (instead of the original recommendation of upgrading to .NET 4.5)?

这真的被窃听我,因为在未来的未来几个月大部分的流量到我的网站将是移动用户,我可能会失去他们中的一些,如果他们要保持每次记录(我知道我讨厌登录在移动设备上...)

This has really been bugging me since the majority of the traffic to my site in the next coming months will be from mobile users, and I'll probably lose some of them if they have to keep logging in everytime (I know I hate having to login on a mobile device...)

编辑:

顺便说一句 - 这是关于同一主题的另一页:<一href=\"http://stackoverflow.com/questions/4158550/asp-net-forms-authentication-when-using-iphone-uiwebview\">Asp.Net使用iPhone的UIWebView 当窗体身份验证

Btw - Here's another page on the same subject: Asp.Net Forms Authentication when using iPhone UIWebView

和我也尝试实施这一并没有工作,要么:

And I've also tried implementing this and it didn't work either:

http://www.bloggersworld.com/index.php/asp-net-forms-authentication-iphone-cookies/

和上面包括斯科特Hanselmans修复建议:

And the above includes Scott Hanselmans suggested fix:

<一个href=\"http://www.hanselman.com/blog/FormsAuthenticationOnASPNETSitesWithTheGoogleChromeBrowserOnIOS.aspx\" rel=\"nofollow\">http://www.hanselman.com/blog/FormsAuthenticationOnASPNETSitesWithTheGoogleChromeBrowserOnIOS.aspx

下面是我的cookie创建code,在情况下,它可以帮助:

Here's my cookie creation code, in case it helps:

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设置为false ....不知道,如果有差别,我会做更多的研究。

One thing that I pointed out in my comments, I have "PersistentCookie" set to false....not sure if that makes a difference, I'll do more research.

更新:

首先,在PersistentCookie现在设置为true,并且未对行为的任何更改。

First of all, the PersistentCookie is set to true now, and that did not have any changes on the behavior.

下面的一个评论者的建议,我跑提琴手而访问来自iPhone的网站。经历了很短,简单的步骤,以获取安装后,这里是我发现了什么。

One of the commentors below suggested that I run Fiddler while accessing the website from iPhone. After going through the very short and easy steps to get that setup, here's what I found out.

我尝试的第一个请求指出,(我相信)的实际问题是什么。当我检查了第一个请求,iOS的Safari浏览器发送一个DNT头。不知道该说什么了,我看着它,这是一个不跟踪标题。

The first request I tried pointed out (I believe) what the actual problem is. When I examined that first request, iOS Safari is sending a DNT header. Having no idea what that was, I looked it up and it's a "Do Not Track" header.

接下来,我去检查过我的Safari浏览器设置,把它们关掉。你猜怎么着,它已经关闭。凭啥是Safari浏览器(iOS版)发送DNT头球时设置(设置 - > Safari浏览器 - >不跟踪)没有设置?此外,座饼干,这是对的下方,设置为从不。

Next, I went and checked my Safari settings to turn that off. Guess what, it's already off. Why the hell is Safari (iOS) sending a DNT header when the setting (Settings -> Safari -> Do Not Track) is not set? Also, the Block Cookies, which is right below it, is set to "Never".

感到沮丧与之后,我去检查iOS版Chrome,看看是否仍然无法正常工作。有用!从我可以告诉,我会关闭所有标签,再试一次,关闭所有标签,然后杀死浏览器,它仍然有效。万岁!

After getting frustrated with that, I went to check on Chrome for iOS to see if that still doesn't work. IT WORKS! From what I can tell, I'd close all tabs, try again, close all tabs, then kill the browser, and it still works. Hooray!

现在,我需要弄清楚为什么iOS的Safari浏览器发送的DNT头...

Now, I need to figure out why iOS Safari is sending the DNT header...

推荐答案

这是可怕的embarrasing。我发现我已经在隐私浏览模式我的iOS Safari浏览器多年!

This is HORRIBLY embarrasing. I found out that I've had my iOS Safari browser in "Private Browsing Mode" FOR YEARS!

我觉得像我的软件开发职位需要一段时间删除。对不起,浪费大家的时间。

I feel like my "Software Developer" job title needs removed for awhile. Sorry for wasting everyone's time.

这篇关于iOS上的Safari浏览器不会保存浏览器会话之间饼干的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆