【问题标题】:Token Authentication stopped working after migration from ASP.NET Core 1 to ASP.NET Core 2从 ASP.NET Core 1 迁移到 ASP.NET Core 2 后,令牌身份验证停止工作
【发布时间】:2018-01-31 03:45:27
【问题描述】:

我按照site 将我的网站从 ASP.NET Core 1 迁移到 ASP.NET Core 2。

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

我的服务如下所示:

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

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

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

【问题讨论】:

  • 您是否再次在您的数据库上运行迁移?
  • 我现在试过了,但我一直收到这个错误:Unable to create an object of type 'ApplicationDbContext'. Add an implementation of 'IDesignTimeDbContextFactory&lt;ApplicationDbContext&gt;' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time.
  • 有趣,这可能是您的上下文不起作用的原因,也许您正在生成旧版本的 ApplicationDbContext,是不是有一个与 .Net-Core-2.0 相关联的新版本我仍然没有t 已升级,因为 IdentityServer4 需要升级
  • 在升级到无参数构造函数后,我现在设法进行了迁移。但我的主张仍然无效
  • 是重定向到404还是抛出401进行身份验证!

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


【解决方案1】:

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

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

将 defaultauthenticationscheme 和 DefaultChallengeScheme 添加到 addauthentication 对我有用。

如下更改 addauthentication 以使 jwt 身份验证工作!

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-13
    • 2021-12-17
    • 1970-01-01
    • 2022-11-12
    • 2015-05-16
    • 1970-01-01
    • 2018-02-23
    • 1970-01-01
    相关资源
    最近更新 更多