成功登录Azure Ad b2c后关联失败

时间:2019-05-06 07:26:15

标签: .net asp.net-core

我们有一个基于cookie的身份验证设置,用于处理用户。我们正在Service Fabric上使用ASP.NET Core 2.1。

我们已经进行了一些设置,以使Cookie身份验证方案使用AzureAD“回退”到OpenID身份验证。

我们能够成功加载azure ad b2c登录,但是登录后我们遇到了Correlation Failed问题。我们该如何解决?

 public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.AddCors();
        services.AddAuthentication(sharedOptions =>
        {
            sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
            sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
        })
        .AddAzureAdB2C(options => Configuration.Bind("Authentication:AzureAdB2C", options))
        .AddCookie(options =>
        {
            options.ExpireTimeSpan = TimeSpan.FromMinutes(600);
            options.SlidingExpiration = true;
            options.Cookie.HttpOnly = false;
            options.Cookie.SecurePolicy = Microsoft.AspNetCore.Http.CookieSecurePolicy.SameAsRequest;
        });
        services.AddMvc();
        services.AddDistributedMemoryCache();
        services.AddSession(options =>
        {
            options.IdleTimeout = TimeSpan.FromHours(1);
            options.Cookie.HttpOnly = true;
            options.Cookie.IsEssential = true;
        });

        Utility.Caching.CachingConfig.ConfigureCache(services, hostingEnvironment);
        Utility.Swagger.SwaggerConfig.ConfigureService(services, SwaggerApiVersion, SwaggerApiTitle,
            Path.Combine(AppContext.BaseDirectory, $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"));
    }

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        var forwardedHeaderOptions = new ForwardedHeadersOptions
        {
            ForwardedHeaders =
           Microsoft.AspNetCore.HttpOverrides.ForwardedHeaders.XForwardedHost |
           Microsoft.AspNetCore.HttpOverrides.ForwardedHeaders.XForwardedFor |
           Microsoft.AspNetCore.HttpOverrides.ForwardedHeaders.XForwardedProto
        };
        //SenseiLogger.Current.LogInformational(1, "KnownNetworks = " + String.Join<string>(", ", forwardedHeaderOptions.KnownNetworks.Select<Microsoft.AspNetCore.HttpOverrides.IPNetwork, string>(ipnet => ipnet.ToString())));
       // SenseiLogger.Current.LogInformational(1, "KnownProxies = " + String.Join<IPAddress>(", ", forwardedHeaderOptions.KnownProxies));

        // required for XForwardedHost/For to take effect; also seems to interfere with
        // XForwardedProto and caused redirect_uri to use http: instead of https:
        forwardedHeaderOptions.KnownProxies.Clear();
        forwardedHeaderOptions.KnownNetworks.Clear();

        app.UseForwardedHeaders(forwardedHeaderOptions);

        var config = new ConfigurationBuilder().AddJsonFile("sharedappsettings.json").Build();
        Tracking.SetConfiguration();
        app.Use((context, next) =>
        {
            context.Request.Scheme = config["RequestScheme"];
            return next();
        });
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }
        app.UseStaticFiles();
        app.UseCors(builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials());
        app.UseSession();
        app.UseAuthentication();
        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
        Utility.Swagger.SwaggerConfig.ConfigureApplication(app, SwaggerApiVersion, SwaggerApiTitle);

        Tracking.TrackEvent(Assembly.GetEntryAssembly().GetName().Name + ".Started");
    }  

0 个答案:

没有答案