Ejemplo n.º 1
0
        public async Task <IActionResult> Register([FromBody] UserPostRequest model)
        {
            var createdUser = await this.CreateUser(model).ConfigureAwait(false);

            await this.signInManager.SignInAsync(createdUser, true).ConfigureAwait(false);

            IList <Claim> claims = await this.userManager.GetClaimsAsync(createdUser).ConfigureAwait(false);

            var roleNames = await this.userManager.GetRolesAsync(createdUser).ConfigureAwait(false);

            var           roles = this.roleManager.Roles.Where(r => roleNames.Contains(r.Name)).ToArray();
            IList <Claim> roleClaims;

            foreach (var role in roles)
            {
                roleClaims = await this.roleManager.GetClaimsAsync(role).ConfigureAwait(false);

                foreach (var roleClaim in roleClaims)
                {
                    claims.Add(roleClaim);
                }
            }

            var token = this.tokenHelper.BuildToken(claims);

            return(this.Ok(new LoginResponse(token, claims)));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Post([FromBody] UserPostRequest user)
        {
            if (!this.ModelState.IsValid)
            {
                throw new OneFrameWebException((int)HttpStatusCode.BadRequest, this.localize["InvalidModel"]);
            }

            var createdUser = await this.CreateUser(user).ConfigureAwait(false);

            return(this.CreatedAtRoute("UserGet", new { username = createdUser.UserName }, new { id = createdUser.Id, username = createdUser.UserName }));
        }
Ejemplo n.º 3
0
        private async Task <ApplicationUser> CreateUser(UserPostRequest userPostRequest)
        {
            var user = new ApplicationUser()
            {
                UserName = userPostRequest.Email, Email = userPostRequest.Email, PhoneNumber = userPostRequest.PhoneNumber
            };
            var result = await this.userManager.CreateAsync(user, userPostRequest.Password).ConfigureAwait(false);

            if (!result.Succeeded)
            {
                var errorSummary = new ErrorSummary(this.localize["UnableToCreateUser"]);
                foreach (var er in result.Errors)
                {
                    errorSummary.Items.Add(new ErrorItem(this.localize[er.Code]));
                }

                throw new OneFrameWebException((int)HttpStatusCode.BadRequest, errorSummary);
            }

            return(user);
        }