zoukankan      html  css  js  c++  java
  • identity server4 在接入时遇到错误:Exception: Correlation failed. Unknown location的解决方法

    背景:前面尝试了用https的方式解决,但是需要有域名申请的证书,否则被他保护的api被别人调用时报错(没找到解决方法)

    尝试:放弃https的方式,还用原来的http的方式,解决Correlation failed. Unknown location的问题,找到这个文章,asp.net core - OIDC correlation failed in Microsoft Teams authentication popup (no problems in browser) - Stack Overflow

    用下面的代码搞定:

    private void CheckSameSite(HttpContext httpContext, CookieOptions options)
    {
        if (options.SameSite == SameSiteMode.None)
        {
            var userAgent = httpContext.Request.Headers["User-Agent"].ToString();
            // TODO: Use your User Agent library of choice here.
            if (/* UserAgent doesn’t support new behavior */)
            {
                   // For .NET Core < 3.1 set SameSite = (SameSiteMode)(-1)
                   options.SameSite = SameSiteMode.Unspecified;
             }
        }
    }
    
    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            options.MinimumSameSitePolicy = SameSiteMode.Unspecified;
            options.OnAppendCookie = cookieContext => 
                CheckSameSite(cookieContext.Context, cookieContext.CookieOptions);
            options.OnDeleteCookie = cookieContext => 
                CheckSameSite(cookieContext.Context, cookieContext.CookieOptions);
        });
    }
    
    public void Configure(IApplicationBuilder app)
    {
        app.UseCookiePolicy(); // Before UseAuthentication or anything else that writes cookies.
        app.UseAuthentication();
        //
    }

    我的代码记录:

    public class Startup
        {
            public static IContainer AutofacContainer;
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
            }
    
            public IConfiguration Configuration { get; }
            private void CheckSameSite(HttpContext httpContext, CookieOptions options)
            {
                if (options.SameSite == Microsoft.AspNetCore.Http.SameSiteMode.None)
                {
                    var userAgent = httpContext.Request.Headers["User-Agent"].ToString();
                    // TODO: Use your User Agent library of choice here.
                    //if (/* UserAgent doesn’t support new behavior */)
                    //{
                    // For .NET Core < 3.1 set SameSite = (SameSiteMode)(-1)
                    options.SameSite = Microsoft.AspNetCore.Http.SameSiteMode.Lax;
                    //}
                }
            }
    
            // This method gets called by the runtime. Use this method to add services to the container.
            public IServiceProvider ConfigureServices(IServiceCollection services)
            {
                services.AddControllersWithViews();
                services.AddMvc().AddRazorRuntimeCompilation();
    
                JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
    
                services.AddAuthentication(options =>
                {
                    options.DefaultScheme = "Cookies";
                    options.DefaultChallengeScheme = "oidc";
                })
                .AddCookie(options =>
                {
                    options.Cookie.Name = "Cookies";
                })
                .AddOpenIdConnect("oidc", options =>
                {
                    options.SignInScheme = "Cookies";
                    options.Authority = "http://localhost:5000";
                    options.RequireHttpsMetadata = false;
                    options.ClientId = "localMvcCore";
                    options.ClientSecret = "121212";
                    options.ResponseType = "code id_token";
                    options.Scope.Clear();
                    options.Scope.Add("openid");
                    options.Scope.Add("sid");
                    options.Scope.Add("profile");
                    options.Scope.Add("AuthorizationAPI");
                    options.SaveTokens = true;
                });
    
    
    
                services.Configure<CookiePolicyOptions>(options =>
                {
                    options.MinimumSameSitePolicy = Microsoft.AspNetCore.Http.SameSiteMode.Unspecified;
                    options.OnAppendCookie = cookieContext =>
                        CheckSameSite(cookieContext.Context, cookieContext.CookieOptions);
                    options.OnDeleteCookie = cookieContext =>
                        CheckSameSite(cookieContext.Context, cookieContext.CookieOptions);
                });
    
                ContainerBuilder builder = new ContainerBuilder();
                //将services中的服务填充到Autofac中.
                builder.Populate(services);
                //新模块组件注册
                builder.RegisterModule<DefaultModuleRegister>();
                //创建容器.
                AutofacContainer = builder.Build();
                //使用容器创建 AutofacServiceProvider 
                return new AutofacServiceProvider(AutofacContainer);
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    app.UseExceptionHandler("/Home/Error");
                    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                    app.UseHsts();
                }
    
                app.UseCookiePolicy();
                app.UseStaticFiles();
    
                app.UseRouting();
    
                app.UseAuthentication();
                app.UseAuthorization();
    
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllerRoute(
                        name: "default",
                        pattern: "{controller=Home}/{action=Index}/{id?}");
                });
            }
        }
  • 相关阅读:
    你好,2021!
    庚子走,辛丑来,愿一切安好!
    花魂鸟魂总难留,鸟自无言花自羞
    熟悉的小胡同
    夜半听鼾声
    写在儿子22岁生日
    vue配置Azure Artifacts npm包源
    RabbitMQ出错:AMQP close-reason, initiated by Peer, code=530的解决办法
    .NET MVC存储图片到数据库的解决方案
    EF Core解决报错’EntityTypeBuilder未包含“ToTable”的定义‘的方法
  • 原文地址:https://www.cnblogs.com/wjx-blog/p/14803501.html
Copyright © 2011-2022 走看看