我正在使用 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 客户端。
最佳答案
如果有人在使用 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/