コード例 #1
0
        public async Task <Response <TokenDto> > CreateTokenAsync(LoginDto loginDto)
        {
            if (loginDto == null)
            {
                throw new ArgumentNullException(nameof(loginDto));
            }
            var user = await userManager.FindByEmailAsync(loginDto.Email);

            if (user == null)
            {
                return(Response <TokenDto> .Fail("Email veya parola yanlış", 404, true));
            }

            if (!await userManager.CheckPasswordAsync(user, loginDto.PassWord))
            {
                return(Response <TokenDto> .Fail("Email veya parola yanlış", 404, true));
            }

            var token            = tokenService.CreateToken(user);
            var UserRefreshToken = await refreshToken.Where(x => x.UserId == user.Id).SingleOrDefaultAsync();

            if (UserRefreshToken == null)
            {
                await refreshToken.AddAsync(new RefreshToken { UserId = user.Id, Code = token.RefreshToken, OTraih = token.RefreshTokenExpiration });
            }
            else
            {
                UserRefreshToken.Code   = token.RefreshToken;
                UserRefreshToken.OTraih = token.RefreshTokenExpiration;
            }
            await unitOfWork.CommitAsync();

            return(Response <TokenDto> .Success(token, 200));
        }
コード例 #2
0
        public async Task <ActionResult <ApplicationUser> > Register(ApplicatonUserCreate applicatonUserCreate)
        {
            var applicationUserIdentity = new ApplicationUserIdentity
            {
                Username = applicatonUserCreate.Username,
                Email    = applicatonUserCreate.Email,
                FullName = applicatonUserCreate.FullName
            };

            var result = await _userManager.CreateAsync(applicationUserIdentity, applicatonUserCreate.Password);

            if (result.Succeeded)
            {
                ApplicationUser user = new ApplicationUser()
                {
                    ApplicationUsrId = applicationUserIdentity.ApplicationUserId,
                    Username         = applicationUserIdentity.Username,
                    Email            = applicationUserIdentity.Email,
                    Fullname         = applicatonUserCreate.FullName,
                    Token            = _tokenServices.CreateToken(applicationUserIdentity)
                };

                return(user);
            }

            return(BadRequest(result.Errors));
        }
コード例 #3
0
        public async Task <ActionResult <UserDTO> > RegisterUser(RegistrationDTO registrationDto)
        {
            if (await UserExist(registrationDto.Username))
            {
                return(BadRequest("Username is already registered!!"));
            }

            using var hmac = new HMACSHA512();
            var user = new AppUser
            {
                UserName     = registrationDto.Username,
                PasswordHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(registrationDto.Password)),
                PasswordSalt = hmac.Key
            };

            await _database.Users.AddAsync(user);

            await _database.SaveChangesAsync();

            return(new UserDTO
            {
                Username = user.UserName,
                Token = _tokenServices.CreateToken(user)
            });
        }
コード例 #4
0
        public async Task <ActionResult <UserDto> > GetCurrentUser()
        {
            var user = await _userManager.FindByEmailFromClaimPrincipal(HttpContext.User);

            return(new UserDto
            {
                Email = user.Email,
                Token = _tokenServices.CreateToken(user),
                DisplayName = user.DisplayName
            });
        }
コード例 #5
0
        public async Task <ActionResult <UserDTO> > Register(RegisterDTO register)
        {
            if (await UserExisted(register.Username))
            {
                return(BadRequest("Usuario ya registrado"));
            }
            using var hmac = new HMACSHA512();
            var user = new AppUser
            {
                UserName     = register.Username.ToLower(),
                PasswordHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(register.Password)),
                PasswordSalt = hmac.Key
            };

            _Context.Users.Add(user);
            await _Context.SaveChangesAsync();

            return(new UserDTO
            {
                UserName = register.Username,
                Token = _TokenServices.CreateToken(user)
            });
        }
コード例 #6
0
        public async Task <ActionResult <UserDto> > Register(RegisterDto userDetails)
        {
            if (await _user.UserExists(userDetails.Email))
            {
                return(BadRequest("Email already exists!"));
            }
            //Adding user to the database
            var newUser = await _user.AddUser(userDetails);

            var userDto = new UserDto
            {
                Id          = newUser.Id,
                FirstName   = userDetails.FirstName,
                LastName    = userDetails.LastName,
                Email       = userDetails.Email,
                CreatedDate = newUser.CreatedDate,
                UpdatedDate = newUser.UpdatedDate,
                CreatedById = newUser.CreatedById,
                UpdatedById = newUser.UpdatedById,
                UserType    = newUser.UserType,
                Token       = _tokenService.CreateToken(newUser)
            };

            //Save userinfo to applicant table if the usertype is an applicant.
            if (userDto.UserType == "Applicant")
            {
                var applicantDetails = new ApplicantDto {
                    Id        = userDto.Id,
                    FirstName = userDto.FirstName,
                    LastName  = userDto.LastName,
                    Email     = userDto.Email
                };
                await _applicant.Save(applicantDetails);
            }

            return(userDto);
        }
コード例 #7
0
        public async Task <ActionResult <UserDto> > Register(RegisterDto registerDto)
        {
            if (await UserExists(registerDto.Username))
            {
                return(BadRequest("Username is taken"));
            }

            using var hmac = new HMACSHA512();
            var user = new AppUser
            {
                Username     = registerDto.Username.ToLower(),
                PasswordHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(registerDto.Password)),
                PasswordSalt = hmac.Key
            };

            _context.Users.Add(user);
            await _context.SaveChangesAsync();

            return(new UserDto
            {
                Username = user.Username,
                Token = tokenService.CreateToken(user)
            });
        }
コード例 #8
0
        public async Task <ActionResult <UserDto> > register(RegisterDto registerDto)
        {
            if (await UserExist(registerDto.Username))
            {
                return(BadRequest("User Is Taken"));
            }
            var user = _mapper.Map <AppUser>(registerDto);

            using var hmac = new HMACSHA512();

            user.UserName     = registerDto.Username.ToLower();
            user.PasswordHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(registerDto.Password));
            user.PasswordSalt = hmac.Key;

            _Context.Add(user);
            await _Context.SaveChangesAsync();

            return(new UserDto {
                Username = user.UserName,
                token = _tokenServices.CreateToken(user),
                KnownAs = user.KnownAs,
                Gender = user.Gender
            });
        }