【发布时间】:2021-07-20 16:19:52
【问题描述】:
就像 AzureAD 一样,我们有自己的自定义公司 ActiveDirectory,我们使用 OpenIdConnect(AddOpenIdConnect 扩展方法)从 UI 和 .NetCore 中的身份验证 API 连接它。 在我在 UI 端进行身份验证后的用例中,我需要来自我添加“OnTokenValidated”的自定义数据库中的其他应用程序特定声明 - 这是基于角色和声明隐藏或公开 UI 元素所必需的。
OnTokenValidated = async ctx =>
{
//Get user's immutable object id from claims that came from Azure AD
string oid = ctx.Principal.FindFirstValue("http://schemas.microsoft.com/identity/claims/objectidentifier");
//Get EF context
var db = ctx.HttpContext.RequestServices.GetRequiredService<AuthorizationDbContext>();
//Check is user a super admin
bool isSuperAdmin = await db.SuperAdmins.AnyAsync(a => a.ObjectId == oid);
if (isSuperAdmin)
{
//Add claim if they are
var claims = new List<Claim>
{
new Claim(ClaimTypes.Role, "superadmin")
};
var appIdentity = new ClaimsIdentity(claims);
ctx.Principal.AddIdentity(appIdentity);
}
}
现在在 API 端再次进行令牌验证后,我必须调用自定义数据库来获取应用程序特定的角色。是否可以将这些角色包含在 JWT 令牌本身中,所以在 API 端,所有角色和声明(AD + Custom DB)都存在。或者我不必在 API 中再次调用 CustomDB 的任何其他方式。
【问题讨论】:
标签: c# asp.net authentication openid-connect dotnetopenauth