在开发过程中,使用“test”声明信息自动登录用户如何?例如,假设在非开发环境中,您使用以下内容来授权用户:
identity = new ClaimsIdentity(new[] {
new Claim(ClaimTypes.Name, userList.fullname),
new Claim(ClaimTypes.Role, userList.userrole),
new Claim(ClaimTypes.NameIdentifier, userEmail),
}, CookieAuthenticationDefaults.AuthenticationScheme);
var principal = new ClaimsPrincipal(identity);
var login = HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);
return RedirectToAction("Index", "Home");
当你在开发时,可以做如下操作:
if (env.EnvironmentName == "Development")
{
identity = new ClaimsIdentity(new[] {
new Claim(ClaimTypes.Name, "Test User"),
new Claim(ClaimTypes.Role, "Tester"),
new Claim(ClaimTypes.NameIdentifier, "tester@test.com"),
}, CookieAuthenticationDefaults.AuthenticationScheme);
HttpContext.Session.SetString(SessionUserName, userList.fullname);
var principal = new ClaimsPrincipal(identity);
var login = HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);
return RedirectToAction("Index", "Home");
}
services.AddMvc()
在 .net core 3.0 中属于MvcServiceCollectionExtensions
扩展。您可以使用与.net core 2
相同的方法来实现所需功能。 - Venkata DorisalaAddMvc
模式设置。即使它能够工作,我也会感到不舒服,因为AddMvc
似乎不再是推荐的模式,并且在这种情况下已被AddControllers
取代。 - stellr42[AllowAnonymous]
过滤器应用作为简化开发的方式实际上并不是一个好主意,因为您在未提供用户实际认证时跳过了应用程序的重要部分。因此,最好的做法是登录一个包含所需声明的合成用户,并使授权按预期工作。 - poke