kaj*_*123 4 asp.net asp.net-mvc jwt asp.net-core cookie-authentication
我听说这是存储 JWT 最安全的方式之一。我的问题是。我怎样才能将它保存在cookie中?
这是Startup.cs中ConfigureServises中的函数
services.AddControllers();
services.AddTransient<IUserRepository, UserRepository>();
services.AddTransient<ITokenService, TokenService>();
IdentityModelEventSource.ShowPII = true;
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = Configuration["Jwt:Issuer"],
ValidAudience = Configuration["Jwt:Issuer"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Jwt:Key"]))
};
});
Run Code Online (Sandbox Code Playgroud)
小智 8
传递给 AddJwtBearer 的选项对象包含其自己的 Events 属性,该属性允许您自定义流程的各个部分。您需要使用 MessageReceived Event 来实现相同的目的
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options => {
options.Events = new JwtBearerEvents
{
OnMessageReceived = context =>
{
context.Token = context.Request.Cookies["CookieName"];
return Task.CompletedTask;
}
};
});
}
Run Code Online (Sandbox Code Playgroud)
在 asp.net core 中使用 HTTP cookie
| 归档时间: |
|
| 查看次数: |
5694 次 |
| 最近记录: |