简体   繁体   English

从ASP.NET Core 1迁移到ASP.NET Core 2后,令牌身份验证停止工作

[英]Token Authentication stopped working after migration from ASP.NET Core 1 to ASP.NET Core 2

I followed this site to migrate my website from ASP.NET Core 1 to ASP.NET Core 2. 我按照此站点将我的网站从ASP.NET Core 1迁移到ASP.NET Core 2。

However after doing the ASP Identity changes my authentication stopped working. 但是,在执行ASP身份更改后,我的身份验证停止工作。

My services look like this: 我的服务看起来像这样:

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>();
}

And I have app.UseAuthentication(); 我有app.UseAuthentication(); at the bottom of my Configure method. 在我的Configure方法的底部。

However when I check var isAuthenticated = User.Identity.IsAuthenticated; 但是,当我检查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 我在迁移过程中遇到了类似的问题

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!). 对我来说,登录工作没有任何问题,但后续请求失败,重定向到登录页面而不是抛出401(这导致404作为它的web api我没有任何登录页面!)。

Adding the defaultauthenticationscheme and DefaultChallengeScheme to the addauthentication did the trick for me. 将defaultauthenticationscheme和DefaultChallengeScheme添加到addauthentication为我做了诀窍。

Changing the addauthentication as follows to make the jwt authentication work! 按如下所示更改addauthentication以使jwt身份验证正常工作!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM