目前我正在使用 Identity server4 开发一个 POC,如果 Windows 身份验证失败,我必须显示我自己的登录页面(在这种情况下,我只想显示我自己的登录页面并避免浏览器登录弹出窗口。
我的问题是在代码中注入我自己的登录页面?应用程序如何知道 Windows 身份验证失败?如果您检查下面的代码,首先请求 AuthenticateAsync 始终返回 null,然后它从 else 块调用 Challenge,要求浏览器发送 Kerberos 令牌 我们实现了 SSO,但现在我想在 SSO 失败时显示我自己的登录页面。 我的场景与this
完全相同有人知道如何实现这一目标吗?
private async Task<IActionResult> ProcessWindowsLoginAsync(string returnUrl)
{
// see if windows auth has already been requested and succeeded.
var result = await HttpContext.AuthenticateAsync(_windowsAuthConfig.WindowsAuthenticationProviderName);
if (result?.Principal is WindowsPrincipal wp)
{
var props = new AuthenticationProperties
{
RedirectUri = Url.Action("Callback"),
Items =
{
{ "returnUrl", returnUrl},
{ "scheme", _windowsAuthConfig.WindowsAuthenticationProviderName}
}
};
var id = new ClaimsIdentity(_windowsAuthConfig.WindowsAuthenticationProviderName);
var claims = await _userStore.GetClaimsForWindowsLoginAsync(wp);
id.AddClaims(claims);
_logger.LogDebug("Signing in user with windows authentication.");
await HttpContext.SignInAsync(IdentityServerConstants.ExternalCookieAuthenticationScheme,new ClaimsPrincipal(id),props);
return Redirect(props.RedirectUri);
}
else
{
_logger.LogDebug("Re-triggered windows authentication using ChallengeResult.");
// Trigger windows auth
// since windows auth don't support the redirect uri,
// this URL is re-triggered when we call challenge
return Challenge(_windowsAuthConfig.WindowsAuthenticationSchemes);
}
}
诀窍在于将挑战与视图一起返回。所以需要手动设置StatusCode和Headers。所以(假设asp net core mvc)而不是:
return Challenge("Windows");
这样做:
var result = View(nameof(MyFallbackFailViewName), model);
result.StatusCode = 401;
Response.Headers.WWWAuthenticate.Append("Negotiate");
Response.Headers.WWWAuthenticate.Append("NTLM");
return result;