Identity Server 4:MVC客户端的正确登出

5
我在IdentityServer 4的注销功能上遇到了麻烦。 我的IS4应用主要是根据他们网站上的教程创建的,所以没有真正的自定义行为。我也使用了ASP.net Core Identity。 我有一个MVC客户端(同样是基于项目模板)。我只是在首页顶部添加了一个“注销”按钮,以便注销当前已验证的用户。

这是我MVC客户端中的注销方法:

public async Task Logout()
{
    await HttpContext.SignOutAsync("Cookies");
    await HttpContext.SignOutAsync("oidc");
}

所以,准确地说,教程中的内容如下:

这是MVC客户端的Startup.cs配置:

services.AddAuthentication(options =>
{
    options.DefaultScheme = "Cookies";
    options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options =>
{
    options.SignInScheme = "Cookies";

    options.Authority = "http://localhost:5000";
    options.RequireHttpsMetadata = false;
    options.CallbackPath = new PathString("/Home/");

    options.ClientId = "Core.WebUI";
    options.ClientSecret = "secret";
    options.ResponseType = "code id_token";

    options.SaveTokens = true;
    options.GetClaimsFromUserInfoEndpoint = true;

    options.Scope.Add("offline_access");                    
});

没有花哨的东西... 现在是IS4应用程序中的MVC客户端配置:

new Client
{
    ClientId = "Core.WebUI",
    ClientName = "MVC Client",
    ClientSecrets = new List<Secret>
    {
        new Secret("secret".Sha256())
    },
    AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,
    RequireConsent = false,

    // where to redirect to after login
    RedirectUris = { "http://localhost:5011/Home/" },

    // where to redirect to after logout
    PostLogoutRedirectUris = { "http://localhost:5011/Home/" },
    AlwaysSendClientClaims = true,
    AlwaysIncludeUserClaimsInIdToken = true,
    AllowedScopes =
    {
        IdentityServerConstants.StandardScopes.OpenId,
        IdentityServerConstants.StandardScopes.Profile
    },
    AllowOfflineAccess = true
}

再次强调教程中的内容。我的问题是:当用户登录后,我点击注销按钮,被重定向到IS4应用程序的注销页面,显示我已经注销。但实际上并没有注销,因为如果我回到我的MVC,我仍然可以访问受保护的功能(带有Authorize属性)。为了正确地注销我的用户,一旦我在D4应用程序的注销页面中,我必须点击IS4应用程序的注销按钮……只有这样我才能正确注销...

我想要的是,当我在我的MVC客户端上点击注销按钮时,我确实被注销,并直接重定向到我的MVC客户端的主页(不显示“您已注销”页面)

我对IS4和ADP.NET都很新,所以任何帮助都非常欢迎...谢谢!


你解决了吗?我遇到了完全相同的行为。 - jpmir
不幸的是,我正在工作的项目发生了重大变化,我们从ASP.NET Core MVC客户端切换到了Angular客户端...因此,这个问题不再适用了... - Flo Dupuy
3个回答

2
这是我解决问题的方法:
    public IActionResult LogOff()
    {
        return new SignOutResult(new[] { "oidc", "Cookies" });
    }

1
最好不要使用魔法字符串,而是:
  return new SignOutResult(new[]
            {
                CookieAuthenticationDefaults.AuthenticationScheme, 
                OpenIdConnectDefaults.AuthenticationScheme
            });

0

你试过了吗?

public async Task<IActionResult> Logout()
{
   await _signInManager.SignOutAsync();
   return View("Logout"); // or whatever url Redirect("http://localhost:5011/Home/")
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接