简体   繁体   English

如何获取asp.net core 6 web api中的Role Claims?

[英]How to get Role Claims in asp.net core 6 web api?

I try to get RoleClaims from ASP.NET CORE Identity and my code is:我尝试从 ASP.NET CORE Identity 获取RoleClaims ,我的代码是:

[HttpGet]
public async Task<IActionResult> GetAllRoleClaims(string email)
{
    var user = await _userManager.FindByEmailAsync(email);
    var role= await _userManager.GetRolesAsync(user);
    var roleclaim = await _roleManager.GetClaimsAsync(role);
   // var roleclaim = await _roleManager.GetClaimsAsync((IdentityRole)role);

    return Ok(roleclaim);
}

It give me the error:它给我错误:

cannot convert from 'System.Collections.Generic.IList' to 'Microsoft.AspNetCore.Identity.IdentityRole'无法从“System.Collections.Generic.IList”转换为“Microsoft.AspNetCore.Identity.IdentityRole”

the Error is at this section:错误在这一部分:

var roleclaim = _roleManager.GetClaimsAsync(role);

The GetClaimsAsync(role) function does not accept the role and give the above error and when I use this var roleclaim = _roleManager.GetClaimsAsync((IdentityRole)role); GetClaimsAsync(role) function 不接受该role并出现上述错误,当我使用此var roleclaim = _roleManager.GetClaimsAsync((IdentityRole)role); for conversion it gives me another error at runtime:对于转换,它在运行时给我另一个错误:

Unable to cast object of type 'System.Collections.Generic.List`1[System.String]' to type 'Microsoft.AspNetCore.Identity.IdentityRole'.无法将类型为“System.Collections.Generic.List`1[System.String]”的 object 转换为类型“Microsoft.AspNetCore.Identity.IdentityRole”。

Can anyone help me how to getRoleClaims from .net core Identity using roleManager ?谁能帮助我如何使用getRoleClaims从 .net core Identity roleManager

In your code,在你的代码中,

var role= await _userManager.GetRolesAsync(user);

Returns a list of role names (strings), eg ["admin", "user"].返回角色名称(字符串)列表,例如 ["admin", "user"]。

Now, use the role name (string) to get the role object if your user only has one role,现在,如果您的用户只有一个角色,则使用角色名称(字符串)获取角色 object,

var role = await _roleManager.FindByNameAsync(role.First());

Then, pass the role to get the claims,然后,传递角色以获取声明,

var roleclaim = _roleManager.GetClaimsAsync(role);

As per your scenario you can do as following: Therefore, it will return you the required claim.根据您的情况,您可以执行以下操作: 因此,它将返回给您所需的索赔。

[HttpGet]
public async Task<IActionResult> GetAllRoleClaims(string email)
{
    var user = await _userManager.FindByEmailAsync(email);

    var roleclaim = await _userManager.GetClaimsAsync(user);

    return Ok(roleclaim);
}

Note: If you want to get only single claim by given email, in this scenario its better to skip looping and above way would be efficient in regards of Big O .注意:如果你只想通过给定的 email 获得单一声明,在这种情况下最好跳过循环,并且以上方法对于Big O来说是有效的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用ASP.NET Core获取另一个用户的声明 - how to get claims of another user using ASP.NET Core 通过ASP.NET核心身份中的角色声明进行JWT身份验证 - JWT Authentication by role claims in ASP.NET core Identity ASP.NET Core JWT 映射角色声明到 ClaimsIdentity - ASP.NET Core JWT mapping role claims to ClaimsIdentity ASP.NET 核心 JWT 身份验证和授权角色声明 - ASP.NET Core JWTAuthentication and Authorize Role Claims 如何从 asp.net web api 中的身份声明获取 JWT 身份验证的登录用户 - How to get JWT authenticated sign-in user from identity claims in asp.net web api 如何在ASP.NET Core Web API中实现通过id获取? - How to implement get by id in ASP.NET Core Web API? 如何在 .Net Core 3.1 Web API 控制器中获取声明? - How to get claims in .Net Core 3.1 Web API Controller? 如何在Asp.NET Core WEB API中使用.Net(C#)在有效载荷中使用自定义JSON声明创建JWT令牌 - How to create a JWT token with custom JSON claims in Payload using .Net (C#) in Asp.NET Core WEB API ASP.NET 核心 Web API - 如何 ZE0626222614BDEE31951D84C64E5E 使用基于登录用户角色的 DBEE31951D84C64E5E 身份登录用户角色 - ASP.NET Core Web API - How to Select Records based on logged in user role using DB Identity 与自定义身份验证中的角色权限相比,ASP.NET核心身份中的角色声明 - Role Claims in ASP.NET Core Identity compared to Role Permissions in custom auth
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM