Beispiel #1
0
        public virtual async Task <ActionResult> Token([FromForm] TokenRequest tokenRequest, CancellationToken cancellationToken)
        {
            if (!tokenRequest.grant_type.Equals("password", StringComparison.OrdinalIgnoreCase))
            {
                throw new Exception("OAuth flow is not password.");
            }

            //var user = await userRepository.GetByUserAndPass(username, password, cancellationToken);
            var user = await userManager.FindByNameAsync(tokenRequest.username);

            if (user == null)
            {
                throw new BadRequestException("نام کاربری یا رمز عبور اشتباه است");
            }

            var isPasswordValid = await userManager.CheckPasswordAsync(user, tokenRequest.password);

            if (!isPasswordValid)
            {
                throw new BadRequestException("نام کاربری یا رمز عبور اشتباه است");
            }


            //if (user == null)
            //    throw new BadRequestException("نام کاربری یا رمز عبور اشتباه است");

            var jwt = await jwtService.GenerateAsync(user);

            return(new JsonResult(jwt));
        }
        public virtual async Task <ActionResult> Token([FromForm] TokenRequest model, CancellationToken cancellationToken)
        {
            if (!model.grant_type.Equals("password", StringComparison.OrdinalIgnoreCase))
            {
                return(BadRequest("OAuth flow is not password."));
            }

            var user = await _userManager.FindByNameAsync(model.username);

            if (user == null)
            {
                return(Unauthorized());
            }

            var isPasswordValid = await _userManager.CheckPasswordAsync(user, model.password);

            if (!isPasswordValid)
            {
                return(Unauthorized());
            }

            var jwt = await _jWTService.GenerateAsync(user);

            return(new JsonResult(jwt));
        }