C# 向AAD OpenIDConnect认证用户添加自定义声明
我有一个MVC应用程序.NETV。4.5.2,其中我需要向AAD OpenIDConnect认证用户添加自定义声明 身份验证代码(Startup.cs): 主控制器中的以下行始终为空:C# 向AAD OpenIDConnect认证用户添加自定义声明,c#,.net,azure-active-directory,openid-connect,claims,C#,.net,Azure Active Directory,Openid Connect,Claims,我有一个MVC应用程序.NETV。4.5.2,其中我需要向AAD OpenIDConnect认证用户添加自定义声明 身份验证代码(Startup.cs): 主控制器中的以下行始终为空: var userClaims = User.Identity as System.Security.Claims.ClaimsIdentity; var test = userClaims?.FindFirst("MyTestClaim")?.Value; 显示了被验证用户的所有声明,但我无法显示“MyTest
var userClaims = User.Identity as System.Security.Claims.ClaimsIdentity;
var test = userClaims?.FindFirst("MyTestClaim")?.Value;
显示了被验证用户的所有声明,但我无法显示“MyTestClaim”。结果表明我的代码位于错误的OpenIdConnectAuthenticationNotifications部分。我应该使用SecurityTokenValidation,而不是使用AuthorizationCodeReceived。在这里添加自定义声明(到目前为止)还确保了当用户导航到应用程序中的不同页面时,自定义声明不会消失
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthenticationFailed = OnAuthenticationFailed,
SecurityTokenValidated = async (x) =>
{
var identity = x.AuthenticationTicket.Identity;
identity.AddClaim(new Claim("test", "test"));
await Task.FromResult(0);
}
}
您提到过吗?
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthenticationFailed = OnAuthenticationFailed,
SecurityTokenValidated = async (x) =>
{
var identity = x.AuthenticationTicket.Identity;
identity.AddClaim(new Claim("test", "test"));
await Task.FromResult(0);
}
}