Auth0 访问令牌不返回角色(授权扩展)
Auth0 Access Token Not Returning Roles (Authorization Extension)
请协助。我是 Auth0 的新手。我想在我的 angular 8 应用程序和 ASP.NET Core 2.2 API 中使用基于角色的访问控制 (RBAC)。我已经在 Auth0 中注册了 API 和 SPA。 身份验证工作正常,但我遇到授权问题。我从 Auth0 返回的 access_token 中没有角色。它具有权限而不是角色。我已经安装了 Auth0 Authorization2.6 扩展。我还启用了 RBAC。
我从输出日志中得到这个 (Visual Studio 2019) Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler:Information: Successfully validated the token.
并且它无法授权并出现此错误 Microsoft.AspNetCore.Authorization.DefaultAuthorizationService:Information: Authorization failed
我已经创建了用户、角色和权限。我为所有用户分配了相关角色。下面是我的 Startup class(API)的代码片段。
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddCors(options =>
{
options.AddPolicy("AllowSpecificOrigin",
builder =>
{
builder
.WithOrigins("http://localhost:4200")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
var domain = $"https://{Configuration["Auth0:Issuer"]}/";
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = ClaimTypes.NameIdentifier,
RoleClaimType = ClaimTypes.Role,
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = Configuration["Auth0:Issuer"],
ValidAudience = Configuration["Auth0:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration.GetSection("Auth0")["ClientSecret"]))
};
options.Authority = domain;
options.Audience = Configuration["Auth0:Audience"];
});
services.AddAuthorization(options =>
{
options.AddPolicy("StakeholderManager", policy => policy.RequireRole("StakeholderManager"));
});
}
我的 API 端点看起来像这样
[HttpGet("students")]
[Authorize(Roles = "StakeholderManager")]
public IActionResult GetAllStudents()
{
var user = User;
return Ok(new
{
Student1 = "James Bond",
Student2 = "Belinda Abrahams"
});
}
下面如果我的访问令牌被发送到 API 进行身份验证和授权
启用 RBAC
我还修改了 Auth0 授权规则以尝试按照下面的屏幕截图包含角色,但这也没有帮助。
授权扩展将被弃用并替换为核心 RBAC 功能(这是您在仪表板的用户部分中看到的角色)。如果您将角色和权限迁移到该功能,您应该能够将角色添加到自定义声明中的令牌,就像您在提供的屏幕截图中尝试做的那样。
有关示例,请参阅此文档:
https://auth0.com/docs/authorization/concepts/sample-use-cases-rules#add-user-roles-to-tokens
这个文档也很有用:
https://auth0.com/docs/authorization/concepts/core-vs-extension
请协助。我是 Auth0 的新手。我想在我的 angular 8 应用程序和 ASP.NET Core 2.2 API 中使用基于角色的访问控制 (RBAC)。我已经在 Auth0 中注册了 API 和 SPA。 身份验证工作正常,但我遇到授权问题。我从 Auth0 返回的 access_token 中没有角色。它具有权限而不是角色。我已经安装了 Auth0 Authorization2.6 扩展。我还启用了 RBAC。
我从输出日志中得到这个 (Visual Studio 2019) Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler:Information: Successfully validated the token.
并且它无法授权并出现此错误 Microsoft.AspNetCore.Authorization.DefaultAuthorizationService:Information: Authorization failed
我已经创建了用户、角色和权限。我为所有用户分配了相关角色。下面是我的 Startup class(API)的代码片段。
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddCors(options =>
{
options.AddPolicy("AllowSpecificOrigin",
builder =>
{
builder
.WithOrigins("http://localhost:4200")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
var domain = $"https://{Configuration["Auth0:Issuer"]}/";
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = ClaimTypes.NameIdentifier,
RoleClaimType = ClaimTypes.Role,
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = Configuration["Auth0:Issuer"],
ValidAudience = Configuration["Auth0:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration.GetSection("Auth0")["ClientSecret"]))
};
options.Authority = domain;
options.Audience = Configuration["Auth0:Audience"];
});
services.AddAuthorization(options =>
{
options.AddPolicy("StakeholderManager", policy => policy.RequireRole("StakeholderManager"));
});
}
我的 API 端点看起来像这样
[HttpGet("students")]
[Authorize(Roles = "StakeholderManager")]
public IActionResult GetAllStudents()
{
var user = User;
return Ok(new
{
Student1 = "James Bond",
Student2 = "Belinda Abrahams"
});
}
下面如果我的访问令牌被发送到 API 进行身份验证和授权
启用 RBAC
我还修改了 Auth0 授权规则以尝试按照下面的屏幕截图包含角色,但这也没有帮助。
授权扩展将被弃用并替换为核心 RBAC 功能(这是您在仪表板的用户部分中看到的角色)。如果您将角色和权限迁移到该功能,您应该能够将角色添加到自定义声明中的令牌,就像您在提供的屏幕截图中尝试做的那样。
有关示例,请参阅此文档: https://auth0.com/docs/authorization/concepts/sample-use-cases-rules#add-user-roles-to-tokens
这个文档也很有用: https://auth0.com/docs/authorization/concepts/core-vs-extension