AspNetCore 2.1 中使用 WsFederation 时出现注销 (LogOut) 错误

par*_*rag 2 .net c# ws-federation asp.net-core

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

没有为“联合”方案注册注销身份验证处理程序。注册的注销方案有:WsFederation、Cookies。您是否忘记调用 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();

}
Run Code Online (Sandbox Code Playgroud)

这是 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);
    }
Run Code Online (Sandbox Code Playgroud)

Tao*_*hou 5

如错误所示,您已注册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;
    })
Run Code Online (Sandbox Code Playgroud)

但是,您已退出,WsFederationAuthenticationDefaults.AuthenticationTypeFederation。您应该注销WsFederationDefaults.AuthenticationScheme而不是WsFederationAuthenticationDefaults.AuthenticationType.

尝试下面的代码:

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