【问题标题】:ASP.NET Core 2 Authorize attribute jwtASP.NET Core 2 授权属性 jwt
【发布时间】:2018-08-19 07:48:57
【问题描述】:

请告诉我为什么这段代码不起作用。

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

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddAuthentication(options =>
        {
            options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
            options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            options.DefaultSignInScheme = JwtBearerDefaults.AuthenticationScheme;
        }).AddJwtBearer(options =>
        {
            options.RequireHttpsMetadata = false;
            options.TokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuer = true,
                ValidIssuer = AuthOptions.ISSUER,
                ValidateAudience = true,
                ValidAudience = AuthOptions.AUDIENCE,
                ValidateLifetime = true,
                ValidateIssuerSigningKey = true,
                IssuerSigningKey = AuthOptions.GetSymmetricSecurityKey()
            };
        });

        services.AddDbContext<ApplicationContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        services.AddIdentity<User, IdentityRole>().AddEntityFrameworkStores<ApplicationContext>();
        services.AddMvc();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseAuthentication();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller}/{action}/{id?}",
                defaults: new { controller = "Home", action = "Index" });
        });
    }
}

我尝试删除 options.Default* 并将其替换为仅 JwtBearerDefaults.AuthenticationScheme。仅当我将 [Authorize] 更改为 [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] 时它才有效。但我不想为每个属性使用AuthenticationSchemes 属性。

【问题讨论】:

  • 您的 Startup 中有其他身份验证或身份相关的东西吗?
  • @poke 我更新了我的代码。请检查

标签: c# asp.net-core jwt asp.net-core-identity


【解决方案1】:

services.AddIdentity(…) 设置 ASP.NET Core 身份,该身份使用基于表单的身份登录工作所需的 Cookie 身份验证注册多个 Cookie 身份验证方案。

作为其中的一部分,它还将默认身份验证和质询方案设置为IdentityConstants.ApplicationScheme

由于您调用AddIdentity 之后 AddAuthentication,您为后者所做的默认配置将被身份配置覆盖。因此,要解决您的问题,您必须确保在 注册身份之后在身份选项中设置默认方案。

services.AddIdentity<User, IdentityRole>()
    .AddEntityFrameworkStores<ApplicationContext>();;

services.AddAuthentication(options =>
{
    options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultSignInScheme = JwtBearerDefaults.AuthenticationScheme;
})
    .AddJwtBearer(…);

请注意,这将显然停止 ASP.NET Core Identity 的身份验证 cookie 成为默认身份验证和质询方案。因此,如果您的应用程序有没有使用 JWT Bearer 的区域,那么这些区域将停止工作,并且需要显式的 Authenticate 属性才能切换回身份 cookie。

【讨论】:

    猜你喜欢
    • 2021-10-05
    • 1970-01-01
    • 2020-07-21
    • 1970-01-01
    • 1970-01-01
    • 2018-10-30
    • 1970-01-01
    • 1970-01-01
    • 2020-04-11
    相关资源
    最近更新 更多