1.NuGet引用
IdentityServer4.AccessTokenValidation
2.Startup.cs注册
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) .AddCookie(options => { options.ExpireTimeSpan = TimeSpan.FromMinutes(60); //到期时间 options.Cookie.Name = "lym.Cookies"; //名称 });
3.注册中件间
app.UseAuthentication();
4.使用细节
var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);//获取实例 identity.AddClaim(new Claim(ClaimTypes.Sid, "Sid"));//添加 identity.AddClaim(new Claim(ClaimTypes.Name, "Name"));//添加 await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity));//保存 var sid = HttpContext.User.Claims.SingleOrDefault(s => s.Type == ClaimTypes.Sid);//获取值 var name = HttpContext.User.Claims.SingleOrDefault(s => s.Type == ClaimTypes.Name);