public async Task <IActionResult> Get() { var apiData = new ApiResultModel(); var userId = UserHttpInfo.GetValueByToken(Common.Utility.Models.Config.TokenInfoType.UserId); var user = await sysUsersService.GetModelAsync(userId);//根据id获取用户信息 var requestAuth = new RequestAuthModel() { Key = user.user_account, Secret = user.user_pwd }; //通过用户信息获取最新的token var responesToken = await new ToKenService().GetTokenAsync(requestAuth); if (responesToken.IsError) { apiData.Code = ErrorCodeType.ServerError; apiData.SetErrorCodeTypeMsg(); } else { apiData.Result = responesToken; } return(Ok(apiData)); }
public async Task <IActionResult> Post([FromBody] RequestAuthModel authModel) { var apiData = new ApiResultModel(); var responesToken = await new ToKenService().GetTokenAsync(authModel); if (responesToken.IsError) { apiData.Code = ErrorCodeType.KeyOrSecretError; apiData.SetErrorCodeTypeMsg(); } else { apiData.Result = responesToken; } return(Ok(apiData)); }
public IActionResult Post([FromBody] RequestAuthModel authModel) { var apiData = new ApiResultModel(); var key = Configuration.GetValue <string>("RestApi:Users:Key"); var secret = Configuration.GetValue <string>("RestApi:Users:Secret"); if (string.Equals(key, authModel.Key) && string.Equals(secret, authModel.Secret)) { apiData.Result = new { AccessToken = Guid.NewGuid().ToString("n"), ExpiresIn = DateTime.Now.GetTicks() }; } else { apiData.Code = ErrorCodeType.KeyOrSecretError; } return(Ok(apiData)); }
public async Task <ResponesToKenModel> GetTokenAsync(RequestAuthModel requestAuthModel) { var rdata = new ResponesToKenModel(); var handler = new HttpClientHandler(); handler.ServerCertificateCustomValidationCallback = delegate { return(true); }; var client = new HttpClient(handler); var disco = await client.GetDiscoveryDocumentAsync(passwordToken.Address); if (disco.IsError) { rdata.IsError = disco.IsError; return(rdata); } var tokenResponse = await client.RequestPasswordTokenAsync(new PasswordTokenRequest() { Address = disco.TokenEndpoint, ClientId = passwordToken.ClientId, ClientSecret = passwordToken.ClientSecret, Scope = passwordToken.Scope, UserName = requestAuthModel.Key, Password = requestAuthModel.Secret }); if (tokenResponse.IsError) { rdata.IsError = tokenResponse.IsError; return(rdata); } rdata.AccessToken = tokenResponse.AccessToken; rdata.ExpiresIn = tokenResponse.ExpiresIn; rdata.TokenType = tokenResponse.TokenType; var p_path = Path.Combine(StaticConfig.ContentRootPath, AppFileConfig.PrivateKey); rdata.Pkey = await p_path.ReadFileAsync(); return(rdata); }