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

ASP.NET Identity not being able to login

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>

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

[Authorize(Roles = RoleName.IsAdmin)]
public async Task<ActionResult> Register(RegisterViewModel model)
    if (ModelState.IsValid)
        var user = new ApplicationUser { UserName = model.FirstName, Email = model.Email,kclusername=model.kclusername,
        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);
            // 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");

    // 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:

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:
            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