问题描述
我关注了这个将我的网站从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 Identity之后,我的身份验证停止了工作。
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>();
}
我有 app.UseAuthentication();
在我的Configure方法的底部。
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作为其
a网站) 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后,令牌身份验证停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!