ASP .NET Core 2.0-OpenId Connect身份验证:关联错误

时间:2018-07-03 19:15:31

标签: c# asp.net-core oauth-2.0 asp.net-core-2.0 pingfederate

我正在尝试在ASP.NET Core 2.0 Web应用程序上创建身份验证。

我的公司正在使用Ping Federate,我试图通过公司登录页面对用户进行身份验证,并使用我的签名密钥(此处为{X509SecurityKey)来验证返回的令牌。

ping登录链接链接如下:

https://auth.companyname.com

我将Startup.cs配置为能够登录并挑战该站点。

我用[Authorize(Policy="Mvc")]装饰了HomeController。

我能够访问登录页面,但是,每当我从登录页面返回时,我都会得到( 我尝试关闭/启用多个多重验证):

  

异常:相关性失败。

     

未知位置

     

异常:处理远程登录时遇到错误。

     

Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler.HandleRequestAsync()

错误消息不是很有帮助...以前有人遇到过这样的问题吗?

    public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();

    services.AddAuthentication(options =>
    {
        options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
    })
    .AddCookie()
    .AddOpenIdConnect(options =>
    {
        options.Authority = PF_LOGINPATH;
        options.ClientId = Configuration["ClientId"];
        options.ClientSecret = Configuration["ClientSecret"];
        options.Scope.Clear();

        options.ResponseType = OpenIdConnectResponseType.CodeIdTokenToken;
        options.SaveTokens = false;

        options.GetClaimsFromUserInfoEndpoint = false;//true;

        options.TokenValidationParameters = new TokenValidationParameters
        {
            RequireSignedTokens =  false,
            ValidateActor = false,
            ValidateAudience = false,
            ValidateIssuer = false,
            ValidateIssuerSigningKey = false,
            ValidateTokenReplay = false,

            // Compensate server drift
            ClockSkew = TimeSpan.FromHours(24),
            //ValidIssuer = PF_LOGINPATH;
            // Ensure key
            IssuerSigningKey = CERTIFICATE,                    

            // Ensure expiry
            RequireExpirationTime = false,//true,
            ValidateLifetime = false,//true,                    

            // Save token
            SaveSigninToken = false
        };                

    });

    services.AddAuthorization(options =>
    {
        options.AddPolicy("Mvc", policy =>
        {
            policy.AuthenticationSchemes.Add(OpenIdConnectDefaults.AuthenticationScheme);
            policy.RequireAuthenticatedUser();
        });
    });
}

// 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.UseBrowserLink();
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }

    app.UseStaticFiles();

    app.UseAuthentication();

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

2 个答案:

答案 0 :(得分:0)

我也有类似情况。我的应用程序网址是这样的:“ https://domain/appname” 因此,当有人输入网址“ https://domain/appname/”(带有斜杠)时,会出现“相关”错误。这就是我解决它的方法(从其他网站找到)

public void ConfigureServices(IServiceCollection services)
        {
            services.AddAuthentication(options =>
                {
                    //Auth schemes here
                })
                .AddOpenIdConnect(oid =>
                {
                    //Other config here
                    oid.Events = new OpenIdConnectEvents()
                    {
                        OnRemoteFailure = OnRemoteFailure

                    };
                });
        }

private Task OnRemoteFailure(RemoteFailureContext context)
        {

            if (context.Failure.Message.Contains("Correlation failed"))
            {
                context.Response.Redirect("/AppName"); // redirect without trailing slash
                context.HandleResponse();
            }

            return Task.CompletedTask;
        }

答案 1 :(得分:0)

以斜杠结束回调网址