繁体   English   中英

身份服务器 4:从 MVC 客户端正确注销

[英]Identity Server 4 : Proper logout from MVC Client

我在 IdentityServer 4 中的注销功能遇到了麻烦。我的 IS4 应用程序主要是他们网站上教程的结果,所以他们并不是真正的自定义行为。 我也使用 ASP.net Core Identity。 我有一个 MVC 客户端(同样,基本上是项目模板)。 我刚刚在索引页面的顶部添加了一个“注销”按钮,以便将当前经过身份验证的用户注销。

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

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

这正是教程所说的。

这是 MVC Client 的 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 还很陌生,所以非常欢迎任何帮助......谢谢!

这是我解决这个问题的方法:

    public IActionResult LogOff()
    {
        return new SignOutResult(new[] { "oidc", "Cookies" });
    }

最好不要使用魔法字符串,但是:

  return new SignOutResult(new[]
            {
                CookieAuthenticationDefaults.AuthenticationScheme, 
                OpenIdConnectDefaults.AuthenticationScheme
            });

你有没有试过,

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM