【发布时间】:2021-04-06 21:00:56
【问题描述】:
我正在尝试使用 Windows 身份验证凭据将我的本机(Winforms、控制台应用程序)客户端连接到托管在 IIS 上的 Identity Server。重点是让用户通过 AD 进行身份验证,并使用这些凭据从 Identity Server(通过商业 https://commercial.abp.io/ 平台运行)获得正确的声明和角色。
编辑: 我发现这不是与客户端相关的问题,因为我什至无法直接在托管站点上使用我的外部登录(Windows 凭据)。
这个东西在由 IISExpress 托管时在本地工作,然后我将它发布到 IIS 并在 IIS 设置中启用匿名和 Windows 身份验证,这就是问题开始的地方。
当我运行它并单击外部登录(Windows 凭据)按钮时,我通常会重定向到 https://myserver/Error?httpStatusCode=401 并且我会提示我的 Windows 凭据(即使我正确插入,只需再次重复提示)。
我有时会使用我的 Windows 凭据登录(这是目标)。使用用户名和密码登录可以正常工作。
我在这里看到有人提到的类似问题: https://github.com/IdentityServer/IdentityServer4/issues/4937 没有任何解决方案\答案。
我的客户端基本上是来自 https://github.com/damienbod/AspNetCoreWindowsAuth 的示例 NativeConsolePKCEClient
static string _authority = "https://myserver/";
string redirectUri = "https://127.0.0.1:45656";
var options = new OidcClientOptions
{
Authority = _authority,
ClientId = "native.code",
ClientSecret = "secret",
RedirectUri = redirectUri,
Scope = "openid profile",
FilterClaims = false,
Browser = browser,
Flow = OidcClientOptions.AuthenticationFlow.AuthorizationCode,
ResponseMode = OidcClientOptions.AuthorizeResponseMode.Redirect,
LoadProfile = true
};
_oidcClient = new OidcClient(options);
var result = await _oidcClient.LoginAsync();
在服务器端启动配置服务:
private void ConfigureAuthentication(ServiceConfigurationContext context, IConfiguration configuration)
{
context.Services.Configure<IISOptions>(iis => // IISOptions
{
iis.AuthenticationDisplayName = "Windows";
iis.AutomaticAuthentication = false;
});
context.Services.AddAuthentication()
.AddJwtBearer(options =>
{
options.Authority = configuration["AuthServer:Authority"];
options.RequireHttpsMetadata = Convert.ToBoolean(configuration["AuthServer:RequireHttpsMetadata"]); ;
options.Audience = "ABPIdentityServer";
});
}
这是 ProcessWindowsLoginAsync 质询方法:
private async Task<IActionResult> ProcessWindowsLoginAsync(string returnUrl)
{
// see if windows auth has already been requested and succeeded
var result = await HttpContext.AuthenticateAsync(Microsoft.AspNetCore.Server.IISIntegration.IISDefaults.AuthenticationScheme);
if (result?.Principal is WindowsPrincipal wp)
{
// we will issue the external cookie and then redirect the
// user back to the external callback, in essence, tresting windows
// auth the same as any other external authentication mechanism
var props = new AuthenticationProperties()
{
RedirectUri = "./ExternalLoginCallback",
Items =
{
{ "returnUrl", returnUrl },
{ "scheme", Microsoft.AspNetCore.Server.IISIntegration.IISDefaults.AuthenticationScheme },
}
};
var id = new ClaimsIdentity(Microsoft.AspNetCore.Server.IISIntegration.IISDefaults.AuthenticationScheme);
id.AddClaim(new Claim(JwtClaimTypes.Subject, wp.Identity.Name));
id.AddClaim(new Claim(JwtClaimTypes.Name, wp.Identity.Name));
// add the groups as claims -- be careful if the number of groups is too large
{
var wi = (WindowsIdentity)wp.Identity;
var groups = wi.Groups.Translate(typeof(NTAccount));
var roles = groups.Select(x => new Claim(JwtClaimTypes.Role, x.Value));
id.AddClaims(roles);
}
await HttpContext.SignInAsync(IdentityConstants.ExternalScheme, new ClaimsPrincipal(id), props);
return Redirect(props.RedirectUri);
}
else
{
// trigger windows auth
// since windows auth don't support the redirect uri,
// this URL is re-triggered when we call challenge
return Challenge("Windows");
}
}
我怀疑在调用 Challenge 时这段代码会以某种方式返回重定向到错误页面,但我不确定,现在我知道为什么了。
那我错过了什么?是否可以在 IIS 上同时运行 Windows 和匿名身份验证?
在这里我也发现了类似的问题: identity server 4 windows authentication
但提供的答案对我没有帮助。
【问题讨论】:
-
在支持的平台上,您可以使用 IdentityServer 通过 Windows 身份验证对用户进行身份验证。更多相关信息可以参考这个链接:docs.identityserver.io/en/dev/topics/windows.html
-
感谢您的链接,但我已经尝试过该链接上的内容。
标签: c# asp.net-core iis identityserver4 identitymodel