Пример #1
0
        public async Task<IdentityResult> RegisterCustomerAsync(UserModel userModel, AppRole appRole = AppRole.customer)
        {
            var addToRoleResult = new IdentityResult();
            // Add generate username to the model
            IdentityUser user = new IdentityUser
            {
                UserName = userModel.UserName,
                Email = userModel.EmailAddress,
                EmailConfirmed = true,
                PhoneNumber = userModel.PhoneNumber,
                PhoneNumberConfirmed = true,
            };

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

            //Role-user combination is managed as Contact-Role combination in ContactRole table
            //Create Role Admin if it does not exist
            var _role = await _roleManager.FindByNameAsync(appRole.ToString());
            if (_role == null)
            {
                _role = new IdentityRole(appRole.ToString());
                var roleresult = await _roleManager.CreateAsync(_role);
            }
            var rolesForUser = await _userManager.GetRolesAsync(user.Id);
            if (!rolesForUser.Contains(_role.Name))
            {
                addToRoleResult = await _userManager.AddToRoleAsync(user.Id, _role.Name);
            }

            return addToRoleResult;
        }
Пример #2
0
        public async Task<IHttpActionResult> RegisterCustomer(UserModel userModel)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            var result = new IdentityResult();
            try
            {
                var userExistance = _repo.IsUserExistsByUsername(userModel.UserName);

                if (userExistance)
                {
                    return BadRequest("User Name already exist.");
                }
                   

                result = await _repo.RegisterCustomerAsync(userModel);

                var user = new User() 
                {
                   DateOfBirth = userModel.DateOfBirth,
                   FirstName = userModel.FirstName,
                   LastName = userModel.LastName,
                   Gender = (userModel.Gender == 1) ? Gender.male : Gender.female
                };
                UnitOfWork.UserRepository.Insert(user);
                UnitOfWork.SaveChange();
            }
            catch (Exception)
            {
                IHttpActionResult errorResult = GetErrorResult(result);

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

            return Ok();
        }
Пример #3
0
        //http://localhost:51473/api/Account/RegisterAdmin
//        {
//    "UserName": "******",
//    "Password": "******",
//    "ConfirmPassword": "******",
//    "EmailAddress": "*****@*****.**",
//    "PhoneNumber": "09911270918",
//    "FirstName": "test",
//    "LastName": "user",
//    "Gender": "1",
//    "DateOfBirth": "2015-08-01"
//}
        // POST api/Account/RegisterAdmin
        //[HttpPost]
        //[AllowAnonymous]
        //[Route("RegisterAdmin")]
        private async Task<IHttpActionResult> RegisterAdmin(UserModel userModel)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            var result = new IdentityResult();
            try
            {
                result = await _repo.RegisterCustomerAsync(userModel, AppRole.admin);

                var user = new User()
                {
                    //DateOfBirth = userModel.DateOfBirth,
                    FirstName = userModel.FirstName,
                    LastName = userModel.LastName,
                    Gender = (userModel.Gender == 1) ? Gender.male : Gender.female
                };
                UnitOfWork.UserRepository.Insert(user);
                UnitOfWork.SaveChange();
            }
            catch (Exception)
            {
                IHttpActionResult errorResult = GetErrorResult(result);

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

            return Ok();
        }