Exemple #1
0
        public IdentityResult UpdateUserInfo(string id, string username, string fullName, string phone, string address, string email, string password, string role)
        {
            var user = userManager.FindById(id);

            user.UserName     = username;
            user.Email        = email;
            user.PasswordHash = userManager.PasswordHasher.HashPassword(password);
            var oldRoleId   = user.Roles.SingleOrDefault().RoleId;
            var oldRoleName = roleManager.Roles.SingleOrDefault(r => r.Id == oldRoleId).Name;

            if (oldRoleName != role)
            {
                userManager.RemoveFromRole(user.Id, oldRoleName);
                userManager.AddToRole(user.Id, role);
            }
            var result = userManager.Update(user);

            var personalUserInfo = new PersonalUserInfo
            {
                UserId      = id,
                FullName    = fullName,
                PhoneNumber = phone,
                Address     = address
            };
            var personalUserInfoToUpdate = _context.PersonalUserInfos.FirstOrDefault(p => p.UserId == id);

            if (personalUserInfoToUpdate != null)
            {
                _context.Entry(personalUserInfoToUpdate).CurrentValues.SetValues(personalUserInfo);
                _context.SaveChanges();
            }
            return(result);
        }
Exemple #2
0
        public IdentityResult CreateUser(string username, string fullName, string phone, string address, string email, string password, string role = nameof(RoleType.User))
        {
            var user = new IdentityUser()
            {
                UserName = username, Email = email
            };
            IdentityResult result = userManager.Create(user, password);

            if (result.Succeeded)
            {
                var addRoleResult  = userManager.AddToRole(user.Id, role);
                var personUserInfo = new PersonalUserInfo()
                {
                    UserId = user.Id, FullName = fullName, PhoneNumber = phone, Address = address
                };
                _context.PersonalUserInfos.Add(personUserInfo);
                _context.SaveChanges();
                return(addRoleResult);
            }
            return(result);
        }