locked
Getting trouble while assigning user against previously created role in Microsoft.AspNetCore.Identity tables RRS feed

  • Question

  • User-1225773522 posted

    Hi Everyone,

    I am working on Asp.net core 2.1 MVC application, where I am authenticating internal AD users by Ldap and authentication is working fine.

    My second requirement was to add successful authenticated users into Microsoft default identity table AspNetUsers for role based authorization management, and this also work fine.

    Now, While adding resource against previously created role, I am facing issue, it just returns succeeded =false and does not assign user, please see below error description.

    addUserTorole = {Failed : DuplicateUserName,DuplicateEmail} {Microsoft.AspNetCore.Identity.IdentityError}

    0 - Description = "User name 'abc123' is already taken."

    1- Description = "Email 'abc@gmail.com' is already taken."

    Whereas I know there no record inside dbo.AspNetUserRoles table, and just one record with User name 'abc123' with said Email in stored in dbo.AspNetUsers table.

     public async Task<IActionResult> EmployeeLogin(LoginViewModel loginViewModel)
            {
    
                if (!ModelState.IsValid)
                    return View(loginViewModel);
                var user = await _userManager.FindByLdapNameAsync(loginViewModel.UserName);
                if (user != null)
                {
                    try
                    {                   
                        var result = await _signInManager.PasswordSignInAsync(user, loginViewModel.Password, false, false);
                        if (result.Succeeded)
                        {
                            var findUser = await _userManager.FindByNameAsync(loginViewModel.UserName);
                            if(findUser ==null)
                            {   // For storing users into indentity databse                        
                                var createIdenity = await _userManager.CreateAsync(user, "Welcome$123");                           
                            }
                            var addUserTorole = await _userManager.AddToRoleAsync(user, "Administrators"); 
                            if (addUserTorole.Succeeded)
                            { }
    
                            if (string.IsNullOrEmpty(loginViewModel.ReturnUrl))
                            {
                                 return RedirectToAction("Index", "Customer");
                            }        
                            return Redirect(loginViewModel.ReturnUrl);
                        }
                    }
                    catch (Exception e) { }
                }
    
                ModelState.AddModelError("", "Username/password not found");
                return View(loginViewModel);
            }
           

    Please help me out where i am doing mistake.

    Thanks,

    Sonu

    Saturday, October 27, 2018 6:57 AM

All replies

  • User475983607 posted

    It looks like you or someone on your team created a custom User Manager.  If we assume the error is correct, then the code is trying to insert a duplicate record.  You'll need to take a closer look at the custom code.

    Taking a closer look at the code shows questionable logic.  There is an empty catch block which is always bad because it hides the exception message.  

    The code as written code tries tries to sign in the user after validating the user exists in LDAP.  If successful it assigns the user to an admin role which does not make sense, IMHO.

    I recommend that you take a step back and try running the code through the Visual Studio debugger to debug the custom User Manager logic.

    Sunday, October 28, 2018 2:50 PM