Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.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
Asp.net core 使用Identity server在Azure功能中获取用户信息和其他声明_Asp.net Core_Authentication_Azure Functions_Identityserver4_Claims - Fatal编程技术网

Asp.net core 使用Identity server在Azure功能中获取用户信息和其他声明

Asp.net core 使用Identity server在Azure功能中获取用户信息和其他声明,asp.net-core,authentication,azure-functions,identityserver4,claims,Asp.net Core,Authentication,Azure Functions,Identityserver4,Claims,我有一个应用程序,其中身份验证是使用IdentityServer4和Azure AD作为OpenID提供者完成的。IdentityServer4托管在Azure应用程序服务中。在成功的身份验证之后,我能够在Angular应用程序中获得访问令牌。访问令牌被传递到基于.Net内核的RESTful API,该API托管在Azure Function 3.x中。 在我的Azure函数中,我希望在不触及IdentityServer4的端点“/connect/userinfo”的情况下获取用户信息和其他声明

我有一个应用程序,其中身份验证是使用IdentityServer4和Azure AD作为OpenID提供者完成的。IdentityServer4托管在Azure应用程序服务中。在成功的身份验证之后,我能够在Angular应用程序中获得访问令牌。访问令牌被传递到基于.Net内核的RESTful API,该API托管在Azure Function 3.x中。 在我的Azure函数中,我希望在不触及IdentityServer4的端点“/connect/userinfo”的情况下获取用户信息和其他声明

类似于以下内容的内容将有助于获得索赔

[FunctionName("MyFunctionName")]
public static HttpResponseMessage Run(
            [HttpTrigger(
                AuthorizationLevel.Anonymous,
                "get", "post",
                Route = "MyFunctionName")]HttpRequestMessage req, 
            ILogger log, 
            ClaimsPrincipal claimsPrincipal)
{
    // My function code here...
}

如何在Azure函数中获取I用户信息和其他声明,其中身份验证由IdentityServer4完成,Azure AD作为OpenID提供程序

如果您拥有有效的访问令牌,则您可以自行向UserInfo端点发出请求,以检索剩余的用户详细信息

阅读更多关于它的信息


如果您不想访问userinfo端点,唯一的选择是直接在令牌中包含所需的数据。在这里,您需要在令牌大小和便利性之间进行权衡。然后你会得到一个真正的无状态系统。

如果你不想点击Identity Server的用户信息端点来获取用户信息和其他声明,下面是需要做的事情

  • 将索赔信息添加到授权令牌
  • 解析授权令牌并提取用户信息和其他声明信息
  • 这种方法的缺点是增加了令牌大小,但优点是不需要hit userinfo端点来保存http请求。因此,每种方法之间都有权衡

    以下是如何在Identity Server中配置api时添加索赔信息。通常,如果您使用了Identity Server模板,则此信息驻留在Config.cs中

    public static IEnumerable<ApiResource> GetApis()
        {
            var apiResourceList = new List<ApiResource>
            {
                    new ApiResource(IdentityServerConstants.LocalApi.ScopeName)
                    { 
                        UserClaims =
                        {
                            JwtClaimTypes.Email,
                            JwtClaimTypes.PhoneNumber,
                            JwtClaimTypes.GivenName,
                            JwtClaimTypes.FamilyName,
                            JwtClaimTypes.PreferredUserName
                        },                      
                    }
                    
            };          
            return apiResourceList;
        }
    
    公共静态IEnumerable


    这也非常有用。

    我想在Azure功能中访问此信息。为每个Http请求点击/userinfo端点不是一个好主意。应该有某种机制,通过它我可以访问userinfo而不必到达终点。更新了我的答案