带有WsFederation的AspNetCore 2.1中的注销(注销)错误

时间:2018-10-25 15:13:31

标签: c# .net asp.net-core ws-federation

在ASP .NET Core 2.1应用程序中注销(注销)时出现以下错误

  

未为方案“联合身份”注册任何注销身份验证处理程序。注册的注销方案是:WsFederation,Cookie。您忘了打电话给AddAuthentication()。AddCookies(“ Federation”,...)

这是我的Startup.cs中的代码段

public void ConfigureServices(IServiceCollection services)
{
    services.AddAuthentication(sharedOptions =>
        {
            sharedOptions.DefaultScheme =
                    CookieAuthenticationDefaults.AuthenticationScheme;
            sharedOptions.DefaultSignInScheme = 
                    CookieAuthenticationDefaults.AuthenticationScheme;
            sharedOptions.DefaultChallengeScheme = 
                    WsFederationDefaults.AuthenticationScheme;
        })
        .AddWsFederation(options =>
        {
            options.Wtrealm = this._wtrealm;
            options.MetadataAddress = this._metadataAddress;
        })
        .AddCookie();

}

这是SignOut方法中的代码

    public IActionResult SignOut()      
    {
        foreach (var key in this.HttpContext.Request.Cookies.Keys)
        {
            this.HttpContext.Response.Cookies.Delete(key);

            // this.HttpContext.Response.Cookies.Append(key, 
            //                       string.Empty, 
            //                       new CookieOptions() { 
            //                             Expires = DateTime.Now.AddDays(-1) 
            //                       });
        }

        return this.SignOut(
             new  Microsoft.AspNetCore.Authentication.AuthenticationProperties 
             {
                  RedirectUri = this.GetReturnUrl() 
             },
             CookieAuthenticationDefaults.AuthenticationScheme,
             WsFederationAuthenticationDefaults.AuthenticationType);
    }

1 个答案:

答案 0 :(得分:3)

如错误所示,您使用以下代码注册了WsFederationCookies

services.AddAuthentication(sharedOptions =>
    {
        sharedOptions.DefaultScheme =
                CookieAuthenticationDefaults.AuthenticationScheme;
        sharedOptions.DefaultSignInScheme = 
                CookieAuthenticationDefaults.AuthenticationScheme;
        sharedOptions.DefaultChallengeScheme = 
                WsFederationDefaults.AuthenticationScheme;
    })
    .AddWsFederation(options =>
    {
        options.Wtrealm = this._wtrealm;
        options.MetadataAddress = this._metadataAddress;
    })

但是,您正在登出WsFederationAuthenticationDefaults.AuthenticationType的{​​{1}}。您应该登出Federation而不是WsFederationDefaults.AuthenticationScheme

请尝试以下代码:

WsFederationAuthenticationDefaults.AuthenticationType