在我的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) –