【发布时间】: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<ApplicationDbContext>' 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进行身份验证!