JWT请求无法通过授权策略

问题描述 投票:0回答:2

我正在Asp.Net上实施JWT授权,但我在这里遇到了麻烦。当我尝试访问仪表板/家庭控制器的方法时,我总是收到“401 Unauthorized”响应。根据我的研究,我可以说请求无法通过

[Authorize(Policy = "ApiUser")]

但是当评论那句话我在这里收到错误

var userId = _caller.Claims.Single(c => c.Type == "id");

哪个说明序列中没有适当的元素,如果我打印出来的话

_httpContextAccessor.HttpContext.User.Claims

我基本上收到'[]'。另外我想让你注意到令牌是correct 我在postman的帮助下测试应用程序 我把这个错误堆了两天,真的很依赖你的帮助。告诉我是否需要一些额外的代码。 码: 仪表板控制器:

 [Authorize(Policy = "ApiUser")]
        [Route("api/[controller]/[action]")]
        public class DashboardController : Controller
        {
            private readonly ClaimsPrincipal _caller;
            private readonly BackendContext _appDbContext;
            private readonly IHttpContextAccessor _httpContextAccessor;

            public DashboardController(UserManager<AppUser> userManager, BackendContext appDbContext, IHttpContextAccessor httpContextAccessor)
            {
                _caller = httpContextAccessor.HttpContext.User;
                _appDbContext = appDbContext;
                _httpContextAccessor = httpContextAccessor;
            }

            // GET api/dashboard/home

            [HttpGet]
            public async Task<IActionResult> Home()
            {
                // retrieve the user info
                //HttpContext.User
                //return new OkObjectResult(_httpContextAccessor.HttpContext.User.Claims);
                    var userId = _caller.Claims.Single(c => c.Type == "id");

                var customer = await _appDbContext.Customers.Include(c => c.Identity).SingleAsync(c => c.Identity.Id == userId.Value);

                return new OkObjectResult(new
                {
                    Message = "This is secure API and user data!",
                    customer.Identity.FirstName,
                    customer.Identity.LastName,
                    customer.Identity.PictureUrl,
                    customer.Identity.FacebookId,
                    customer.Location,
                    customer.Locale,
                    customer.Gender
                });
            }
        }

启动:

public class Startup
    {
        private const string SecretKey = "iNivDmHLpUA223sqsfhqGbMRdRj1PVkH"; // todo: get this from somewhere secure
        private readonly SymmetricSecurityKey _signingKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(SecretKey));

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddIdentity<IdentityUser, IdentityRole>()
                .AddEntityFrameworkStores<BackendContext>()
                .AddDefaultTokenProviders();

            services.AddDbContext<BackendContext>(options =>
              options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
                  b => b.MigrationsAssembly("Backend")));

            services.AddSingleton<IJwtFactory, JwtFactory>();
            var jwtAppSettingOptions = Configuration.GetSection(nameof(JwtIssuerOptions));

            // Configure JwtIssuerOptions
            services.Configure<JwtIssuerOptions>(options =>
            {
                options.Issuer = jwtAppSettingOptions[nameof(JwtIssuerOptions.Issuer)];
                options.Audience = jwtAppSettingOptions[nameof(JwtIssuerOptions.Audience)];
                options.SigningCredentials = new SigningCredentials(_signingKey, SecurityAlgorithms.HmacSha256);
            });

            var tokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuer = true,
                ValidIssuer = jwtAppSettingOptions[nameof(JwtIssuerOptions.Issuer)],

                ValidateAudience = true,
                ValidAudience = jwtAppSettingOptions[nameof(JwtIssuerOptions.Audience)],

                ValidateIssuerSigningKey = true,
                IssuerSigningKey = _signingKey,

                RequireExpirationTime = false,
                ValidateLifetime = true,
                ClockSkew = TimeSpan.Zero
            };

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;

            }).AddJwtBearer(configureOptions =>
            {
                configureOptions.ClaimsIssuer = jwtAppSettingOptions[nameof(JwtIssuerOptions.Issuer)];
                configureOptions.TokenValidationParameters = tokenValidationParameters;
                configureOptions.SaveToken = true;
            });

            // api user claim policy
            services.AddAuthorization(options =>
            {
                options.AddPolicy("ApiUser", policy => policy.RequireClaim(Constants.Strings.JwtClaimIdentifiers.Rol, Constants.Strings.JwtClaims.ApiAccess));
            });

            // add identity
            var builder = services.AddIdentityCore<AppUser>(o =>
            {
                // configure identity options
                o.Password.RequireDigit = false;
                o.Password.RequireLowercase = false;
                o.Password.RequireUppercase = false;
                o.Password.RequireNonAlphanumeric = false;
                o.Password.RequiredLength = 6;
            });
            builder = new IdentityBuilder(builder.UserType, typeof(IdentityRole), builder.Services);
            builder.AddEntityFrameworkStores<BackendContext>().AddDefaultTokenProviders();

            services.AddAutoMapper();

            services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            services.TryAddSingleton<IActionContextAccessor, ActionContextAccessor>();
            services.AddDbContext<BackendContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
                b => b.MigrationsAssembly("Backend")));
            services.AddTransient<IStoreService, StoreService>();
            services.AddMvc();//.AddFluentValidation(fv => fv.RegisterValidatorsFromAssemblyContaining<Startup>());
        }
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseAuthentication();
            app.UseDefaultFiles();
            app.UseStaticFiles();

            app.UseMvc();
        }
    }

常量(在启动时使用):

 public static class Constants
        {
            public static class Strings
            {
                public static class JwtClaimIdentifiers
                {
                    public const string Rol = "rol", Id = "id";
                }

                public static class JwtClaims
                {
                    public const string ApiAccess = "api_access";
                }
            }
        }  
c# .net-core jwt authorization asp.net-core-webapi
2个回答
0
投票

对于您的问题是由IssuerAudience的令牌生成和验证之间的不匹配配置引起的。

Startup.cs中,你配置ValidateIssuer = trueValidateAudience = true,但是对于你提供的令牌,没有issaud

尝试以下两种方法:

  • 禁用以验证IssuerAudience var tokenValidationParameters = new TokenValidationParameters { ValidateIssuer = false, ValidIssuer = "Issuer", ValidateAudience = false, ValidAudience = "Audience", ValidateIssuerSigningKey = true, IssuerSigningKey = _signingKey, RequireExpirationTime = false, ValidateLifetime = true, ClockSkew = TimeSpan.Zero };
  • 或者,在生成令牌时提供IssuerAudience public IActionResult Login() { var secretKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("iNivDmHLpUA223sqsfhqGbMRdRj1PVkH")); var signinCredentials = new SigningCredentials(secretKey, SecurityAlgorithms.HmacSha256); var tokeOptions = new JwtSecurityToken( issuer: "Issuer", audience: "Audience", claims: new List<Claim>() { new Claim("rol", "api_access") }, expires: DateTime.Now.AddMinutes(25), signingCredentials: signinCredentials ); var tokenString = new JwtSecurityTokenHandler().WriteToken(tokeOptions); return Ok(new { Token = tokenString }); }

这是一个工作演示CoreJwt


0
投票

自问这个问题以来已过去了大约4个月,但我希望它能帮助未来的访客,我遇到了同样的问题,因为我错过了将app.UseAuthentication();添加到我的Configure方法:

app.UseAuthentication();
//////////////////////////
app.UseHttpsRedirection();
app.UseStaticFiles();

此外,我错过了将JwtIssuerOptions添加到我的appsettings.jsonappsettings.Development.json

"JwtIssuerOptions": {
"Issuer": "webApi",
"Audience": "http://localhost:5000/"
},
© www.soinside.com 2019 - 2024. All rights reserved.