asp.netcoreMVC中的所有身份验证选项是什么?
创始人
2024-09-18 02:30:25
0

asp.net core MVC提供了多种身份验证选项。以下是常见的身份验证选项及其对应的代码示例:

  1. Cookies认证: 使用Cookies-based认证系统,可以直接在代码中添加认证授权逻辑。示例代码如下:

// Startup.cs public void ConfigureServices(IServiceCollection services) { services.ConfigureApplicationCookie(options => { options.Cookie.HttpOnly = true; options.ExpireTimeSpan = TimeSpan.FromDays(30); options.LoginPath = "/Account/Login"; options.AccessDeniedPath = "/Account/AccessDenied"; options.SlidingExpiration = true; });

// ...

}

// AccountController.cs public async Task Login(LoginViewModel model, string returnUrl = null) { // ...

// Issue authentication cookie with custom claims
var claims = new List
{
    new Claim(ClaimTypes.Name, user.FullName),
    new Claim(ClaimTypes.Email, user.Email)
    // ...
};
var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
var authProperties = new AuthenticationProperties
{
    IsPersistent = model.RememberMe,
    ExpiresUtc = DateTimeOffset.UtcNow.AddHours(1),
    AllowRefresh = true
};
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), authProperties);

// ...

}

  1. JWT认证: 使用基于JSON web tokens的身份验证系统,可以添加授权令牌以及进行身份验证。示例代码如下:

// Startup.cs public void ConfigureServices(IServiceCollection services) { services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidateIssuerSigningKey = true, ValidIssuer = "mydomain.com", ValidAudience = "mydomain.com", IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["JwtKey"])) };

相关内容

热门资讯

透视模拟器(wepOkE)透视... 透视模拟器(wepOkE)透视辅助工具(wepoke插件)真是真的是有挂(详细透视AI教程);玩家必...
透视辅助(微扑克全自动机器人)... 透视辅助(微扑克全自动机器人)线上wpk德州ai机器人(详细辅助解密教程)固有真的是有挂1、很好的工...
微扑克wpk透视辅助!来玩ap... 这是一款非常优秀的微扑克wpk透视辅助 ia辅助检测软件,能够让你了解到微扑克wpk透视辅助中牌率当...
透视神器(智星德州菠萝辅助器)... 透视神器(智星德州菠萝辅助器)gg扑克有假吗(详细辅助普及教程)起初是真的有挂1、在智星德州菠萝辅助...
透视安装(WEPOke)透明挂... 透视安装(WEPOke)透明挂辅助挂(wepoke辅助挂)真是是真的有挂(详细透视科技教程);wep...
德扑之星辅助器购买!微扑克智能... 《德扑之星辅助器购买!微扑克智能助手,(来玩德州app)好像真的是有挂(详细辅助新2025版)》 德...
透视辅助(aapoker外挂)... 透视辅助(aapoker外挂)菠萝德州偷偷看功能(详细辅助透视教程)切实是有挂;透视辅助(aapok...
透视透视(wepOke)透明挂... 1、透视透视(wepOke)透明挂辅助神器(wepoke有没有挂)往昔有挂(详细透视力荐教程)(UU...
微扑克辅助挂!wpk开挂实锤,... 微扑克辅助挂!wpk开挂实锤,(aa poker)果然真的有挂(详细透视透视教程);1分钟了解详细教...
透视科技(微扑克wpk透视辅助... 透视科技(微扑克wpk透视辅助)wepoke软件能玩吗(详细辅助2025新版总结)原来真的有挂是一款...