asp.net-core - 在 ASP .Net Core 2.2 中添加 JWT 令牌后授权不起作用
问题描述
我正在学习如何在 ASP 中使用 JWT 令牌身份验证。我有一个简单的网站,其中包含 CRUD 操作和一个存储库,我添加了注册/登录功能,并且我有一个名为“管理员”的超级用户角色。通常,这是我检查用户当前是否以管理员身份登录的方式:
bool admin = User.IsInRole(Constants.ADMIN_ROLE);
在我将 JWT 身份验证添加到我的应用程序之前,此功能运行良好。现在,使用我的管理员帐户(手动添加到数据库)登录,我从来没有得到正确的值。当我尝试查询用户时,我得到 NULL:
var user = await _userManager.GetUserAsync(HttpContext.User)
这是我在 Startup.cs 中的 ConfigureServices:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddIdentity<MyUser, MyRole>()
.AddEntityFrameworkStores<MyContext>()
.AddRoles<MyRole>();
services.AddDbContext<MyContext>(builder =>
{
builder.UseSqlServer(Configuration["ConnectionStrings"]);
});
// repo code here ommitted for clarity
var appSettingsSection = Configuration.GetSection("AppSettings");
services.Configure<AppSettings>(appSettingsSection);
var appSettings = appSettingsSection.Get<AppSettings>();
var key = Encoding.ASCII.GetBytes(appSettings.Secret);
services.AddAuthentication(x =>
{
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(x =>
{
x.RequireHttpsMetadata = false;
x.SaveToken = true;
x.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(key),
ValidateIssuer = false,
ValidateAudience = false,
ValidateLifetime = true,
};
});
}
这是我的配置类:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseMvc();
app.UseCors(x => x.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
}
据了解,由于 JWT 是身份验证,它根本不应该影响授权,所以我想我忘了使用一些额外的选项,但我似乎找不到什么。任何帮助表示赞赏!
解决方案
我最终完全在 JWT 中实现了身份验证。
在创建令牌描述符期间,我在令牌中添加了一个新的用户声明:
...
Subject = new ClaimsIdentity(new Claim[]
{
new Claim(ClaimTypes.Name, userId.ToString()),
new Claim(ClaimTypes.Role, roleId)
}),
...
并添加与内部角色相关的信息(在我的情况下,我使用了角色的 ID 以使其不那么具有描述性,以防有人设法破译它)
var roles = await _userManager.GetRolesAsync(userInDb);
var userRoleId = await _roleManager.FindByNameAsync(roles.First());
...
return Ok(_tokenService.GenerateToken(userInDb.Id, userRoleId.Id.ToString()));
然后,确定用户是否是管理员就像从令牌中获取适当的值一样简单:
private async Task<bool> IsUserAdmin()
{
var roleId = Guid.Parse(User.Claims.FirstOrDefault(e => e.Type == "role").Value);
var adminId = (await _roleManager.FindByNameAsync(Constants.ADMIN_ROLE))?.Id;
return roleId == adminId;
}
尽管这似乎可行,但我仍然认为可能有更好(不那么 hacky)的方法可以更好地将 JWT 集成到身份中,所以如果有人可以提出更简洁的解决方案,我很乐意接受它作为答案而不是我的解决方案。
推荐阅读
- mysql - 如何计算联接表中的特定变量
- excel - Excel VBA 条件、动态工作表参考
- javascript - 将数组中的数字相加
- javascript - JavaScript 算法和 JavaScript 引擎
- javascript - 如果对象包含真键,则返回布尔真值,不返回值
- sql - 无法将记录集中的值分配给 vba 变量(错误 -2147217887 (80040e21))
- php - wordpress ajax中的400个错误请求
- sql - Keep ORDER BY 子句在视图中无效
- python - 从 Python 中的现有文件中选择一些单词
- wordpress - 如何限制下载副本和打印阅读microsoft office online 中的PPT 文件?