Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.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# 使用Microsoft.AspNetCore.ApiAuthorization.IdentityServer时,如何在JWT和用户主体中添加自定义用户声明_C#_Asp.net Core_Identityserver4_Asp.net Core Identity - Fatal编程技术网

C# 使用Microsoft.AspNetCore.ApiAuthorization.IdentityServer时,如何在JWT和用户主体中添加自定义用户声明

C# 使用Microsoft.AspNetCore.ApiAuthorization.IdentityServer时,如何在JWT和用户主体中添加自定义用户声明,c#,asp.net-core,identityserver4,asp.net-core-identity,C#,Asp.net Core,Identityserver4,Asp.net Core Identity,我正在使用新的React模板,它是.NETCore3版本的一部分。此模板使用Microsoft.AspNetCore.ApiAuthorization.IdentityServer集成ASP.NET核心标识、IdentityServer并对用户注册、身份验证和授权做出反应 这对于简单场景非常有效,但我发现对于更复杂场景的文档比较混乱。这主要是因为现在有很多不同的齿轮,而且很难找到合适的位置 我想做的是:我想向用户添加一个自定义声明(比如,IsAdmin:true)。出于身份验证的目的,此声明应该

我正在使用新的React模板,它是.NETCore3版本的一部分。此模板使用
Microsoft.AspNetCore.ApiAuthorization.IdentityServer
集成ASP.NET核心标识、IdentityServer并对用户注册、身份验证和授权做出反应

这对于简单场景非常有效,但我发现对于更复杂场景的文档比较混乱。这主要是因为现在有很多不同的齿轮,而且很难找到合适的位置

我想做的是:我想向用户添加一个自定义声明(比如,
IsAdmin:true
)。出于身份验证的目的,此声明应该在ApiController的.NET Core HttpContext中可用(作为用户声明主体的一部分),并且应该在React可以读取此声明的地方(这可能是identitytoken/jwt),以提供良好的用户体验

实现这一点的好方法是什么?

我认为您应该检查ApiResource配置。无论您在ApiResource配置的UserClaims属性中添加了什么声明,这些声明都将出现在访问令牌中。e、 g

 public IEnumerable<ApiResource> GetApiResources()
 {
      return new List<ApiResource>
      {
            new ApiResource("api1")
            {
                UserClaims = new[] { "CustomClaim1", "CustomClaim2"},
            }, 
       }
 }
public IEnumerable GetApiResources()
{
返回新列表
{
新ApiResource(“api1”)
{
UserClaims=new[]{“CustomClaim1”、“CustomClaim2”},
}, 
}
}
在上面的代码中,访问代码将包含CustomClaim1和CustomClaim2。因此,如果您不提及它们,它们将不会出现在访问令牌中。希望这有帮助

我认为您应该检查ApiResource配置。无论您在ApiResource配置的UserClaims属性中添加了什么声明,这些声明都将出现在访问令牌中。e、 g

 public IEnumerable<ApiResource> GetApiResources()
 {
      return new List<ApiResource>
      {
            new ApiResource("api1")
            {
                UserClaims = new[] { "CustomClaim1", "CustomClaim2"},
            }, 
       }
 }
public IEnumerable GetApiResources()
{
返回新列表
{
新ApiResource(“api1”)
{
UserClaims=new[]{“CustomClaim1”、“CustomClaim2”},
}, 
}
}

在上面的代码中,访问代码将包含CustomClaim1和CustomClaim2。因此,如果您不提及它们,它们将不会出现在访问令牌中。希望这有帮助

您可以使用
IProfileService
向JWT令牌添加自定义声明:

public class MyProfileService : IProfileService
{
    public MyProfileService()
    { }

    public Task GetProfileDataAsync(ProfileDataRequestContext context)
    {

        var claims = new List<Claim>()
        {

            new Claim(ClaimTypes.Role, "Admin")
        };
        context.IssuedClaims.AddRange(claims);
        return Task.CompletedTask;
    }

    public Task IsActiveAsync(IsActiveContext context)
    {
        // await base.IsActiveAsync(context);
        return Task.CompletedTask;
    }
}
公共类MyProfileService:IProfileService
{
公共MyProfileService()
{ }
公共任务GetProfileDataAsync(ProfileDataRequestContext上下文)
{
var索赔=新列表()
{
新索赔(ClaimTypes.Role,“Admin”)
};
context.IssuedClaims.AddRange(索赔);
返回Task.CompletedTask;
}
公共任务IsActiveAsync(IsActiveContext上下文)
{
//wait base.IsActiveAsync(上下文);
返回Task.CompletedTask;
}
}
关于ID令牌的长度,默认情况下,声明不会包含在ID令牌中,而是从OIDC的userinfo端点获取声明。你可以:

  • 从React模板中的
    user.profile
    获取声明,它将自动向具有id令牌的userinfo端点发送请求

  • 或者,在Identity Server 4中注册客户端时,您可以在客户端选项中将
    AlwaysInCludeUserClaimsIniToken
    设置为true,但我担心您不需要使用
    ApiAuthorization
    服务,Identity Server的全部功能仍然可用于自定义身份验证以满足您的需要


  • 您可以使用
    IProfileService
    向JWT令牌添加自定义声明:

    public class MyProfileService : IProfileService
    {
        public MyProfileService()
        { }
    
        public Task GetProfileDataAsync(ProfileDataRequestContext context)
        {
    
            var claims = new List<Claim>()
            {
    
                new Claim(ClaimTypes.Role, "Admin")
            };
            context.IssuedClaims.AddRange(claims);
            return Task.CompletedTask;
        }
    
        public Task IsActiveAsync(IsActiveContext context)
        {
            // await base.IsActiveAsync(context);
            return Task.CompletedTask;
        }
    }
    
    公共类MyProfileService:IProfileService
    {
    公共MyProfileService()
    { }
    公共任务GetProfileDataAsync(ProfileDataRequestContext上下文)
    {
    var索赔=新列表()
    {
    新索赔(ClaimTypes.Role,“Admin”)
    };
    context.IssuedClaims.AddRange(索赔);
    返回Task.CompletedTask;
    }
    公共任务IsActiveAsync(IsActiveContext上下文)
    {
    //wait base.IsActiveAsync(上下文);
    返回Task.CompletedTask;
    }
    }
    
    关于ID令牌的长度,默认情况下,声明不会包含在ID令牌中,而是从OIDC的userinfo端点获取声明。你可以:

  • 从React模板中的
    user.profile
    获取声明,它将自动向具有id令牌的userinfo端点发送请求

  • 或者,在Identity Server 4中注册客户端时,您可以在客户端选项中将
    AlwaysInCludeUserClaimsIniToken
    设置为true,但我担心您不需要使用
    ApiAuthorization
    服务,Identity Server的全部功能仍然可用于自定义身份验证以满足您的需要


  • 您希望这些声明也出现在访问令牌中吗?@AparnaGadgil这可能是最好的。我认为这是在当前请求的用户主体中获取它的最简单方法,对吗?是的,正确!我在下面添加了我的答案,以获取访问代码中的自定义声明。你好,尼克,我想知道你是否能解决这个问题。我在试图解决如何将自定义声明或角色声明添加到客户端时遇到了完全相同的问题?有关更多背景信息,请查看答案。是否希望声明也显示在access token中?@aparnagagadgil这可能是最好的。我认为这是在当前请求的用户主体中获取它的最简单方法,对吗?是的,正确!我在下面添加了我的答案,以获取访问代码中的自定义声明。你好,尼克,我想知道你是否能解决这个问题。我在尝试解决如何向客户添加自定义声明或角色声明时遇到了完全相同的问题?有关更多背景信息,请查看答案。谢谢!但是你知道我是如何通过我提到的ApiAuthorization集成包实现这一点的吗?似乎ApiResource是在这里生成的,我不知道如何定制它:这个类实际构建了ApiResource:有吗