Esempio n. 1
0
//        [Authorize(Roles = "Admin")]
        public async Task <IActionResult> AddUser([FromBody] UserRegisterTransmitModel model)
        {
            model.Locale = this.Request.Headers["Accept-Language"].ToString().Split(",")[0];
            model.ApplicationRoleName = "User";
            if (!ModelState.IsValid || !await _dataAccess.AddUser(model))
            {
                return(BadRequest("Invalid user definition"));
            }

            return(Ok());
        }
Esempio n. 2
0
        public async Task <bool> AddUser(UserRegisterTransmitModel model)
        {
            ApplicationUser user = new ApplicationUser
            {
                UserName      = model.UserName,
                Email         = model.Email,
                PhoneNumber   = model.PhoneNumber,
                Locale        = model.Locale,
                SecurityStamp = Guid.NewGuid().ToString()
            };

            IdentityResult createResult = await _userManager.CreateAsync(user, model.Password);

            IdentityResult addClaimResult =
                await _userManager.AddClaimAsync(user, new Claim(ClaimTypes.Role, "User"));

            if (createResult.Succeeded && addClaimResult.Succeeded)
            {
                ApplicationRole applicationRole = await _roleManager.FindByNameAsync("User");

                if (applicationRole != null)
                {
                    throw new ArgumentException($"Not find role User", "original");
                }

                IdentityResult roleResult = await _userManager.AddToRoleAsync(user, applicationRole.Name);

                if (roleResult.Succeeded)
                {
                    return(true);
                }
            }

//            await _userManager.DeleteAsync(user);
            return(false);
        }