示例#1
0
            public async Task <UserDTO> Handle(Comand request, CancellationToken cancellationToken)
            {
                if (await _dataContext.Users.AnyAsync(usr => usr.Email == request.Email))
                {
                    throw new RestException(System.Net.HttpStatusCode.BadRequest, new { Error = "User exists" });
                }

                var user = new AppUser()
                {
                    Email       = request.Email,
                    DisplayName = request.DisplayName,
                    UserName    = request.UserName
                };

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

                if (result.Succeeded)
                {
                    //await _signInManager.SignInAsync(user, true);
                    return(new UserDTO
                    {
                        DisplayName = request.DisplayName,
                        Image = null,
                        UserName = request.UserName,
                        Token = _jWTGenerator.CreateToken(user)
                    });
                }
                throw new Exception("Problem creating user");
            }
示例#2
0
            public async Task <User> Handle(Query request, CancellationToken cancellationToken)
            {
                var user = await _userManager.FindByEmailAsync(request.Email);

                if (user == null)
                {
                    Console.Write("User is Null");
                    throw new RestException(HttpStatusCode.Unauthorized);
                }

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

                if (result.Succeeded)
                {
                    return(new User
                    {
                        DisplayName = user.DisplayName,
                        Token = _jwtGenerator.CreateToken(user),
                        Username = user.UserName,
                        CashAmount = user.CashAmount,
                        Image = null
                    });
                }

                throw new RestException(HttpStatusCode.Unauthorized);
            }
示例#3
0
        public async Task <User> Handle(LoginQuery request, CancellationToken cancellationToken)
        {
            var user = await userManager.FindByNameAsync(request.UserName);

            if (user == null)
            {
                user = await userManager.FindByEmailAsync(request.UserName);
            }

            if (user != null)
            {
                var result = await signInManager.CheckPasswordSignInAsync(user, request.Password, false);

                if (result.Succeeded)
                {
                    var photo = await context.Photos.Where(x => x.Id == user.PhotoId).FirstOrDefaultAsync();

                    return(new User
                    {
                        DisplayName = user.DisplayName,
                        Token = await jWTGenerator.CreateToken(user),
                        Username = user.UserName,
                        ProfileImage = photo.Url
                    });
                }
            }

            throw new ArgumentNullException();
        }
示例#4
0
            public async Task <User> Handle(Query request, CancellationToken cancellationToken)
            {
                var user = await userManager.FindByEmailAsync(request.Email);

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

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

                if (result.Succeeded)
                {
                    //TO-DO : Generate Token for the User
                    return(new User()
                    {
                        DisplayName = user.DisplayName,
                        Image = string.Empty,
                        Token = jwtGenerator.CreateToken(user),
                        UserName = user.UserName
                    });
                }

                throw new RestException(System.Net.HttpStatusCode.Unauthorized);
            }
示例#5
0
            public async Task <User> Handle(Command request, CancellationToken cancellationToken)
            {
                //handler logic
                if (await _context.Users.Where(x => x.Email == request.Email).AnyAsync())
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { Email = "Email already exists" });
                }
                if (await _context.Users.Where(x => x.UserName == request.UserName).AnyAsync())
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { UserName = "******" });
                }
                var user = new AppUser
                {
                    DisplayName = request.DisplayName,
                    Email       = request.Email,
                    UserName    = request.UserName,
                };
                //return result
                // var isSuccess = await _context.SaveChangesAsync() > 0;
                // if (isSuccess) return Unit.Value;
                var result = await _userManager.CreateAsync(user, request.Password);

                if (result.Succeeded)
                {
                    return(new User
                    {
                        DisplayName = user.DisplayName,
                        Token = _jwtGenerator.CreateToken(user),
                        UserName = user.UserName,
                        Image = user.Photos.FirstOrDefault(x => x.IsMain)?.Url
                    });
                }

                throw new Exception("Problem creating user");
            }
示例#6
0
            public async Task <User> Handle(Query request, CancellationToken cancellationToken)
            {
                //handler logic
                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)
                {
                    //TODO: Generate token
                    return(new User
                    {
                        DisplayName = user.DisplayName,
                        //use interface instead of concrete class
                        Token = _jwtGenerator.CreateToken(user),
                        UserName = user.UserName,
                        Image = user.Photos.FirstOrDefault(x => x.IsMain)?.Url,
                    });
                }
                throw new RestException(HttpStatusCode.Unauthorized);
            }
示例#7
0
            public async Task <UserData> Handle(Signup request, CancellationToken cancellationToken)
            {
                //ver si existe
                var exists = (await context.Users.Where(x => x.Email == request.Email).AnyAsync() ||
                              await context.Users.Where(x => x.UserName == request.Username).AnyAsync());

                if (exists)
                {
                    throw new BusinessException(System.Net.HttpStatusCode.BadRequest, new { msg = "Ya existe un usuario registrado con este email" });
                }

                var user = new User
                {
                    FullName = $"{request.Name} {request.LastName}",
                    Email    = request.Email,
                    UserName = request.Username
                };

                var res = await userManager.CreateAsync(user, request.Password);

                return((res.Succeeded) ? new UserData
                {
                    FullName = user.FullName,
                    Token = generator.CreateToken(user, null),
                    Username = user.UserName,
                    Email = user.Email
                } : throw new BusinessException(System.Net.HttpStatusCode.InternalServerError, "No se pudo agregar al usuario"));
            }
            public async Task <LoggedInUserDto> Handle(Command request, CancellationToken cancellationToken)
            {
                var newUser = _mapper.Map <AppUser>(request);
                var result  = await _userMgr.CreateAsync(newUser, request.Password);

                if (!result.Succeeded)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new {
                        errors = string.Join(", ", result.Errors.Select(e => e.Description))
                    });
                }

                var token = await _userMgr.GenerateEmailConfirmationTokenAsync(newUser);

                token = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(token));
                var emailConfirmationUrl = $"{request.OriginUrl}/user/confirmEmail?token={token}&email={request.Email}";

                var emailBody = "";

                return(new LoggedInUserDto
                {
                    UserDetails = _mapper.Map <UserDto>(newUser),
                    Token = _jWTGenerator.CreateToken(newUser)
                });
            }
示例#9
0
            public async Task <User> Handle(Command request, CancellationToken cancellationToken)
            {
                if (await _context.Users.Where(x => x.Email == request.Email).AnyAsync())
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { Email = "Email already exists" });
                }

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

                var user = new AppUser
                {
                    DisplayName = request.DisplayName,
                    Email       = request.Email,
                    UserName    = request.Username
                };

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

                if (result.Succeeded)
                {
                    return(new User
                    {
                        DisplayName = user.DisplayName,
                        Token = _jwtGenerator.CreateToken(user),
                        Username = user.UserName,
                        Image = null,
                        CashAmount = 1000,
                    });
                }

                throw new Exception("Problem creating user");
            }
示例#10
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)
                {
                    // TODO: generate token
                    return(new User
                    {
                        DisplayName = user.DisplayName,
                        Token = _jwtGenerator.CreateToken(user),
                        Username = user.UserName,
                        Image = user.Photos.FirstOrDefault(photo => photo.IsMain)?.Url
                    });
                }
                else
                {
                    throw new RestException(HttpStatusCode.Unauthorized);
                }
            }
示例#11
0
            public async Task <ServiceResponse <UserLoginDto> > Handle(LoginCommand request, CancellationToken cancellationToken)
            {
                var encryptedPassword = PasswordEncrypter.Encrypt(request.Password);

                var dbUser = await _userRepository.TableNoTracking.FirstOrDefaultAsync(r => r.Email == request.Email && r.Password == encryptedPassword, cancellationToken);

                if (dbUser is null)
                {
                    throw new ApiException("User not found or given information is wrong");
                }

                if (!dbUser.IsActive)
                {
                    throw new ApiException("User state is Passive!");
                }

                var          user      = _mapper.Map <UserDto>(dbUser);
                UserLoginDto userLogin = _JWTGenerator.CreateToken(dbUser);

                userLogin.User = user;

                return(new ServiceResponse <UserLoginDto>()
                {
                    Value = userLogin
                });
            }
            public async Task <LoggedInUserDto> Handle(Query request, CancellationToken cancellationToken)
            {
                var existingUser = await _userMgr.FindByEmailAsync(request.Email);

                if (existingUser == null)
                {
                    throw new RestException(HttpStatusCode.Unauthorized, "Unauthorized access");
                }

                var result = await _signInMgr.CheckPasswordSignInAsync(existingUser, request.Password, false);

                if (!result.Succeeded)
                {
                    throw new RestException(HttpStatusCode.Unauthorized, "Unauthorized access");
                }
                ;

                var userDetails = _mapper.Map <UserDto>(existingUser);

                return(new LoggedInUserDto
                {
                    UserDetails = userDetails,
                    Token = _jWTGenerator.CreateToken(existingUser)
                });
            }
示例#13
0
            public async Task <User> Handle(Command request, CancellationToken cancellationToken)
            {
                if (await _context.Users.AnyAsync(user => user.Email == request.Email))
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { Email = "Email already exists" });
                }
                if (await _context.Users.AnyAsync(user => user.UserName == request.Username))
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { Username = "******" });
                }

                var user = new AppUser
                {
                    DisplayName = request.DisplayName,
                    Email       = request.Email,
                    UserName    = request.Username,
                };

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

                if (result.Succeeded)
                {
                    return new User
                           {
                               DisplayName = user.DisplayName,
                               Username    = user.UserName,
                               Token       = _jwtGenerator.CreateToken(user),
                               Image       = user.Photos.FirstOrDefault(photo => photo.IsMain)?.Url
                           }
                }
                ;

                throw new Exception("Problem creating user");
            }
        }
示例#14
0
            public async Task <User> Handle(Command request, CancellationToken cancellationToken)
            {
                if (await context.Users.Where(m => m.Email == request.Email).AnyAsync())
                {
                    throw new RestException(System.Net.HttpStatusCode.BadRequest, new { Email = "A User already exists with this email." });
                }
                if (await context.Users.Where(m => m.UserName == request.Username).AnyAsync())
                {
                    throw new RestException(System.Net.HttpStatusCode.BadRequest, new { Username = "******" });
                }

                var user = new AppUser()
                {
                    DisplayName = request.DisplayName,
                    Email       = request.Email,
                    UserName    = request.Username,
                };

                var result = await userManager.CreateAsync(user, request.Password);

                if (result.Succeeded)
                {
                    return(new User()
                    {
                        DisplayName = user.DisplayName,
                        Token = jWTGenerator.CreateToken(user),
                        UserName = user.UserName,
                        Image = string.Empty
                    });
                }

                throw new Exception("Problem creating a New User");
            }
示例#15
0
            public async Task <User> Handle(Command request, CancellationToken cancellationToken)
            {
                if (await _context.Users.AnyAsync(x => x.Email == request.Email))
                {
                    throw new RestException(HttpStatusCode.Conflict, new { Email = request.Email + " already exists" });
                }

                if (await _context.Users.AnyAsync(x => x.UserName == request.Username))
                {
                    throw new RestException(HttpStatusCode.Conflict, new { UserName = request.Username + " already exists" });
                }

                var user = new AppUser
                {
                    DisplayName = request.DisplayName,
                    Email       = request.Email,
                    UserName    = request.Username
                };

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

                if (result.Succeeded)
                {
                    return(new User
                    {
                        DisplayName = user.DisplayName,
                        Token = _jwtGenerator.CreateToken(user),
                        Username = user.UserName
                    });
                }

                throw new Exception("Error creating user");
            }
示例#16
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);
                }

                // third bool parameter is for lockout if user credentials are incorrect.
                var result = await _signInManager.CheckPasswordSignInAsync(user, request.Password, false);

                if (result.Succeeded)
                {
                    // TODO: Return token
                    return(new User
                    {
                        DisplayName = user.DisplayName,
                        Token = _jwtGenerator.CreateToken(user),
                        Username = user.UserName,
                        Image = user.Photos.FirstOrDefault(x => x.IsMain)?.Url
                    });
                }

                throw new RESTException(HttpStatusCode.Unauthorized);
            }
示例#17
0
            public async Task <User> Handle(Command request, CancellationToken cancellationToken)
            {
                if (await _context.Users.Where(x => x.Email == request.Email).AnyAsync())
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { Email = "E-mail already exists..." });
                }
                if (await _context.Users.Where(x => x.UserName == request.Username).AnyAsync())
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { Username = "******" });
                }

                var user = new AppUser
                {
                    DisplayName = request.DisplayName,
                    Email       = request.Email,
                    UserName    = request.Username
                };

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


                if (result.Succeeded)
                {
                    return(new User
                    {
                        DisplayName = user.DisplayName,
                        Token = _jWTGenerator.CreateToken(user),
                        Username = user.UserName,
                        Image = user.Photos.FirstOrDefault(x => x.IsMain)?.Url
                    });
                }

                throw new Exception("Problems creating new account!");
            }
示例#18
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 = "Email already exists" });
                }

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

                var user = new AppUser
                {
                    DisplayName = request.DisplayName,
                    Email       = request.Email,
                    UserName    = request.Username,
                };
                //here savechangesAsync retuns int, this int is number of changes saved in database so if its 0 then no changes is saved.
                var result = await _userManager.CreateAsync(user, request.Password);

                if (result.Succeeded)
                {
                    return(new User
                    {
                        DisplayName = user.DisplayName,
                        Token = _jwtGenerator.CreateToken(user),
                        Username = user.UserName,
                        Image = null,
                    });
                }

                throw new Exception("Problem Creating user");
            }
示例#19
0
            public async Task <User> Handle(Query request, CancellationToken cancellationToken)
            {
                var user = await _userManager.FindByEmailAsync(request.Email);

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

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

                if (result.Succeeded)
                {
                    var token = _jWTGenerator.CreateToken(user);
                    return(new User
                    {
                        DisplayName = user.DisplayName,
                        Token = token,
                        Username = user.UserName,
                        Image = null
                    });
                }

                throw new RestException(HttpStatusCode.Unauthorized);
            }
示例#20
0
 public User(AppUser user, IJWTGenerator jwtGenerator, string refreshToken)
 {
     DisplayName  = user.DisplayName;
     Token        = jwtGenerator.CreateToken(user);
     Username     = user.UserName;
     Image        = user.Photos.FirstOrDefault(x => x.IsMain)?.Url;
     Bio          = user.Bio;
     RefreshToken = refreshToken;
 }
示例#21
0
            public async Task <User> Handle(Query request, CancellationToken cancellationToken)
            {
                var user = await _userManager.FindByNameAsync(_userAccessor.GetCurrentUsername());

                return(new User
                {
                    DisplayName = user.DisplayName,
                    Token = _jWTGenerator.CreateToken(user),
                    Username = user.UserName
                });
            }
示例#22
0
            public async Task <User> Handle(Query request, CancellationToken cancellationToken)
            {
                var user = await _userManager.FindByNameAsync(_userAccessor.GetCurrentUsername());

                return(new User {
                    Display = user.Display,
                    Username = user.UserName,
                    Token = _jwtGenerator.CreateToken(user),
                    Image = user.Photos.FirstOrDefault(x => x.IsMain)?.Url
                });
            }
            public async Task <UserDTO> Handle(Query request, CancellationToken cancellationToken)
            {
                var user = await _userManager.FindByNameAsync(_userAccesor.GetUserName());

                return(new UserDTO()
                {
                    DisplayName = user.DisplayName,
                    Image = user.Photos.FirstOrDefault(ph => ph.IsMain)?.Id,
                    UserName = user.UserName,
                    Token = _jWTGenerator.CreateToken(user)
                });
            }
示例#24
0
            public async Task <AppUserViewModel> Handle(Query query, CancellationToken cancellationToken)
            {
                var appUser = await _userManager.FindByNameAsync(_userAccessor.GetCurrentUsername());

                return(new AppUserViewModel
                {
                    DisplayName = appUser.DisplayName,
                    Username = appUser.UserName,
                    Token = _jwtGenerator.CreateToken(appUser),
                    PictureURL = appUser.Photos.FirstOrDefault(x => x.IsMain)?.URL,
                });
            }
示例#25
0
            public async Task <UserData> Handle(Execute request, CancellationToken cancellationToken)
            {
                var user = await userManager.FindByNameAsync(sessionUser.GetSessionUser());

                var roleList = new List <string>(await userManager.GetRolesAsync(user));

                return(new UserData
                {
                    FullName = user.FullName,
                    Username = user.UserName,
                    Email = user.Email,
                    Token = generator.CreateToken(user, roleList),
                    Image = null
                });
            }
            public async Task <LoggedInUserDto> Handle(Query request, CancellationToken cancellationToken)
            {
                var existingUser = await _userMgr.FindByEmailAsync(_userAccessor.GetCurrentEmail());

                if (existingUser == null)
                {
                    throw new RestException(HttpStatusCode.Unauthorized, "Unauthorized access");
                }

                var userDetails = _mapper.Map <UserDto>(existingUser);

                return(new LoggedInUserDto
                {
                    UserDetails = userDetails,
                    Token = _jWTGenerator.CreateToken(existingUser)
                });
            }
示例#27
0
            public async Task <LoggedInUserDto> Handle(Command request, CancellationToken cancellationToken)
            {
                var newUser = _mapper.Map <AppUser>(request);
                var result  = await _userMgr.CreateAsync(newUser, request.Password);

                if (!result.Succeeded)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new {
                        errors = string.Join(", ", result.Errors.Select(e => e.Description))
                    });
                }

                return(new LoggedInUserDto
                {
                    UserDetails = _mapper.Map <UserDto>(newUser),
                    Token = _jWTGenerator.CreateToken(newUser)
                });
            }
示例#28
0
            public async Task <User> Handle(Query request, CancellationToken cancellationToken)
            {
                //handler logic
                //we get user object from usermanager-> httpcontext -> username
                var user = await _userManager.FindByNameAsync(_userAccess.GetCurrentUsername());

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

                return(new User
                {
                    Token = _generator.CreateToken(user),
                    DisplayName = user.DisplayName,
                    Image = user.Photos.FirstOrDefault(x => x.IsMain)?.Url,
                    Username = user.UserName
                });
            }
示例#29
0
        public async Task <User> Handle(UserRegisterCommand request, CancellationToken cancellationToken)
        {
            var userExist = await dbContext.Users.Where(x => x.UserName == request.UserName).AnyAsync();

            var defaultPhotoId = await dbContext.Photos.Select(x => x.Id).FirstOrDefaultAsync();

            if (userExist)
            {
                throw new Exception();
            }
            else
            {
                var user = new AppUser
                {
                    DisplayName = $"{request.FirstName} {request.LastName}",
                    UserName    = request.UserName,
                    FirstName   = request.FirstName,
                    LastName    = request.LastName,
                    Email       = request.Email,
                    PhotoId     = defaultPhotoId
                };



                var result = await userManager.CreateAsync(user, request.Password);

                if (result.Succeeded)
                {
                    await userManager.AddToRoleAsync(user, "User");

                    return(new User
                    {
                        DisplayName = user.DisplayName,
                        Token = await jWTGenerator.CreateToken(user),
                        ProfileImage = null
                    });
                }
            }

            throw new ArgumentNullException();
        }
示例#30
0
            public async Task <UserDTO> Handle(Query request, CancellationToken cancellationToken)
            {
                var user = await _userManager.FindByEmailAsync(request.Email);

                if (user != null)
                {
                    var result = await _signInManager.CheckPasswordSignInAsync(user, request.Password, false);

                    if (result.Succeeded)
                    {
                        return(new UserDTO
                        {
                            DisplayName = user.DisplayName,
                            UserName = user.UserName,
                            Image = user.Photos.FirstOrDefault(ph => ph.IsMain)?.Id,
                            Token = _jWTGenerator.CreateToken(user)
                        });
                    }
                }
                throw new RestException(System.Net.HttpStatusCode.Unauthorized);
            }