コード例 #1
0
            public async Task <UserDTO> Handle(Command request, CancellationToken cancellationToken)
            {
                //can't create user with existing email or username
                if (await _context.Users.Where(x => x.Email == request.Email).AnyAsync())
                {
                    throw new Application.Errors.RestException(HttpStatusCode.BadRequest, new { Email = "Email already exists." });
                }

                if (await _context.Users.Where(x => x.UserName == request.UserName).AnyAsync())
                {
                    throw new Application.Errors.RestException(HttpStatusCode.BadRequest, new { UserName = "******" });
                }

                //create new user
                var user = new Domain.AppUser
                {
                    DisplayName        = request.DisplayName,
                    UserName           = request.UserName,
                    Email              = request.Email,
                    RefreshToken       = _jwtGenerator.CreateRefreshToken(),
                    RefreshTokenExpiry = DateTime.Now.AddDays(30)
                };

                await _userManager.UpdateAsync(user);

                //user manager automatically salts and hashes password
                var result = await _userManager.CreateAsync(user, request.Password);

                if (result.Succeeded)
                {
                    return(new UserDTO
                    {
                        DisplayName = user.DisplayName,
                        Token = _jwtGenerator.CreateToken(user),
                        RefreshToken = _jwtGenerator.CreateRefreshToken(),
                        UserName = user.UserName,
                        Image = user.Photos.FirstOrDefault(x => x.IsMain)?.Url
                    });
                }
                else
                {
                    throw new Exception("Problem creating user.");
                }
            }
コード例 #2
0
            public async Task <UserDTO> Handle(Query request, CancellationToken cancellationToken)
            {
                //get user by email
                var user = await _userManager.FindByEmailAsync(request.Email);

                //if no email, return 401 status code
                if (user == null)
                {
                    throw new Application.Errors.RestException(HttpStatusCode.Unauthorized);
                }

                //check password
                var result = await _signInManager.CheckPasswordSignInAsync(user, request.Password, false);

                //if password succeeded, create and save refresh token and return JWT and token
                if (result.Succeeded)
                {
                    user.RefreshToken       = _jwtGenerator.CreateRefreshToken();
                    user.RefreshTokenExpiry = DateTime.Now.AddDays(30);

                    await _userManager.UpdateAsync(user);

                    return(new UserDTO
                    {
                        DisplayName = user.DisplayName,
                        Token = _jwtGenerator.CreateToken(user),
                        RefreshToken = user.RefreshToken,
                        UserName = user.UserName,
                        Image = user.Photos.FirstOrDefault(x => x.IsMain)?.Url
                    });
                }
                else //if password failed, return 401
                {
                    throw new Application.Errors.RestException(HttpStatusCode.Unauthorized);
                }
            }