Home ASP.NET Identity not being able to login
Reply: 0

ASP.NET Identity not being able to login

user3334
1#
user3334 Published in June 23, 2018, 5:30 pm

I am working to give an admin the option of adding new users to my website, using ASP.NET Identity.

My idea is that I have a link in my layout that redirects me to the account/register view

if (User.IsInRole(RoleName.IsAdmin))
{ //this is just for the admin
    <ul class="nav navbar-nav">                        
        //other links
        <li>@Html.ActionLink("Add assistant", "Register", "Account")</li>
    </ul>
}

This is my account/register action, where I add users to the role "IsAssistant".

[HttpPost]
[Authorize(Roles = RoleName.IsAdmin)]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
    if (ModelState.IsValid)
    {
        var user = new ApplicationUser { UserName = model.FirstName, Email = model.Email,kclusername=model.kclusername,
        FirstName=model.FirstName,LastName=model.LastName};
        var result = await UserManager.CreateAsync(user, model.Password);
        if (result.Succeeded)
        {
            var role = new RoleStore<IdentityRole>();
            var roleManager = new RoleManager<IdentityRole>(role);
            await roleManager.CreateAsync(new IdentityRole(RoleName.IsAssistant));
            await UserManager.AddToRoleAsync(user.Id, RoleName.IsAssistant);
            await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
            UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
            // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
            // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

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

    // If we got this far, something failed, redisplay form
    return View(model);
}

When I create a new user, I am redirected and sign in as the new user. But when I try to log in again, I can not, and I receive a message "invalid login attempt". I did not modify the login action:

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

    // This doesn't count login failures towards account lockout
    // To enable password failures to trigger account lockout, change to shouldLockout: true
    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("", "Invalid login attempt.");
            return View(model);
    }
}

What is wrong? Am I missing something? Thanks.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.305751 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO