Search code examples
asp.netcookiesforms-authentication.net-4.7.2samesite

Condtionally set ASP.NET session and authentication cookies samesite value based on browsers


I have done the following changes to my web.config and I'm able to server both the authentication and the session cookie with samesite=none and secure.

the problem is for browsers such as chrome 51-66 sending samesite=none invalidates the cookie and then the user does not have a session and is not able to login. https://www.chromium.org/updates/same-site/incompatible-clients

is there a way to maybe extend the classes that create those cookies or some other way to conditionally set the samesite parameter based on the browser/useragent

<system.web>
    <httpCookies sameSite="None"/>
    <sessionState cookieSameSite="None" />
    <authentication mode="Forms">
        <forms cookieSameSite="None" />
    </authentication>

Solution

  • While not a complete solution (as it only covers the session cookie as in my use case I set my forms authentication cookie manually), I implemented the following in my MVC5 application to handle the setting of the SameSite attribute:

    In Global.asax.cs

    protected void Session_Start(object sender, EventArgs e)
    {
        var cookie = Response.Cookies["ASP.NET_SessionId"];
        if (cookie != null)
            cookie.SameSite = SameSiteCookieUtils.GetSameSiteMode(Request.UserAgent, SameSiteMode.None);
    }
    
    // Ref https://learn.microsoft.com/en-us/aspnet/core/security/samesite?view=aspnetcore-3.1#supporting-older-browsers
    public static class SameSiteCookieUtils
    {
        /// <summary>
        /// -1 defines the unspecified value, which tells ASPNET to not send the SameSite attribute
        /// </summary>
        public const SameSiteMode Unspecified = (SameSiteMode) (-1);
    
        public static SameSiteMode GetSameSiteMode(string userAgent, SameSiteMode mode)
        {
            if (string.IsNullOrWhiteSpace(userAgent))
                return mode;
    
            if (mode == SameSiteMode.None && DisallowsSameSiteNone(userAgent))
                return Unspecified;
    
            return mode;
        }
    
        public static bool DisallowsSameSiteNone(string userAgent)
        {
            // Cover all iOS based browsers here. This includes:
            // - Safari on iOS 12 for iPhone, iPod Touch, iPad
            // - WkWebview on iOS 12 for iPhone, iPod Touch, iPad
            // - Chrome on iOS 12 for iPhone, iPod Touch, iPad
            // All of which are broken by SameSite=None, because they use the iOS networking
            // stack.
            if (userAgent.Contains("CPU iPhone OS 12") ||
                userAgent.Contains("iPad; CPU OS 12"))
            {
                return true;
            }
    
            // Cover Mac OS X based browsers that use the Mac OS networking stack.
            // This includes:
            // - Safari on Mac OS X.
            // This does not include:
            // - Chrome on Mac OS X
            // Because they do not use the Mac OS networking stack.
            if (userAgent.Contains("Macintosh; Intel Mac OS X 10_14") &&
                userAgent.Contains("Version/") && userAgent.Contains("Safari"))
            {
                return true;
            }
    
            // Cover Chrome 50-69, because some versions are broken by SameSite=None,
            // and none in this range require it.
            // Note: this covers some pre-Chromium Edge versions,
            // but pre-Chromium Edge does not require SameSite=None.
            if (userAgent.Contains("Chrome/5") || userAgent.Contains("Chrome/6"))
            {
                return true;
            }
    
            return false;
        }
    }
    

    With the DisallowsSameSiteNone logic coming from the Microsoft Docs https://learn.microsoft.com/en-us/aspnet/core/security/samesite?view=aspnetcore-3.1#supporting-older-browsers

    I also set the following in my web.config

    <httpCookies httpOnlyCookies="true" requireSSL="true" />
    

    Hopefully some of that is useful to you