• Asp.Net Core 部署发布后EFCore数据库断连和Google浏览器无法存储Cookie


    Asp.net Core 网站部署之后遇到一下问题。

    1. 第一次打开网页登录时查询数据库操作断连。

    2. 谷歌浏览器无法正常存储Cookie。

    问题解决:

    问题一:

    前提条件:

    Nuget包引用说明:Pomelo.EntityFrameworkCore.MySql

    步骤:

    将ConfigureServices方法中使用数据库断连重试机制。

    //注册数据库连接
    services.AddDbContext<ServerDBContext>(options => options.UseMySql(Configuration.GetConnectionString("DefaultConnection"),builder=> {
             builder.EnableRetryOnFailure();
    }));

    问题二:

    据说原因:非Chrome80+浏览器不识别Cookie上的SameSite=none属性值,导致认证Cookie在后续请求中被抛弃。

    步骤1:

    在Configure方法中

    // 表示ASP.NET Core 启动Cookie策略
    app.UseCookiePolicy();

    步骤2:

    在ConfigureService中,添加cookie的策略配置代码;

    services.Configure<CookiePolicyOptions>(options =>
    {
            options.MinimumSameSitePolicy = (SameSiteMode)(-1);
            options.OnAppendCookie = cookieContext =>
                CheckSameSite(cookieContext.Context, cookieContext.CookieOptions);
            options.OnDeleteCookie = cookieContext =>
                CheckSameSite(cookieContext.Context, cookieContext.CookieOptions);
    });

    步骤3:

    Startup类添加CheckSameSite方法

    private void CheckSameSite(HttpContext httpContext, CookieOptions options)
    {
        if (options.SameSite == SameSiteMode.None)
        {
            var userAgent = httpContext.Request.Headers["User-Agent"].ToString();
            if (DisallowsSameSite.DisallowsSameSiteNone(userAgent)) 
         {
            options.SameSite
    = SameSiteMode.Unspecified;
        }
      }
    }

    步骤4:

    创建DisallowsSameSite.DisallowsSameSiteNone静态方法

     public  class DisallowsSameSite
        {
            public static bool DisallowsSameSiteNone(string userAgent)
            {
                // Check if a null or empty string has been passed in, since this
                // will cause further interrogation of the useragent to fail.
                if (String.IsNullOrWhiteSpace(userAgent))
                    return false;
    
                // 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;
            }
        }

    引用:https://www.cnblogs.com/JulianHuang/p/12596115.html

  • 相关阅读:
    前端菜鸡之路——聊天室2.0
    AngularJS入门——hello world!
    前端菜鸡之路——网页上的图标
    可拖动的消息框
    node.js之socket.io模块
    yahoo军规的思考
    如何用jquery实现点击后跳到页面指定位置
    CSS多列布局
    Node.js下Mysql数据库连接
    ABAP 程序报 unicode 错误
  • 原文地址:https://www.cnblogs.com/TBW-Superhero/p/13725573.html
Copyright © 2020-2023  润新知