我一直在开发一个带有 .NET Core 后端(服务)的 Angular 应用程序。任务是启用集成身份验证,即使其与本地用户无缝协作,因此我登录到我的(连接到本地 AD)机器一次,Web 应用程序让我无需再次登录即可进入。我们一直在使用 Identity Server 4 并打算使用它来实现这个场景。
官方网站上有一些关于 Windows 身份验证的文档(例如针对 Active directory):http://docs.identityserver.io/en/latest/topics/windows.html 但解释不多。根据我的信息,为了使这种情况起作用,浏览器使用 Kerberos 或 NTLM。 IS4 文档中均未提及它们。我不了解如何获取本地凭据以及 IS4 如何“知道”用户属于 AD?我如何才能确保只有来自特定域的用户才能访问我的应用程序?
我在这里找到了一些有用的东西 https://github.com/damienbod/AspNetCoreWindowsAuth 但问题仍然存在。即使我能够使用我的本地帐户访问该应用程序,我也不了解流程。
我希望在本地网络中使用该应用程序的用户无需输入登录名/密码即可登录该应用程序(一旦他已经登录到 Windows)。这是可以实现的吗?
身份服务器旨在充当身份提供者,如果您需要与您的 AD 交谈,您应该看到他们建议使用 IAuthenticationSchemeProvider 的联合网关架构。 Identity Server 充当端点并与您的 AD 对话。
这是链接:Federation Gateway
您可以控制以编程方式访问您的 AD 并传递正确的凭据以获得身份验证。该步骤应该在您的身份服务器中完成。在您通过身份验证后,您应该再次被重定向到您的应用程序。 关于你的最后一个问题,答案是肯定的,如果你的网站托管在 Intranet 上并且你可以访问你的广告,你不需要捕获你的凭据作为用户输入,你可以像我说的那样以编程方式访问广告.
下面是我用来连接活动目录的代码
在 ExternalController 类上,当您使用 IdentityServer 时,您会得到:(我不记得我从原始代码更改了多少,但您应该明白)
/// <summary>
/// initiate roundtrip to external authentication provider
/// </summary>
[HttpGet]
public async Task<IActionResult> Challenge(string provider, string returnUrl)
{
if (string.IsNullOrEmpty(returnUrl)) returnUrl = "~/";
// validate returnUrl - either it is a valid OIDC URL or back to a local page
if (Url.IsLocalUrl(returnUrl) == false && _interaction.IsValidReturnUrl(returnUrl) == false)
{
// user might have clicked on a malicious link - should be logged
throw new Exception("invalid return URL");
}
if (AccountOptions.WindowsAuthenticationSchemeName == provider)
{
// windows authentication needs special handling
return await ProcessWindowsLoginAsync(returnUrl);
}
else
{
// start challenge and roundtrip the return URL and scheme
var props = new AuthenticationProperties
{
RedirectUri = Url.Action(nameof(Callback)),
Items =
{
{ "returnUrl", returnUrl },
{ "scheme", provider },
}
};
return Challenge(props, provider);
}
}
private async Task<IActionResult> ProcessWindowsLoginAsync(string returnUrl)
{
// see if windows auth has already been requested and succeeded
var result = await HttpContext.AuthenticateAsync(AccountOptions.WindowsAuthenticationSchemeName);
if (result?.Principal is WindowsPrincipal wp)
{
// we will issue the external cookie and then redirect the
// user back to the external callback, in essence, testing windows
// auth the same as any other external authentication mechanism
var props = new AuthenticationProperties()
{
RedirectUri = Url.Action("Callback"),
Items =
{
{ "returnUrl", returnUrl },
{ "scheme", AccountOptions.WindowsAuthenticationSchemeName },
}
};
var id = new ClaimsIdentity(AccountOptions.WindowsAuthenticationSchemeName);
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
if (AccountOptions.IncludeWindowsGroups)
{
var wi = wp.Identity as WindowsIdentity;
var groups = wi.Groups.Translate(typeof(NTAccount));
var roles = groups.Select(x => new Claim(JwtClaimTypes.Role, x.Value));
id.AddClaims(roles);
}
await HttpContext.SignInAsync(
IdentityServer4.IdentityServerConstants.ExternalCookieAuthenticationScheme,
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(AccountOptions.WindowsAuthenticationSchemeName);
}
}
如果你想使用Azure AD,我会推荐你阅读这篇文章: https://damienbod.com/2019/05/17/updating-microsoft-account-logins-in-asp-net-core-with-openid-connect-and-azure-active-directory/
不确定它是否是您想要的,但我会使用
Active Directory Federation Services
配置 OAuth2 endpoint
并在 .Net Core Web App 中获取用户令牌。
NTLM 身份验证支持是否仅限于非 Microsoft 浏览器?
OAuth2 具有仅使用标准技术的优势。
一种方法是部署应用程序的 2 个实例。 第一个配置为使用 Windows 身份验证,另一个使用 IS4。
您的本地 DNS 应该在内部将流量从 yoursite.com 重定向到 yoursite.internal.com
yoursite.internal.com 将被配置为使用 AD 身份验证。您应该在 appsettings.json 中有一个标志来指示此实例是 AD 身份验证还是 IS4 身份验证。
这个解决方案的缺点是你必须部署 2 个实例