Home > Enterprise >  C# login function implemented using SignInManager
C# login function implemented using SignInManager

Time:08-31

I am trying to implement a login function using the SignInManager I tried the below code Everytime the

result.Succeeded

is failed. I cannot figureout whats wrong. The model state is valid and the database connection is also working.

Controller

[HttpPost]
    

public async Task<IActionResult> Login(LoginViewModel model)
    {
        if (ModelState.IsValid)
        {
            var result = await _signInManager.PasswordSignInAsync(model.UserName,
                model.UserPassword, model.RememberMe,false);

            if (result.Succeeded)
            {
                if (Request.Query.Keys.Contains("ReturnUrl"))
                {
                    return Redirect(Request.Query["ReturnUrl"].First());
                }
                else
                {
                    return RedirectToAction("Shop", "Main");
                }
            }
        }
        else
        {
            ModelState.AddModelError("", "Failed to login");
        }
        return View();
    }

CodePudding user response:

You cannot directly insert a user record with a plain password in the User table. You have to do it via the ASP.NET Identity register user api. See enter image description here

result:

enter image description here

Read this to know more.

  • Related