【发布时间】:2016-02-26 03:22:01
【问题描述】:
我有一个 ASP.NET 5 beta 8 应用程序,它使用 OpenIdConnect 与 Azure Active Directory 集成。我尝试将应用程序更新到 RC1 并将 openid nuget 包更改为"Microsoft.AspNet.Authentication.OpenIdConnect": "1.0.0-rc1-final"。一切似乎都是正确的,但是我用来向我的ClaimsIdentity 添加角色的OnAuthenticationComplete 方法不再在Events 对象中,我找不到替代方法。如何使用新版本向我的身份添加声明?
更新:更改为 OnAuthenticationValidated 仍然不适合我。我一定是在我的活动代码中做错了什么:
OnAuthenticationValidated = async notification =>
{
var claimsIdentity = notification.AuthenticationTicket.Principal.Identity as ClaimsIdentity;
var userRepository=new UserRepository(Configuration);
var userId = claimsIdentity?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
var user = await userRepository.FindAsync(userId);
if (user == null)
{
await userRepository.AddAsync(new UserDto
{
UserId = userId,
Username = claimsIdentity?.FindFirst(ClaimTypes.Name)?.Value,
DisplayName = claimsIdentity?.FindFirst(ClaimTypes.Name)?.Value
});
}
claimsIdentity?.AddClaim(new Claim(ClaimTypes.Role, "super"));
}
另外,我用来登录的代码是:
[HttpGet]
public IActionResult Login()
{
if (HttpContext.User == null || !HttpContext.User.Identity.IsAuthenticated)
return new ChallengeResult(OpenIdConnectDefaults.AuthenticationScheme, new AuthenticationProperties { RedirectUri = "/" });
return RedirectToAction("Index", "Home");
}
【问题讨论】:
标签: asp.net-core asp.net-core-mvc azure-active-directory openid-connect