Beispiel #1
0
        public async Task<IHttpActionResult> RegisterUser(UserModel userModel)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            IdentityResult result = await _repo.RegisterUser(userModel, "User");

            IHttpActionResult errorResult = GetErrorResult(result);

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

            return Ok();
        }
Beispiel #2
-1
        public async Task<IdentityResult> RegisterUser(UserModel userModel, string roleName)
        {
            IdentityUser user = new IdentityUser
            {
                UserName = userModel.UserName,
                Email = userModel.UserName
            };

            var result = await _userManager.CreateAsync(user, userModel.Password);

            if (result == IdentityResult.Success)
            {
                var role = await _roleManager.FindByNameAsync(roleName);
                if (role == null)
                {
                    role = new IdentityRole(roleName);
                    var roleresult = await _roleManager.CreateAsync(role);
                }

                var userFromDb = await _userManager.FindByNameAsync(user.UserName);

                if (userFromDb != null)
                {
                    await _userManager.AddToRoleAsync(userFromDb.Id, roleName);
                }
            }

            return result;
        }