1、Startup类的Configure方法中,
app.UseIdentity();
改为
app.UseCookieAuthentication(options => { options.AuthenticationScheme = CookieAuthenticationDefaults.AuthenticationScheme; options.AutomaticAuthenticate = true; options.AutomaticChallenge = true; options.CookieHttpOnly = true; });
2、登录的Action方法中:
1 List<Claim> claims = new List<Claim>(); 2 claims.Add(new Claim("Name", "TestName", ClaimValueTypes.String)); 3 claims.Add(new Claim(ClaimTypes.Name, "TestName", ClaimValueTypes.String)); 4 ClaimsIdentity identity = new ClaimsIdentity(claims, "AuthenticationType", "Name", ClaimTypes.Role); 5 ClaimsPrincipal principal = new ClaimsPrincipal(identity); 6 HttpContext.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal); 7 return View();
时间: 2024-10-26 21:25:48