CookieUtil.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. using Microsoft.AspNetCore.Authentication;
  2. using Microsoft.AspNetCore.Authentication.Cookies;
  3. using Microsoft.AspNetCore.Authentication.JwtBearer;
  4. using Microsoft.AspNetCore.Http;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Security.Claims;
  9. using System.Threading.Tasks;
  10. namespace ZR.Admin.WebApi.Framework
  11. {
  12. public class CookieUtil
  13. {
  14. public static void WhiteCookie(HttpContext context, List<Claim> claims)
  15. {
  16. //2.创建声明主题 指定认证方式 这里使用cookie
  17. var claimsIdentity = new ClaimsIdentity(claims, "Login");
  18. Task.Run(async () =>
  19. {
  20. await context.SignInAsync(
  21. JwtBearerDefaults.AuthenticationScheme,//这里要注意的是HttpContext.SignInAsync(AuthenticationType,…) 所设置的Scheme一定要与前面的配置一样,这样对应的登录授权才会生效。
  22. new ClaimsPrincipal(claimsIdentity),
  23. new AuthenticationProperties()
  24. {
  25. IsPersistent = true,
  26. AllowRefresh = true,
  27. ExpiresUtc = DateTimeOffset.Now.AddDays(1),//有效时间
  28. });
  29. }).Wait();
  30. }
  31. }
  32. }