WSFederation ADFS登录循环.Net Core 2.1

问题描述 投票:1回答:1

登录到ADFS(内部部署)时,我输入凭据后将被重定向,最终因错误Exception details(错误详细信息)而出错:Microsoft.IdentityServer.Web.InvalidRequestException:MSIS7042:同一客户端浏览器会话已将“在最后的“ 7”秒内请求6”。请与管理员联系,以获取事件查看器中的详细信息。我遵循了几个堆栈溢出建议(link),但无法解决问题。我在https上运行,并确保证书正确。

这是我的代码

namespace TestApp
{
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }
    private static string HtmlEncode(string content) =>
        string.IsNullOrEmpty(content) ? string.Empty : HtmlEncoder.Default.Encode(content);

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {

        services.Configure<CookiePolicyOptions>(options =>
        {
            options.CheckConsentNeeded = context => false;
            options.MinimumSameSitePolicy = SameSiteMode.None;
            options.Secure = CookieSecurePolicy.SameAsRequest;
        });

        services.AddAuthentication(sharedOptions =>
        {
            sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            sharedOptions.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            sharedOptions.DefaultChallengeScheme = WsFederationDefaults.AuthenticationScheme;
        })
            .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme,
                options =>
                {
                    options.Cookie.Name = ".AspNet.SharedCookie";
                    options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest;
                    options.Cookie.SameSite = SameSiteMode.None;
                })
            .AddWsFederation(options =>
            {
                options.MetadataAddress =
                    $"https://adfsdomain/FederationMetadata/2007-06/FederationMetadata.xml";
                options.Wtrealm = "urn:apptest";
                options.Wreply = "https://appdomain/apptest";
                options.CallbackPath = "/apptest";
                options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
                {
                    ValidateIssuer = true,
                    ValidIssuer = "https://adfsdomain/adfs/services/trust/"
                };
                options.SaveTokens = true;
                options.RequireHttpsMetadata = false;
            });

        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();


        services.AddMvc();//.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);


    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }
        app.UseCors(policy => policy.SetIsOriginAllowed(origin => origin == "https://adfsdomain"));
        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.Use(async (context, next) =>
        {
            if (!context.User.Identity.IsAuthenticated)
            {
                await context.ChallengeAsync(WsFederationDefaults.AuthenticationScheme);
            }
            else
            {
                await next();
            }
        });

        app.UseCookiePolicy();
        app.UseAuthentication();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

}

我正在使用.Net Core 2.1和ADFS 3.0。如果我使用ASP.NET MVC应用程序并使用相同的adfs进行发布,则我的代码将像charm一样工作,这表明ADFS上的配置已正确配置。

redirect .net-core ws-federation adfs3.0
1个回答
0
投票
不确定这是否可以帮助您,但是我让我的ADFS可以与以下人员一起工作:

services.AddAuthentication(sharedOptions => { sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; sharedOptions.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; sharedOptions.DefaultChallengeScheme = WsFederationDefaults.AuthenticationScheme; }) .AddWsFederation(options => { options.Wtrealm = this._Configuration["wsfed:realm"]; options.MetadataAddress = string.Format("{0}/federationmetadata/2007-06/federationmetadata.xml", this._Configuration["wsfed:metadata"]); }) .AddCookie();

© www.soinside.com 2019 - 2024. All rights reserved.