使用 WsFederation 在 AspNetCore 2.1 中注销(注销)错误

SignOut (LogOut) Error in AspNetCore 2.1 with WsFederation

我在 ASP .NET Core 2.1 应用程序

中注销(注销)时遇到以下错误

No sign-out authentication handler is registered for the scheme 'Federation'. The registered sign-out schemes are: WsFederation, Cookies. Did you forget to call 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);
    }

如错误所示,您使用以下代码注册了 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.AuthenticationTypeFederation。您应该注销 WsFederationDefaults.AuthenticationScheme 而不是 WsFederationAuthenticationDefaults.AuthenticationType

试试下面的代码:

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