我正在使用IdenetityServer4并在Logout无法正常工作后重定向到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客户端。
答案 0 :(得分: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;
答案 1 :(得分:5)
如果有人使用了脚手架(他们使用了Razor Page文件),则可以根据Akhilesh的答案来解决此问题:
在Areas \ Identity \ Pages \ Account \ Logout.cshtml中:
首先,添加df %>%
group_by(grp = cumsum(ifelse(is.na(as.numeric(lag(df$b) != df$b)), 0, as.numeric(lag(df$b) != df$b)))) %>%
mutate(
a = ifelse(n() > 1, paste0("grp", b), a),
b = sum(b)
) %>% ungroup() %>% distinct(a, b)
服务:
IIdentityServerInteractionService
您可能需要添加对 IIdentityServerInteractionService _interaction;
public LogoutModel(SignInManager<IdentityUser> signInManager, ILogger<LogoutModel> logger, IIdentityServerInteractionService _interaction)
{
_signInManager = signInManager;
_logger = logger;
this._interaction = _interaction;
}
的支持,逻辑可能因情况而异,在我的情况下,Get或Post无关紧要:
OnGet()
在OnPost中添加LogoutId逻辑:
public async Task<IActionResult> OnGet(string returnUrl = null)
{
return await this.OnPost(returnUrl);
}
答案 2 :(得分:0)
不需要额外的代码。您应该确保wwwroot / js和LoggedOut.cshtml中是否存在Model.AutomaticRedirectAfterSignOut = true和signout-redirect.js
@if (Model.AutomaticRedirectAfterSignOut)
{
<script src="~/js/signout-redirect.js"></script>
}
使所有工作(请参见下面的代码)
window.addEventListener("load", function () {
var a = document.querySelector("a.PostLogoutRedirectUri");
if (a) {
window.location = a.href;
}
});
因此,用户从LoggedOut.cshtml重定向到mvc