Click here to Skip to main content
14,696,499 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created users and assigned single role for each user using the below: I have to update the userinfo and role staying at admin user.

            var userDet = new ApplicationUser
        {

            Email = model.Email,
            UserName = model.Email,
            TenantId = tenantId,
            IsAdmin = false


        };

        var result = await _userManager.CreateAsync(userDet, model.Password);

        if (model.Role == "ADMIN")
            throw new NullReferenceException("Can't Add Admin Role to User!");

        var assignRole = await _userManager.AddToRoleAsync(userDet, model.Role);


What I have tried:

I have created users and assigned single role for each user using the below: I have to update the userinfo and role staying at admin user.

            var userDet = new ApplicationUser
        {

            Email = model.Email,
            UserName = model.Email,
            TenantId = tenantId,
            IsAdmin = false


        };

        var result = await _userManager.CreateAsync(userDet, model.Password);

        if (model.Role == "ADMIN")
            throw new NullReferenceException("Can't Add Admin Role to User!");

        var assignRole = await _userManager.AddToRoleAsync(userDet, model.Role);
But I am unable to update the created users role as i can't get the roleid and rolename from userid from the below code:

        var user = await _userManager.FindByEmailAsync(model.Email);
        var admin = await _userManager.FindByEmailAsync(model.AdminEmail);
        bool IsAdmin = admin.IsAdmin;

        var userDet = new ApplicationUser
        {

            OrganizationName = model.OrganizationName,
        };

        var result = await _userManager.UpdateAsync(userDet);

        var user = await _userManager.FindAsync(model.Email);
        string oldRoleName = await _userManager.GetRoles(user.Id).FirstOrDefault();
        var removerole= await _roleManager.RemoveRoleAsync(user.Id, oldRoleName);
        var result2 = await _userManager.AddToRoleAsync(user.Id, model.Role);
I am unable to retrive RoleName, roleid from the above code. How can i update the user based on id of model.Email user from another user.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900