Example #1
0
        public async Task <IActionResult> AuthenticateUser([FromBody] IdentityAuthenticationRequest authenticationRequest)
        {
            if (authenticationRequest == null ||
                string.IsNullOrEmpty(authenticationRequest.Identifier) ||
                string.IsNullOrEmpty(authenticationRequest.Password))
            {
                return(HandleBadRequest("An identity identifier and password need to be supplied for authentication requests."));
            }

            try
            {
                string token = await AuthenticationService.Authenticate(authenticationRequest.Identifier, authenticationRequest.Password);

                if (token != null)
                {
                    return(Ok(new AuthenticationResponse(token)));
                }
                else
                {
                    return(Unauthorized());
                }
            }
            catch (EntityNotFoundException)
            {
                return(Unauthorized());
            }
            catch (Exception exception)
            {
                return(HandleUnexpectedException(exception));
            }
        }
        /// <summary>
        ///     用户认证接口
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cellphone"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public async Task <IdentityAuthenticationResponse> IdentityAuthentication(IdentityAuthenticationRequest request, string cellphone, string pwd)
        {
            IdentityAuthenticationResponse response = new IdentityAuthenticationResponse {
                IsSuccess = false
            };

            try
            {
                string url = $"{BizeBaseUrl}User/Auth/CGAuthenticate";
                //登录后去token
                LoginResponse loginResponse = await this.Login(new LoginRequest { LoginName = cellphone, Password = pwd });

                if (loginResponse == null)
                {
                    return(response);
                }
                Client.DefaultRequestHeaders.Remove("X-JYM-Authorization");
                Client.DefaultRequestHeaders.Add("X-JYM-Authorization", $"Bearer {loginResponse.AccessToken}");
                HttpResponseMessage responseMessage = await Client.PostAsJsonAsync(url, request);

                //IdentityAuthenticationResponse response = await responseMessage.Content.ReadAsAsync<IdentityAuthenticationResponse>();
                response.IsSuccess      = responseMessage.StatusCode == HttpStatusCode.OK;
                response.UserIdentifier = loginResponse.UserId.Replace("-", "").ToUpper();
            }
            catch (Exception)
            {
                return(response);
            }
            return(response);
        }