Ashok Kumar Bhattarai Ответов: 0

Я создал пользователей и назначил одну роль для каждого пользователя, используя следующее: Я должен обновить userinfo и роль, оставаясь в admin user


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);


Что я уже пробовал:

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.

0 Ответов