令牌身份验证停止工作

令牌身份验证停止工作

本文介绍了从 ASP.NET Core 1 迁移到 ASP.NET Core 2 后,令牌身份验证停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我关注了这个网站 将我的网站从 ASP.NET Core 1 迁移到 ASP.NET Core 2.

I followed this site to migrate my website from ASP.NET Core 1 to ASP.NET Core 2.

但是在执行 ASP 身份更改后,我的身份验证停止工作.

However after doing the ASP Identity changes my authentication stopped working.

我的服务如下所示:

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton(_ => Configuration);

    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

    services.AddResponseCaching();

    services.AddIdentity<ApplicationUser, IdentityRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();

    var secretKey = Configuration.GetSection("AppSettings")["SecretKey"];
    var signingKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(secretKey));
    var tokenValidationParameters = new TokenValidationParameters
    {
        ValidateIssuerSigningKey = true,
        IssuerSigningKey = signingKey,
        ValidateIssuer = true,
        ValidIssuer = "arnvanhoutte",
        ValidateAudience = true,
        ValidAudience = "User",
        ValidateLifetime = true,
        ClockSkew = TimeSpan.Zero
    };

    services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
        .AddJwtBearer(options =>
        {
            options.TokenValidationParameters = tokenValidationParameters;
        });

    services.AddWebSocketManager();

    services.AddMvc();

    services.AddTransient<Seed>();
}

我的 Configure 方法底部有 app.UseAuthentication();.

And I have app.UseAuthentication(); at the bottom of my Configure method.

但是,当我在控制器中检查 var isAuthenticated = User.Identity.IsAuthenticated; 时,它总是说 false.虽然它以前有效,所以我不明白为什么这停止工作.

However when I check var isAuthenticated = User.Identity.IsAuthenticated; this in a controller it always says false. It worked before though so I don't understand why this stopped working.

推荐答案

我在迁移过程中遇到了类似的问题

I faced a similar issue during the migration

对我来说,登录没有任何问题,但随后的请求失败,重定向到登录页面而不是抛出 401(这导致 404 作为其一个 web api,我没有任何登录页面!).

For me the login worked without any issue,but subsequent request failed with a redirect to the login page instead of throwing a 401(which caused a 404 as its a web api i didn't had any login page!).

将 defaultauthenticationscheme 和 DefaultChallengeScheme 添加到 addauthentication 对我来说很有效.

修改addauthentication如下,让jwt认证生效!

Changing the addauthentication as follows to make the jwt authentication work!

services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(options =>
            {
                options.TokenValidationParameters = tokenValidationParameters
            });

这篇关于从 ASP.NET Core 1 迁移到 ASP.NET Core 2 后,令牌身份验证停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 07:57