Пример #1
0
        public TokenResponse CreateToken
            (string email, string password)
        {
            var result = new TokenResponse {
                Condition = false, Message = string.Empty, AuthTokenResponse = new UserResponseModel()
            };

            if (string.IsNullOrEmpty(email))
            {
                result.Message = _validationMessage;
                return(result);
            }
            if (string.IsNullOrEmpty(password))
            {
                result.Message = _validationMessage;
                return(result);
            }
            _logService.Log(LogLevel.Info, $"username: {email} - password:{password}");

            var requestBody = new UserTokenRequestModel
            {
                grant_type    = "password",
                username      = email,
                password      = password,
                client_id     = _applicationSetting.ClientId,
                client_secret = _applicationSetting.ClientSecret,
                connection    = _applicationSetting.AuthorityConnection
            }.ToJSON();

            _logService.Log(LogLevel.Info, requestBody);
            var tokenRequestResult = _httpService.Post($"{_applicationSetting.Authority}oauth/token", requestBody.ToStringContent());

            _logService.Log(LogLevel.Info, tokenRequestResult.result);
            if (tokenRequestResult.condition)
            {
                var token = JsonConvert.DeserializeObject <UserResponseModel> (tokenRequestResult.result);
                token.expires_in         = DateTime.UtcNow.AddDays(1).ToString("yyyy-MM-ddTHH:mm:ss");
                result.Condition         = true;
                result.AuthTokenResponse = token;
                return(result);
            }
            result.Message = tokenRequestResult.result;
            return(result);
        }
Пример #2
0
 public static string ToJSON(
     this UserTokenRequestModel requestModel)
 {
     return(Newtonsoft.Json.JsonConvert.SerializeObject(requestModel));
 }