MVC:使用 AAD 身份验证时添加和编辑身份声明角色名称
MVC: Adding and Editing Identity Claim Role Name while using AAD authentication
我使用 AAD 登录并对用户进行身份验证,不幸的是,我无法从 AAD 使用基于角色的功能,因此我需要依赖数据库中设置的角色,但是,我没有不知道如何根据数据库中的内容为此用户添加角色,而不影响身份验证 cookie?
不过,我设法在 app.UseOpenIdConnectAuthentication
中的 Startup.Auth
中手动添加了一个角色,就像这样
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthenticationFailed = (context) =>
{
context.HandleResponse();
context.OwinContext.Response.Redirect("/Account/Login");
return Task.FromResult(0);
},
SecurityTokenValidated = async (x) =>
{
var identity = x.AuthenticationTicket.Identity;
//check the name, add additional claims
identity.AddClaim(new Claim("http://schemas.microsoft.com/ws/2008/06/identity/claims/role", "Administrator"));
await Task.FromResult(0);
}
}
但是,由于我是手动添加 Administrator
,所以我不知道如何根据链接到数据库中用户的角色来更改它。我想我也许可以在它到达我的控制器(这是我的 home/index)时更新角色,但很难找到有用的东西。
我想利用User.IsInRole("UserRoleNameHere")
。
任何帮助或想法将不胜感激!
我找到了解决办法!最终我所做的是向 ClaimsIdentity 添加一个新声明,注销以清除之前存储的身份,然后再次登录以使用新添加的声明添加新身份:
var result = (from U in db.Users
from R in U.Roles
join R2 in db.Roles on R.RoleId equals R2.Id
where U.Id == loggedInUserId
select new { R2.Name, U.UserName }).FirstOrDefault();
ClaimsIdentity identity = (ClaimsIdentity)User.Identity;
identity.AddClaim(new Claim("http://schemas.microsoft.com/ws/2008/06/identity/claims/role", result.Name));
IOwinContext context = new OwinContext();
context.Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);
context.Authentication.SignIn(identity);
await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
if (User.IsInRole("Administrator"))
{
isAdmin = true;
}
我使用 AAD 登录并对用户进行身份验证,不幸的是,我无法从 AAD 使用基于角色的功能,因此我需要依赖数据库中设置的角色,但是,我没有不知道如何根据数据库中的内容为此用户添加角色,而不影响身份验证 cookie?
不过,我设法在 app.UseOpenIdConnectAuthentication
中的 Startup.Auth
中手动添加了一个角色,就像这样
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthenticationFailed = (context) =>
{
context.HandleResponse();
context.OwinContext.Response.Redirect("/Account/Login");
return Task.FromResult(0);
},
SecurityTokenValidated = async (x) =>
{
var identity = x.AuthenticationTicket.Identity;
//check the name, add additional claims
identity.AddClaim(new Claim("http://schemas.microsoft.com/ws/2008/06/identity/claims/role", "Administrator"));
await Task.FromResult(0);
}
}
但是,由于我是手动添加 Administrator
,所以我不知道如何根据链接到数据库中用户的角色来更改它。我想我也许可以在它到达我的控制器(这是我的 home/index)时更新角色,但很难找到有用的东西。
我想利用User.IsInRole("UserRoleNameHere")
。
任何帮助或想法将不胜感激!
我找到了解决办法!最终我所做的是向 ClaimsIdentity 添加一个新声明,注销以清除之前存储的身份,然后再次登录以使用新添加的声明添加新身份:
var result = (from U in db.Users
from R in U.Roles
join R2 in db.Roles on R.RoleId equals R2.Id
where U.Id == loggedInUserId
select new { R2.Name, U.UserName }).FirstOrDefault();
ClaimsIdentity identity = (ClaimsIdentity)User.Identity;
identity.AddClaim(new Claim("http://schemas.microsoft.com/ws/2008/06/identity/claims/role", result.Name));
IOwinContext context = new OwinContext();
context.Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);
context.Authentication.SignIn(identity);
await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
if (User.IsInRole("Administrator"))
{
isAdmin = true;
}