问题
There appears to be an issue with iOS 11 (still an issue as of iOS 11.0.1), home screen web apps, and cookies. When a cookie is set from the server, iOS 11 seems to intermittently delete the cookie. Other times, when the server expires a cookie, iOS 11 appears to intermittently fail to delete the cookie. These two behaviors occur after closing and re-opening the home screen web app and don't seem to be reproducible in Safari. These behaviors also don't appear to be present in iOS 10 or iOS 9.
I am guessing that this is a bug in iOS 11, but was wondering if anyone else has happened upon this issue and has figured out how to work around it.
For example, this ASP.NET code creates and expires a cookie upon login and logout. When the Index URL is added to the home screen, and the app is closed and re-opened (i.e., the Index URL gets reloaded) after either logging in or logging out, the app will sometimes show in the incorrect state upon reloading (logged out after logging in, or still logged in after logging out).
public class HomeController : Controller
{
[Route("~/")]
public ActionResult Index()
{
var loginCookie = Request.Cookies["Login"];
if (loginCookie != null)
{
return View();
}
else
{
return Redirect("Login");
}
}
[Route("~/login/")]
public ActionResult Login()
{
return View();
}
[HttpPost]
[Route("~/login/")]
public ActionResult LoginComplete()
{
var authCookie = new HttpCookie("Login", "login token")
{
Expires = DateTime.Now.AddMinutes(20)
};
Response.Cookies.Add(authCookie);
return Redirect("~/");
}
[Route("~/logout/")]
public ActionResult Logout()
{
var authCookie = new HttpCookie("Login", "login token")
{
Expires = DateTime.Now.AddMinutes(-1000)
};
Response.Cookies.Add(authCookie);
return View();
}
}
Here is the full repo if anyone wants to try reproducing the issue. Keep in mind that this is an intermittent issue, and multiple attempts may be required.
来源:https://stackoverflow.com/questions/46551996/ios-11-cookie-expiration-in-home-screen-web-apps