/// <summary> /// This method make call to remote service API for retrieving token /// </summary> /// <returns> /// Bearer Token class from Consultix authorization API response /// </returns> /// <param name="clientId">Client Id string for token</param> /// <seealso cref="GetToken(Guid)"/> /// <seealso cref="GetClientId(Guid)"/> private BearerToken RefreshToken(string clientId) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; var postWebRequest = (HttpWebRequest)WebRequest.Create(AuthenticationHelper.AuthorizationUrl); postWebRequest.PreAuthenticate = true; var postData = $"grant_type={AuthenticationHelper.GrantType}&" + $"username={AuthenticationHelper.Username}&" + $"password={AuthenticationHelper.Password}&" + $"client_id={clientId}"; postWebRequest.Method = "POST"; var data = Encoding.ASCII.GetBytes(postData); postWebRequest.Accept = "application/json"; postWebRequest.ContentType = "application/x-www-form-urlencoded"; postWebRequest.ContentLength = data.Length; using (var stream = postWebRequest.GetRequestStream()) { stream.Write(data, 0, data.Length); } try { var response = (HttpWebResponse)postWebRequest.GetResponse(); if (response.StatusCode == HttpStatusCode.OK) { var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd(); return(BearerToken.FromJson(responseString)); } else { _logger.LogError("Authentication fail", new Exception("Error obtaining OAuth token. Response code: " + response.StatusCode.ToString())); return(null); } } catch (Exception ex) { _logger.LogError("Authentication fail", new Exception("Error calling the Authentication service.", new Exception( "[Source]: " + ex.Source + ", [Message]: " + ex.Message + ", [Stack Trace]: " + ex.StackTrace ))); return(null); } }
// MÉTODO PARA OBTENER EL TOKEN public static BearerToken ObtenerToken(ClienteAlterno cliente) { string controlador = "/api/token/GetToken"; string endpoint = urlbase + controlador; var client = new RestClient(endpoint); client.Timeout = -1; var request = new RestRequest(Method.POST); request.AddHeader("Content-Type", "application/json"); request.AddHeader("Cookie", "ak_bmsc=1B2DBCB5D80264AA0698B7F0AC518ABCBDF7CF37470800008F7F6B5FEC0F897A~pl/oJgJTrrHhbQTqb4FK0MGGUg6rCfUibWDDgML6mVfnc4voiQnt0bN75qp83XTuKTyEYCh1U6ILMXH71QaJF37B601rg6tJevK8K916oHEpaRqXtKR5ZSwK3VdkH4iyYUQkBJ1zWg+EdCpLPKeFsgVRlVEVKw7YAvgO9i9qbQm9Vx3zIpWWf6xCDcBOa4a6tMYWPEhvRoZ8WlS3llWtt/JuSf67BcnsZk1QiCnyxOEuE="); request.AddParameter("application/json", cliente.ToJson(), ParameterType.RequestBody); IRestResponse response = client.Execute(request); return(BearerToken.FromJson(response.Content)); }