[英]HttpContext.SignInAsync vs. SigninManger.SignInAsync
[英]HttpContext SignInAsync fail with Cookies and Claims
我有一个 ASP.NET CORE 应用程序,我尝试添加一个使用 Cookie 进行身份验证的登录。
我有以下代码
启动文件
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication("CookieAuth")
.AddCookie("CookieAuth", config =>
{
config.Cookie.Name = "GroupHome.Cookie";
config.LoginPath = "/Account/SignIn";
});
...
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
...
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
...
登录.cs
public IActionResult Login(LoginModel lm)
{
using (var ctx = new GroupHomeContext())
{
try {
var user = ctx.Logins.SingleOrDefault(er => er.UserName == lm.UserName);
if (Decrypt(user.Password) == lm.Password)
{
var employee = ctx.Employees.SingleOrDefault(em => em.EmployeeId == user.EmployeeId);
var employeeRole = ctx.EmployeeToRoles.SingleOrDefault(er => er.EmployeeId == employee.EmployeeId);
var Role = ctx.Roles.SingleOrDefault(rl => rl.RoleId == employeeRole.RoleId);
var GroupHomeClaims = new List<Claim>() { new Claim(ClaimTypes.NameIdentifier, employee.EmployeeId.ToString()), new Claim(ClaimTypes.Email, employee.Email), new Claim(ClaimTypes.Role, employeeRole.RoleId.ToString()) };
var GroupHomeIdentity = new ClaimsIdentity(GroupHomeClaims, "Group Home Identity");
var userPrincipal = new ClaimsPrincipal(new[] { GroupHomeIdentity });
if (userPrincipal != null)
{
HttpContext.SignInAsync(userPrincipal); <----- ERROR IS HERE
return RedirectToAction("/Dashboard/Employees");
}
return RedirectToAction("/Account/SignIn");
}
return RedirectToAction("/Account/SignIn");
}
catch (Exception ex)
{
return RedirectToAction("/Account/SignIn");
}
}
}
并且当用户登录所有数据库验证过程都可以,但是当我尝试将 ClaimsPrincipal 设置为 HttpContext.SignInAsync() 时出现以下错误。
您正在调用异步方法SignInAsync ,我想您也应该等待它并使方法异步。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.