services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) .AddCookie(options => { options.LoginPath = new PathString("/Account/Login/"); options.AccessDeniedPath = new PathString("/Account/Forbidden/"); options.ExpireTimeSpan = TimeSpan.FromMinutes(15); options.SlidingExpiration = true; });
var claims = new[] { new Claim(ClaimTypes.Name, "testuser"), new Claim(ClaimTypes.Email, "testuser@example.com"), new Claim(ClaimTypes.Role, "admin"), };
var userIdentity = new ClaimsIdentity(claims, "TestAuth"); var userPrincipal = new ClaimsPrincipal(userIdentity);
await HttpContext.SignInAsync( CookieAuthenticationDefaults.AuthenticationScheme, userPrincipal);
if (User.Identity.IsAuthenticated) { // 模拟登录成功,执行相关操作 } else { // 模拟登录失败 }