在ASP.NET Core中,使用身份验证可以实现用户登录和访问授权的功能。如果无法登录用户,可能是由于以下几个常见问题:
public void ConfigureServices(IServiceCollection services)
{
    // 添加身份验证服务
    services.AddAuthentication(options =>
    {
        options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    })
    .AddCookie(options =>
    {
        options.LoginPath = "/Account/Login"; // 登录页面的路径
        options.AccessDeniedPath = "/Account/AccessDenied"; // 拒绝访问页面的路径
    });
    // 添加授权策略
    services.AddAuthorization();
    // 其他服务配置...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    // 其他中间件配置...
    // 启用身份验证中间件
    app.UseAuthentication();
    // 启用授权中间件
    app.UseAuthorization();
    // 其他中间件配置...
}
[HttpPost]
public async Task Login(LoginViewModel model)
{
    if (ModelState.IsValid)
    {
        var user = await _userManager.FindByNameAsync(model.UserName);
        if (user != null && await _userManager.CheckPasswordAsync(user, model.Password))
        {
            // 登录成功,执行登录操作
            var claims = new List
            {
                new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
                new Claim(ClaimTypes.Name, user.UserName)
            };
            var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
            await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity));
            return RedirectToAction("Index", "Home");
        }
        ModelState.AddModelError(string.Empty, "无效的用户名或密码");
    }
    return View(model);
}
  [Authorize(Roles = "Admin")]
public IActionResult AdminPage()
{
    // 只有拥有 "Admin" 角色的用户才能访问该页面
    return View();
}
通过检查以上几个方面,您应该能够解决ASP.NET Core身份验证中无法登录用户的问题。