简体   繁体   中英

Can't login, but can register in ASP.NET MVC 5 app

After succesfully registering a random user, the app isn't logging them authomatically. I can't even login after typing in infos in defaulf Login View. It just directs me to Home/Index View and that's all. I've added AuthorizeAttribute in FilterConfig and an Admin Role

I tried to create plain new example ASP.NET MVC 5 app just to test the default logging, but it didn't work there either. As in previous app it just keeps directing me to Home/Index page

    // GET: /Account/Login
    [AllowAnonymous]
    public ActionResult Login(string returnUrl)
    {
        ViewBag.ReturnUrl = returnUrl;
        return View();
    }

    //
    // POST: /Account/Login
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
        switch (result)
        {

            case SignInStatus.Success:
                return RedirectToLocal(returnUrl);
            case SignInStatus.LockedOut:
                return View("Lockout");
            case SignInStatus.RequiresVerification:
                return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Nieprawidłowa próba logowania.");
                return View(model);
        }
    }

    [AllowAnonymous]
    public ActionResult Register()
    {
        return View();
    }

    //
    // POST: /Account/Register
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {

                //var roleStore = new RoleStore<IdentityRole>(new ApplicationDbContext());
                //var roleManager = new RoleManager<IdentityRole>(roleStore);
                //await roleManager.CreateAsync(new IdentityRole { Name = "Admin" });
                //await UserManager.AddToRoleAsync(user.Id, "Admin");

                await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);



                return RedirectToAction("Index", "Home");
            }
            AddErrors(result);
        }

        return View(model);
    }

I'd like to know how do I login the users successfully.

Looking at your code, someone would wonder how it is not throwing an error already or how a Build completed.

in your Account controller the Login [HttpPost], The line below is not there.

var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);

You need to have this line after declaring the variable

switch (result)
{
     //ommited block of code
}

In case someone experience this issue, make sure of the model view, it has to be matched (each property should be rendered in the view) unless if the property is defaulted. Along with checking properties types.

Next, you need to check the controller and the view, ensure that the number of passed properties is correct.

mismatched mapping would cause failure action.

Mostly this would solve the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM