コード例 #1
0
        public async Task <JsonResult> StartRegisterBySendingVerificationRequest(PhoneVerificationRequestModel verificationRequest)
        {
            var response = new ResultModel();

            if (!ModelState.IsValid)
            {
                return(JsonModelStateErrors());
            }
            var validPhone = await _authy.ValidatePhoneAsync(verificationRequest.CountryCode, verificationRequest.PhoneNumber);

            if (!validPhone.IsSuccess)
            {
                return(Json(validPhone.ToBase()));
            }

            var isUsedPhoneCheck = await _authy.CheckIfPhoneIsUsedByAnotherUserAsync(validPhone.Result);

            if (!isUsedPhoneCheck.IsSuccess)
            {
                return(Json(isUsedPhoneCheck));
            }

            var userName = _authy.NormalizePhoneNumber(verificationRequest.CountryCode, verificationRequest.PhoneNumber);
            var user     = await _userManager.UserManager.FindByNameAsync(userName);

            if (user != null)
            {
                response.Errors.Add(new ErrorModel(string.Empty, "The user already exists"));
                return(Json(response));
            }
            HttpContext.Session.Set("phone_verification_request", verificationRequest);
            var result = await _authy.PhoneVerificationRequestAsync(
                verificationRequest.CountryCode,
                verificationRequest.PhoneNumber,
                verificationRequest.Via
                );

            return(Json(result));
        }
コード例 #2
0
        public async Task <IActionResult> AuthenticateUser([FromBody] AuthenticateUserRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            string token;

            // verific daca userul exista si daca da, il returneaza din baza de date
            var userInfo = await _mediator.Send(new ApplicationUser
            {
                Email    = request.Email,
                Password = request.Password
            });

            ClaimsIdentity identity;

            if (!userInfo.IsAuthenticated)
            {
                _logger.LogInformation($"Invalid username ({request.Email}) or password ({request.Password})");
                return(Unauthorized("Invalid credentials"));
            }

            // Get the generic claims
            identity = new ClaimsIdentity(await GetGenericIdentity(request.Email, userInfo.NgoId.ToString(), UserType.Assistant.ToString()),
                                          new[]
            {
                new Claim(ClaimsHelper.UserIdProperty, userInfo.UserId.ToString(), ClaimValueTypes.Boolean)
            });

            //var identity = await GetClaimsIdentity(request);
            if (identity == null)
            {
                _logger.LogInformation($"Invalid username ({request.Email}) or password ({request.Password})");
                return(Unauthorized("Invalid credentials"));
            }

            token = GetTokenFromIdentity(identity, _jwtOptions.IssuedAt.Add(TimeSpan.FromHours(0.5)));

            // save the temporary token
            if (!_accountService.SaveTemporaryToken(userInfo.UserId, token))
            {
                return(Unauthorized("Token issue"));
            }

            var phone = userInfo.Phone.StartsWith('0') ? userInfo.Phone.Remove(0, 1) : userInfo.Phone;

            string result = await _authy.PhoneVerificationRequestAsync(
                "+40",
                phone
                );

            // Serialize and return the response
            var response = new AuthenticationResponseModel
            {
                access_token = token,
                expires_in   = (int)TimeSpan.FromHours(0.5).TotalSeconds,      //(int)_jwtOptions.ValidFor.TotalSeconds,
                first_login  = userInfo.FirstAuthentication,
                phone_verification_request = result
            };

            return(Ok(response));
        }