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

问题描述 投票:11回答:2

在Logout不工作后,我正在使用IdenetityServer4并重定向到MVC客户端。以下是我的MVC客户端控制器注销操作:

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

identityserver4
2个回答
20
投票

您的Config.cs或MVC控制器没有问题。

转到IdentityServer4应用程序然后在AccountController的Logout [HttpPost]方法中,执行以下更改:

public async Task<IActionResult> Logout(LogoutViewModel model)
{
   ...    
  //return View("LoggedOut", vm);
  return Redirect(vm.PostLogoutRedirectUri);
}

这会将用户重定向回MVC应用程序(在您的情况下)。

有一种更好的方法:您可以从AccountOptions.cs设置这些选项,如下所示:

public static bool ShowLogoutPrompt = false;
public static bool AutomaticRedirectAfterSignOut = true;

5
投票

如果有人使用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()的支持,逻辑可能会有所不同取决于您的情况,在我的情况下,Get或Post无关紧要:

    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();
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.