ASP.NET MVC 使用 Safari 浏览器表单身份验证错误

问题描述 投票:0回答:1

我在 ASP.NET MVC Web 应用程序中使用表单身份验证,它在 Windows 2016 上运行的实时服务器上运行良好。但是当我们将同一网站部署到 Windows Server 2022 时,Safari 浏览器登录 cookie 将从请求中刷新.

我正在发送表单身份验证 cookie 作为响应,但在下一个请求中没有收到它。

这是我的登录代码:

var cookie = FormsAuthentication.GetAuthCookie("username", true);

var oldTicket = FormsAuthentication.Decrypt(cookie.Value);
var newTicket = new FormsAuthenticationTicket(
            oldTicket.Version,
            oldTicket.Name,
            oldTicket.IssueDate,
            oldTicket.Expiration,
            oldTicket.IsPersistent,
            "Role1,Role2");

if (!FormsAuthentication.CookiesSupported)
{
    string encryptedTicket = FormsAuthentication.Encrypt(newTicket);
    FormsAuthentication.SetAuthCookie(encryptedTicket, true);
}
else
{
    cookie.Value = FormsAuthentication.Encrypt(newTicket);
    HttpContext.Response.Cookies.Add(cookie);
}

在这段代码之后,我还添加了以下内容

System.Web.HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(
        new System.Security.Principal.GenericIdentity("username"),
        "Role1,Role2");

当我尝试在下一个请求中获取它时,它在

global.asax
中为空:

// It's null here
var authCookies = Context.Request.Cookies[FormsAuthentication.FormsCookieName];

使用 Google Chrome 时,它工作得很好,只有 Safari 会导致此问题。我用 iPhone 和 Mac Safari 浏览器进行了测试,发现了同样的问题。

c# asp.net-mvc forms-authentication
1个回答
0
投票

我尝试了一切来找出问题所在,但我无法找出这个问题。最后我将 Windows 服务器从 2022 年降级到 2019 年,立即解决了问题。

© www.soinside.com 2019 - 2024. All rights reserved.