【问题标题】:Bearer Token Authentication in ASP.NET CoreASP.NET Core 中的不记名令牌身份验证
【发布时间】:2016-06-08 16:40:07
【问题描述】:

尝试在简单的 .Net Core Web API 项目中使用基于不记名令牌的身份验证。这是我的Startup.cs

app.UseMvc();
//---
const string secretKey = "mysupersecret_secretkey!123";
SymmetricSecurityKey signingKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey));
SigningCredentials signingCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256);
//---
const string audience = "Audience";
const string issuer = "Issuer";
//---
TokenValidationParameters tokenValidationParameters = new TokenValidationParameters
{
    ValidateIssuerSigningKey = true,
    IssuerSigningKey = signingKey,

    ValidateIssuer = false,
    ValidIssuer = issuer,

    ValidateAudience = true,
    ValidAudience = audience,

    ValidateLifetime = true,

    ClockSkew = TimeSpan.Zero,
    AuthenticationType = JwtBearerDefaults.AuthenticationScheme
};
//---
app.UseJwtBearerAuthentication(new JwtBearerOptions
{
    AutomaticAuthenticate = true,
    AutomaticChallenge = true,
    TokenValidationParameters = tokenValidationParameters,
    AuthenticationScheme = JwtBearerDefaults.AuthenticationScheme,
});

我还将AuthorizeAttribute 添加到控制器操作中

[HttpGet]
[Authorize(ActiveAuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public IEnumerable<string> Get()
{
    return new[] { "value1", "value2" };
}

但是当尝试发送带有标头的获取请求时 Authorization: Bearer [TOKEN] 我得到异常

System.InvalidOperationException: No authentication handler is configured to authenticate for the scheme: Bearer
   at Microsoft.AspNetCore.Http.Authentication.Internal.DefaultAuthenticationManager.

那么这个“身份验证处理程序”是什么?我需要在哪里设置这个处理程序?

【问题讨论】:

标签: c# asp.net-core


【解决方案1】:

在 ASP.NET Core 中,中间件的顺序很重要:它们的执行顺序与注册顺序相同。这里app.UseMvc()是在JWT承载中间件之前调用的,所以这行不通。

app.UseMvc() 放在管道的末尾,它应该可以工作:

app.UseJwtBearerAuthentication(new JwtBearerOptions
{
    AutomaticAuthenticate = true,
    AutomaticChallenge = true,
    TokenValidationParameters = tokenValidationParameters,
    AuthenticationScheme = JwtBearerDefaults.AuthenticationScheme,
});

app.UseMvc();

【讨论】:

  • 这不再有效,因为它已经过时了。我仍在寻找解决方案。
【解决方案2】:

对于 .NET Core 3.0,您需要:

ConfigureServices(IServiceCollection services):

services.AddAuthentication()
    .AddJwtBearer(options =>
    {
        options.Authority = issuer;
        options.Audience  = audience;
        options.TokenValidationParameters = tokenValidationParameters;
    });

Configure(IApplicationBuilder app, IWebHostEnvironment env):

// Add it after app.UseRouting() and before app.UseEndpoints()! 
// Order of middlewares is important!
app.UseAuthentication();
app.UseAuthorization();

PS:要省略[Authorize] 属性中的身份验证方案指示,您可以在AuthenticationOptions 选项中设置ConfigureServices(IServiceCollection services) 中的默认身份验证方案:

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

【讨论】:

    猜你喜欢
    • 2021-04-29
    • 2021-12-17
    • 2015-05-16
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    • 2015-08-13
    • 2017-05-19
    • 2017-10-26
    相关资源
    最近更新 更多