示例#1
0
            public async Task <User> Handle(Command request, CancellationToken cancellationToken)
            {
                if (await _context.Users.AnyAsync(x => x.Email == request.Email))
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { Email = "This email is already taken" });
                }

                if (await _context.Users.AnyAsync(x => x.UserName == request.Username))
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { Username = "******" });
                }
                var user = new AppUser
                {
                    DisplayName = request.DisplayName,
                    UserName    = request.Username,
                    Email       = request.Email
                };
                var result = await _userManager.CreateAsync(user, request.Password);

                if (result.Succeeded)
                {
                    return(new User
                    {
                        DisplayName = user.DisplayName,
                        Username = user.UserName,
                        Token = _jwt.CreatToken(user),
                        Image = null
                    });
                }
                throw new Exception("Saving new changes failed");
            }
示例#2
0
            public async Task <User> Handle(Query request, CancellationToken cancellationToken)
            {
                var user = await _userManager.FindByNameAsync(_userAccessor.GetUserName());

                return(new User
                {
                    DisplayName = user.DisplayName,
                    Username = user.UserName,
                    Token = _tokenGenerator.CreatToken(user),
                    Image = null
                });
            }
示例#3
0
            public async Task <User> Handle(Query request, CancellationToken cancellationToken)
            {
                var user = await _userManager.FindByEmailAsync(request.Email);

                if (user == null)
                {
                    throw new RestException(HttpStatusCode.Unauthorized);
                }

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

                if (result.Succeeded)
                {
                    return(new User
                    {
                        DisplayName = user.DisplayName,
                        Username = user.UserName,
                        Token = _tokenGenerator.CreatToken(user),
                        Image = null
                    });
                }
                throw new RestException(HttpStatusCode.Unauthorized);
            }