IdentityServer4 - 注销后重定向到 MVC 客户端

标签 identityserver4

我正在使用 IdenetityServer4 并在注销后重定向到 MVC 客户端不起作用。以下是我的 MVC 客户端 Controller 注销操作:

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

以下是身份服务器 4 主机配置文件。
public static IEnumerable<Client> GetClients()
{
    return new List<Client>
    {
        // other clients omitted...

        // OpenID Connect implicit flow client (MVC)
        new Client
        {
            ClientId = "mvc",
            ClientName = "MVC Client",
            AllowedGrantTypes = GrantTypes.Implicit,

            // where to redirect to after login
            RedirectUris = { "http://localhost:58422/signin-oidc" },

            // where to redirect to after logout
            PostLogoutRedirectUris = { "http://localhost:58422/signout-callback-oidc" },

            AllowedScopes = new List<string>
            {
                IdentityServerConstants.StandardScopes.OpenId,
                IdentityServerConstants.StandardScopes.Profile
            }
        }
    };
} 

我希望用户在从 IdentityServer 注销后重定向回 MVC 客户端。现在用户必须单击下图中的链接显示以重定向回 MVC 站点,但我认为用户应该自动重定向回 MVC 客户端。

enter image description here

最佳答案

如果有人在使用 Scaffolding(他们使用 Razor Page 文件),这里是根据 Akhilesh 的回答修复它的方法:

在 Areas\Identity\Pages\Account\Logout.cshtml 中:

首先,添加IIdentityServerInteractionService服务:

    IIdentityServerInteractionService _interaction;

    public LogoutModel(SignInManager<IdentityUser> signInManager, ILogger<LogoutModel> logger, IIdentityServerInteractionService _interaction)
    {
        _signInManager = signInManager;
        _logger = logger;
        this._interaction = _interaction;
    }

您可能需要添加对 OnGet() 的支持,逻辑可能会有所不同,具体取决于您的情况,在我的情况下,获取或发布无关紧要:
    public async Task<IActionResult> OnGet(string returnUrl = null)
    {
        return await this.OnPost(returnUrl);
    }

在 OnPost 中添加 LogoutId 逻辑:
    public async Task<IActionResult> OnPost(string returnUrl = null)
    {
        await _signInManager.SignOutAsync();
        _logger.LogInformation("User logged out.");

        var logoutId = this.Request.Query["logoutId"].ToString();

        if (returnUrl != null)
        {
            return LocalRedirect(returnUrl);
        }
        else if (!string.IsNullOrEmpty(logoutId))
        {
            var logoutContext = await this._interaction.GetLogoutContextAsync(logoutId);
            returnUrl = logoutContext.PostLogoutRedirectUri;

            if (!string.IsNullOrEmpty(returnUrl))
            {
                return this.Redirect(returnUrl);
            }
            else
            {
                return Page();
            }
        }
        else
        {
            return Page();
        }
    }

关于IdentityServer4 - 注销后重定向到 MVC 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43259870/

相关文章:

asp.net-core - 带有 IdentityServer4 的 appsettings.json 中的 ClientSecret

identityserver3 - IdentityServer4 IdentityServer3.AccessTokenValidation

c# - 使用 Google 身份验证 (SPA) 中的 IdentityServer 获取姓名和电子邮件声明

android - 如何将 AppAuth-Android 与 IdentityServer4 一起使用?

c# - Identity Server 4 与身份相关的范围请求,但没有 openid 范围

c# - 无法使用HttpClient获取API资源

c# - Microsoft Identity 和 Identity Server 4 流程关系

xamarin.forms - OidcClient2 - 在等待 LoginAsync 时关闭 IBrowser

angular - 身份服务器 4 : force user to login with provider after he closes the browser/tab

.net-core - 是否有一个示例说明如何创建使用 IdentityServer4 进行身份验证的 dotnet core 3 控制台应用程序?