在我的Asp.Net核心網絡api我使用身份與Jwt承載身份驗證。它工作順利,沒有任何大驚小怪。以下是一個代碼,Dotnet核心2.0使用身份與JwtBearer身份驗證
ConfigureServices())
services.AddIdentity<ApplicationUser, IdentityRole<int>>()
.AddEntityFrameworkStores<DataContext, int>()
.AddDefaultTokenProviders();
配置(:我升級到.NET 2.0的核心
app.UseJwtBearerAuthentication(new JwtBearerOptions()
{
AutomaticAuthenticate = true,
AutomaticChallenge = true,
TokenValidationParameters = new TokenValidationParameters()
{
ValidIssuer = "localhost:4200",
ValidAudience = "localhost:4200",
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("SuperSecretKey_GetThisFromAppSettings")),
ValidateLifetime = true
}
});
,今日,整個技術疊加。從可用在那裏有限的幫助,我已經修改了代碼,這樣的..
ConfigureServices()
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<DataContext>()
.AddDefaultTokenProviders();
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.Authority = "localhost:4200";
options.Audience = "localhost:4200";
options.RequireHttpsMetadata = false;
options.TokenValidationParameters = new TokenValidationParameters()
{
ValidateIssuerSigningKey = true,
ValidateIssuer = true,
ValidateLifetime = true,
ValidIssuer = "localhost:4200",
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("SuperSecretKey_GetThisFromAppSettings"))
};
});
配置()
app.UseAuthentication();
現在認證是行不通的。看起來像它的內部配置使用Cookie身份驗證。
是否有其他人遇到過這種情況?任何對此的幫助真的很感激!
感謝,
嘿,成功了!並且非常感謝。但我還有一個問題。你知道如何在未經授權的訪問期間停止默認重定向到登錄頁面嗎?我不需要重定向,而需要將http狀態碼發送給客戶端。 – Wijitha
沒問題,我假設有更好的方法,但因爲您使用Identity和CookieAuthentication作爲第二個身份驗證方案,所以您可以使用登錄路徑,因爲它會回退到它。 services.ConfigureApplicationCookie(options => { options.LoginPath =「/ somecontr/somefunc」; });並返回你需要的任何東西。 public IActionResult somefunc()返回StatusCode(418) –