Example #1
0
        public async Task<IHttpActionResult> AddUser(UserViewModel m)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            var umm = new UserModel() { UserName = m.username, ShowUserName = m.showname, Password = m.password, ConfirmPassword = m.confirmpassword };

            IdentityResult result = await _repo.RegisterUser(umm);

            IHttpActionResult errorResult = GetErrorResult(result);

            if (errorResult != null)
            {
                return errorResult;
            }

            foreach (var r in m.roles)
            {
                bool result2 = await _repo.AddRoleToUser(umm, r);
                if (!result2)
                {
                    return NotFound();
                }
            }

            var um = await _upr.AddUserProfile(umm);
            if (um == null)
                return BadRequest("Не добавил профиль");

            return Ok();
        }
Example #2
0
        public async Task<IdentityResult> RegisterUser(UserModel userModel)
        {
            IdentityUser user = new IdentityUser
            {
                UserName = userModel.UserName.Trim()
            };

            var result = await _userManager.CreateAsync(user, userModel.Password);
            
            return result;
        }
Example #3
0
 public async Task<bool> AddRoleToUser(UserModel userModel, string roleName)
 {
     var u = await FindUser(userModel.UserName, userModel.Password);
     if (u!=null)
     {
         if (!_userManager.IsInRole(u.Id, roleName))
         {
             await _userManager.AddToRoleAsync(u.Id, roleName);
             return true;
         }
         return false;
     }
     else
     {
         return false;
     }
 }
Example #4
0
        public async Task<UserProfile> AddUserProfile(UserModel userModel)
        {
            var u = await _au.FindUser(userModel.UserName,userModel.Password);
            if (u != null)
            {
                var up = new UserProfile()
                {
                    userid = u.Id,
                    showname = userModel.ShowUserName.Trim()
                };

                var exist = GetProfileByUserId(u.Id);
                if (exist != null)
                    return null;
                else
                {
                    var p = _db.UserProfiles.Add(up);
                    _db.SaveChanges();
                    return p;
                }
            }
            else
                return null;
        }
Example #5
0
        public async Task<bool> UpdateUser(UserModel m,string userid,List<string> roles)
        {
            var u = await _userManager.FindByIdAsync(userid);
            if (u != null)
            {
                u.UserName = m.UserName.Trim();
                if (!String.IsNullOrEmpty(m.Password.Trim()) && m.Password.Trim() == m.ConfirmPassword.Trim())
                {
                    var r1 = await _userManager.RemovePasswordAsync(userid);
                    var r2 = await _userManager.AddPasswordAsync(userid, m.Password.Trim());
                    _ctx.SaveChanges();
                }

                foreach (var r in u.Roles.ToList())
                {
                    u.Roles.Remove(r);
                }
                _ctx.SaveChanges();

                var res = await _userManager.AddToRolesAsync(userid, roles.ToArray());
                _ctx.SaveChanges();
                return res.Succeeded;
            }
            else
                return false;
        }
Example #6
0
        public async Task<IHttpActionResult> Register(UserModel userModel)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            IdentityResult result = await _repo.RegisterUser(userModel);

            IHttpActionResult errorResult = GetErrorResult(result);

            if (errorResult != null)
            {
                return errorResult;
            }

            bool result2 = await _repo.AddRoleToUser(userModel, "user");
            if (!result2)
            {
                return NotFound();
            }

            var um = await _upr.AddUserProfile(userModel);
            if (um == null)
                return BadRequest("Не добавил профиль");

            return Ok();
        }