C# Postman返回401,尽管为安全端点分发了有效令牌

C# Postman返回401,尽管为安全端点分发了有效令牌,c#,security,asp.net-core,.net-core,jwt,C#,Security,Asp.net Core,.net Core,Jwt,我已经像这样设置了授权,松散地遵循了三个博客,并且(基本上使它完全开放,除了验证到期时间) 分发的令牌是这样创建的 string secret = "super-secret-password"; byte[] bytes = Encoding.ASCII.GetBytes(secret); SymmetricSecurityKey key = new SymmetricSecurityKey(bytes); Claim[] claims = { new Claim("role", "ba

我已经像这样设置了授权,松散地遵循了三个博客,并且(基本上使它完全开放,除了验证到期时间)

分发的令牌是这样创建的

string secret = "super-secret-password";
byte[] bytes = Encoding.ASCII.GetBytes(secret);
SymmetricSecurityKey key = new SymmetricSecurityKey(bytes);

Claim[] claims = {
  new Claim("role", "basic"),
  new Claim("role", "elevated"),
  new Claim("name", name)
};

JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler();
SecurityTokenDescriptor descriptor = new SecurityTokenDescriptor
{
  Subject = new ClaimsIdentity(claims),
  Expires = DateTime.Now.AddHours(1),
  SigningCredentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256Signature)
};

SecurityToken token = handler.CreateToken(descriptor);
return handler.WriteToken(token);
然后,我将返回的字符串粘贴到JWT.io中,它确认一切都很好(有效签名等等)。但是,当我在Postman中使用该令牌(它添加了头承载器+我的\u令牌\u字符串)时,调用会给我401个未经授权的消息

我在我的控制器中尝试了两种安全方法和一种开放方法(后者按预期工作)

我不知道我可能错过了什么。更糟糕的是,我不知道如何进一步调查

此时我能做什么?


建议设置标题。与我的轻松案例无关,没有观众的认可。真的,什么都不给。(只是确保表明我已经做了努力。)

要检查的一件事是Configure in Startup.cs中“use”语句的顺序。如果在app.UseAuthentication()之前有app.UseAuthentication(),则会得到401s。这一点我以前也有过:

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseCors("CorsPolicy");

        app.UseRouting();

        app.UseAuthentication(); //make sure this comes before app.UseAuthorization()
        app.UseAuthorization(); 
        app.UseHttpsRedirection();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
            endpoints.MapHub<NotificationHubService>("/notification");
        });
    }
public void配置(IApplicationBuilder应用程序,IWebHostEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
附录UseCors(“公司政策”);
app.UseRouting();
app.UseAuthentication();//请确保它位于app.UseAuthentication()之前
app.UseAuthorization();
app.UseHttpsRedirection();
app.UseEndpoints(端点=>
{
endpoints.MapControllers();
endpoints.MapHub(“/notification”);
});
}
[HttpGet("open"), AllowAnonymous]
public ActionResult OpenResult() { return Ok("Open result accessed."); }
[HttpGet("secure"), Authorize]
public ActionResult SecureResult() { return Ok("Secure result accessed."); }
[HttpGet("elevated"), Authorize(Roles = "elevated")]
public ActionResult ElevatedResult() { return Ok("Elevated result accessed."); }
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseCors("CorsPolicy");

        app.UseRouting();

        app.UseAuthentication(); //make sure this comes before app.UseAuthorization()
        app.UseAuthorization(); 
        app.UseHttpsRedirection();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
            endpoints.MapHub<NotificationHubService>("/notification");
        });
    }