public bool isAuthoriseSuccess(AuthorisationState state) { string url = (BreezeAPIPath.EndsWith("/") ? BreezeAPIPath : (BreezeAPIPath += "/")) + "/json/"; // hard code to json to get the Auth_token. string endPoint = ConfigurationManager.AppSettings["BreezeAPI.TimestampEndPoint"]; url += endPoint; // Make sure any SSL (self signing certificate) is accepted System.Net.ServicePointManager.ServerCertificateValidationCallback += delegate(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors) { return(true); // **** Always accept }; HttpWebRequest authRequest = (HttpWebRequest)WebRequest.Create(url); authRequest.Headers.Add(Authorisation, state.AuthorisationCode); authRequest.Headers.Add(AuthToken, state.Auth_Token); authRequest.Method = "GET"; try { var response = (HttpWebResponse)authRequest.GetResponse(); string result = null; using (Stream stream = response.GetResponseStream()) { StreamReader sr = new StreamReader(stream); result = sr.ReadToEnd(); sr.Close(); } if (!String.IsNullOrEmpty(result)) { var timeStamp = JsonConvert.DeserializeObject <TimeStampClass>(result); } } catch (Exception ex) { return(false); } return(true); }
/// <summary> /// Creates an instance of this class for use with making API Calls /// </summary> /// <param name="state">the authorization state required to make the API Calls</param> /// <param name="translator">the translator used to transform the data between your C# client code and the API</param> public APIClient(AuthorisationState state, IDataTranslator translator) { this.State = state; this.Translator = translator; UseCompression = false; }
/// <summary> /// Creates an instance of this class for use with making API Calls /// </summary> /// <param name="state">the authorization state required to make the API Calls</param> public APIClient(AuthorisationState state) { this.State = state; ReturnTypeString = ConfigurationManager.AppSettings["BreezeAPI.ReturnType"]; UseCompression = false; }
public AuthorisationState AuthorizeClient(string encodeAuthorisation) { string url = (BreezeAPIPath.EndsWith("/") ? BreezeAPIPath : (BreezeAPIPath += "/")) + "/json/"; // hard code to json to get the Auth_token. string endPoint = ConfigurationManager.AppSettings["BreezeAPI.TokenEndPoint"]; url += endPoint; // Make sure any SSL (self signing certificate) is accepted System.Net.ServicePointManager.ServerCertificateValidationCallback += delegate(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors) { return(true); // **** Always accept }; HttpWebRequest authRequest = (HttpWebRequest)WebRequest.Create(url); authRequest.Headers.Add(Authorisation, encodeAuthorisation); authRequest.Method = "GET"; ////authRequest.ContentType = "application/x-www-form-urlencoded"; //using (var ms = new MemoryStream()) //{ // using (var writer = new StreamWriter(authRequest.GetRequestStream())) // { // writer.Write(postBody); // writer.Close(); // } //} string ErrorMsg = string.Empty; try { var response = (HttpWebResponse)authRequest.GetResponse(); string result = null; using (Stream stream = response.GetResponseStream()) { StreamReader sr = new StreamReader(stream); result = sr.ReadToEnd(); sr.Close(); } if (!String.IsNullOrEmpty(result)) { // it's JSON so decode it var token = JsonConvert.DeserializeObject <IList <tokenClass> >(result).FirstOrDefault(); if (token != null) { var state = new AuthorisationState { Auth_Token = token.token, AuthorisationCode = encodeAuthorisation, isAuthorised = false }; state.isAuthorised = isAuthoriseSuccess(state); return(state); } else { ErrorMsg = "No token was returned."; } } } catch (Exception ex) { ErrorMsg = ex.Message; } return(new AuthorisationState() { AuthorisationCode = encodeAuthorisation, isAuthorised = false, ErrorMsg = ErrorMsg }); }