我正在尝试尽可能简单地在我的 asp.net 核心 webAPI 上实现 JWT 身份验证。我不知道我错过了什么,但即使使用正确的不记名令牌,它也总是返回 401 。
这是我的 configureServices 代码
public void ConfigureServices(IServiceCollection services)
        {
            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(Encoding.ASCII.GetBytes("A_VERY_SECRET_SECURITY_KEY_FOR_JWT_AUTH")),
                       ValidateAudience = false,
                       ValidateIssuer = false,
                   };
               }
                );
            services.AddControllers();
            services.AddDbContext<dingdogdbContext>(options =>
                    options.UseSqlServer(Configuration.GetConnectionString("dingdogdbContext")));
        }
这就是我生成令牌的方式
        [AllowAnonymous]
        [HttpPost("/Login")]
        public ActionResult<User> Login(AuthModel auth)
        {
            var user = new User();
            user.Email = auth.Email; …asp.net asp.net-authorization jwt asp.net-web-api asp.net-core
美好的一天。
该API适用于报价共享网络应用。
我安装了基于角色的JWT身份验证,其中我具有“成员”和“管理员”角色,并且这些角色的用户正确注册并能够检索令牌。
到目前为止,方法(或类)仅
[Authorize]
可以通过注册令牌正确访问。
现在,一旦我添加了角色,就可以访问需要特定角色的方法或类
[Authorize(Role="Admin")]
是禁止的(403),即使我确实通过Authorization标头传递了正确的令牌。
请注意:我已验证是否正确创建了用户(dbo.AspNetUsers),正确创建了角色(包含“管理员”和“成员”角色的dbo.AspNetRoles)以及正确映射了用户角色(dbo.AspNetUserRoles)。
这是Startup类,其中包含由Configure()调用的方法CreateRoles():
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
    public IConfiguration Configuration { get; }
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<QuotContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        services.AddIdentity<Member, IdentityRole>()
            .AddEntityFrameworkStores<QuotContext>()
            .AddDefaultTokenProviders();
        services.Configure<IdentityOptions>(options =>
        {
            // Password settings
            options.Password.RequireDigit = false;
            options.Password.RequiredLength = 4;
            options.Password.RequireNonAlphanumeric = false;
            options.Password.RequireUppercase = false;
            options.Password.RequireLowercase = false;
            options.Password.RequiredUniqueChars = 2;
            // Lockout settings
            options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30);
            options.Lockout.MaxFailedAccessAttempts = 10;
            options.Lockout.AllowedForNewUsers = true;
            // User settings …