这是我想要完成的任务,也许混合和匹配不起作用,但我很难找到执行以下操作的方法:
API 上的 Windows 身份验证 (AddNegotiate)。以这种方式对用户进行身份验证后,我将检索他们的域安全组,然后从字典中根据成员资格为他们分配角色声明。
我希望能够在使用 API 进行身份验证的其他几个应用程序之间共享此经过身份验证的会话,以便仅在会话超时后才需要进行组查询。
我是否想要执行某种 Cookie 实现来实现此目的?因此,应用程序看起来没有身份验证,而是转到 API,在确认 Windows 组和角色后,会根据该成员身份发出 cookie。
可能还会在应用程序的其他位置进行手动 LDAP 登录,以向当前用户身份添加声明。
我的身份验证服务。 Cookie 和 Negotiate 的组合,如果 DefaultAuthenticationScheme 上没有 NegotateiDefualts,它会提示登录,我希望这是自动的。但是设置了cookie之后,Cookie就应该是认证了。
// Authentication Services
services.AddHttpContextAccessor();
services.AddScoped<IUserPreferencesService, UserPreferencesService>();
services.AddScoped<IDomainUserGroupService, DomainUserGroupService>();
services.AddDataProtection()
.PersistKeysToFileSystem(new DirectoryInfo(Path.Combine(Directory.GetCurrentDirectory(), "DataProtectionKeys")))
.SetApplicationName("AuthenticationAPI")
.SetDefaultKeyLifetime(TimeSpan.FromDays(90));
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = NegotiateDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = NegotiateDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
{
options.Cookie.Name = "AuthCookie";
options.Cookie.Domain = config["DomainSite"];
options.Cookie.HttpOnly = true;
options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
options.Cookie.SameSite = SameSiteMode.None;
options.Cookie.Path = "/";
})
.AddNegotiate(); // For Windows Authentication to retrieve User Groups
/login 是一个控制器:
// LoginUser with Sign In of Claims based on Role Membership
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
[HttpGet("login")]
public async Task<IActionResult> Login()
{
_logger.LogMessage("Loaded Environment : " + _appConfiguration.EnvrionmentLoaded);
if (User.Identity is WindowsIdentity windowsIdentity)
{
var lastAuthenticated = DateTime.UtcNow;
var roles = GetRolesFromGroups(windowsIdentity);
var claims = CreateClaims(windowsIdentity.Name, roles, lastAuthenticated);
var identity = new ClaimsIdentity(claims, "Cookie");
var principal = new ClaimsPrincipal(identity);
var authProperties = new AuthenticationProperties()
{
IsPersistent = true,
ExpiresUtc = lastAuthenticated.AddHours(1)
};
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal, authProperties);
return Ok(new { Message = "User signed in."});
}
else
{
return Unauthorized();
}
}
我将 Windows 身份验证和匿名身份验证设置为 True。现在,初始声明看起来只是在 Windows AUthenitcation 自动返回的声明中加载,并且没有从登录中设置角色。
如何在默认情况下使其路由到登录登录,以便用户立即获得声明?我在这里做错了什么身份验证组合?
不确定它是否有帮助,但我所拥有的(仅适用于代码,由于某种原因在服务器上失败)是2个网站,第一个使用Windows身份验证(可以使用这个来创建您的组),然后该网站创建然后,包含任何相关声明的 cookie 会重定向到另一个基于 cookie 的网站,在该网站中,使用来自 win auth 网站的 cookie 对用户进行身份验证。
我的代码使用.net core 8 如果您认为我们可以互相帮助,请给我留言