Mohsin Sharif
Mohsin Sharif

Reputation: 31

ASP.NET MVC forms authentication error with Safari browser

I am using forms authentication in my ASP.NET MVC web application, it was working fine on live server running on Windows 2016. But when we deployed the same website to a Windows Server 2022, Safari browser login cookies are being flushed from the request.

I am sending forms authentication cookie in response but not receiving it in the next request.

This is my login code:

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);
}

After this code, I also added the following

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

And when I try to get it in next request, it's null in global.asax:

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

With Google Chrome, it's working perfectly fine, only Safari is causing this issue. I tested with iPhone and Mac Safari browsers and see the same issue.

Upvotes: 0

Views: 76

Answers (1)

Mohsin Sharif
Mohsin Sharif

Reputation: 31

I tried everything to identify the issue, But I was unable to figure out this issue. Then finally I downgraded my Windows server from 2022 to 2019 which resolved the issue right away.

Upvotes: 0

Related Questions