Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 向AAD OpenIDConnect认证用户添加自定义声明_C#_.net_Azure Active Directory_Openid Connect_Claims - Fatal编程技术网

C# 向AAD OpenIDConnect认证用户添加自定义声明

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

我有一个MVC应用程序.NETV。4.5.2,其中我需要向AAD OpenIDConnect认证用户添加自定义声明

身份验证代码(Startup.cs):

主控制器中的以下行始终为空:

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);
                    }
                 }