0

我有一个新的 asp.net MVC 核心 6 应用程序。尝试对用户进行身份验证(而不是使用身份脚手架)。但是 SignInmanger 始终返回 False

登录功能

在此处输入图像描述

程序.cs

在此处输入图像描述

登录的完整代码片段:

[HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Login(LoginVM loginvm)
    { // this wil return view model


        if (!ModelState.IsValid)
        {
            return View(loginvm);
        }


        var user = await _userManager.FindByEmailAsync(loginvm.Username);

        if (user != null)
        {
            // if we have user let us check the password

            var checkpsssword = await _userManager.CheckPasswordAsync(user, loginvm.Password);

            if (checkpsssword)
            {
                var letUserLoginIn = await _signInManager.PasswordSignInAsync(user, loginvm.Password, false, false);

                if (letUserLoginIn.Succeeded)
                {
                    var tempo = User.Identity.IsAuthenticated;

                    var isok = _signInManager.IsSignedIn(User);
                    ViewBag.tempo=tempo;
                    ViewBag.isok = isok;

                  return  RedirectToAction("index", "Movie");
                }
                
                ModelState.AddModelError("Error","can login innnnn");
                TempData["Error"] = "Password is not correct! !";
                return View(loginvm);
            }
            else
            {
                // password wrong
                TempData["Error"] = "Password is not correct! !";
            }

        }

        TempData["Error"] = "no user found ya mozznoz!";
        return View(loginvm);//STRONGLY TYPED VIEW 


    }
4

1 回答 1

0

@Kevin 上面提到了一部分,另一部分是缺少身份验证机制寄存器。

它应该是这样的builder.Services.AddAuthentication(opts => opts.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();

AddAuthentication部分添加所有必要的中间件和配置以设置身份验证过程。在这里,我们指定DefaultSchemeCookieAuthenticationDefaults.AuthenticationScheme

AddCookie告诉 asp.net Core 我们希望将登录信息存储在 cookie 中,因此,发送了一个响应,告诉客户端使用预定义的信息保存一个 cookie(并且选择的身份验证机制的名称默认为CookieAuthenticationDefaults.AuthenticationScheme)。

对于每个后续请求,都包含 cookie,然后服务器知道,我们已经登录

于 2022-01-04T08:08:33.153 回答