/// <summary> /// API client authentication. /// If authentication fails, use the errorCode and errorDescription attributes on the response dictionary to get error details. /// If authentication succeeds, SessionId is set to the Session ID. /// The Session ID is reusable so it's recommended that you save this value and use it for further API calls. /// Once you are done using the API, call ZelloAPI.Logout() to end the session and invalidate Session ID. /// </summary> /// <param name="username">administrative username.</param> /// <param name="password">administrative password.</param> public async Task <ZelloAPIResult> Authenticate(string username, string password) { ZelloAPIResult result = await callAPI("user/gettoken", HTTPMethod.GET, null).ConfigureAwait(false); ZelloAPIResult returnResult; if (!result.Success) { returnResult = new ZelloAPIResult(false, result.Response, null); return(returnResult); } if (result.Response == null) { returnResult = new ZelloAPIResult(false, null, null); return(returnResult); } var token = (string)result.Response["token"]; SessionId = (string)result.Response["sid"]; if (apiKey == null) { returnResult = new ZelloAPIResult(false, result.Response, null); return(returnResult); } string hashedPassword = MD5Hash(MD5Hash(password) + token + apiKey); string parameters = "username="******"&password="******"user/login", HTTPMethod.POST, parameters); return(returnResult); }
/// <summary> /// Ends session identified by sessionId. /// Use this method to terminate the API session. /// See ZelloAPI.Authenticate(). /// </summary> public async Task <ZelloAPIResult> Logout() { ZelloAPIResult result = await callAPI("user/logout", HTTPMethod.GET, null); SessionId = null; return(result); }
async Task <ZelloAPIResult> callAPI(string command, HTTPMethod method, string parameters) { ZelloAPIResult returnResult; string prefix = "http://"; if (host.Contains("http://") || host.Contains("https://")) { prefix = ""; } string urlString = prefix + host + "/" + command; if (SessionId != null) { urlString += "?sid=" + SessionId; } LastURL = urlString; var request = (HttpWebRequest)WebRequest.Create(urlString); request.Method = convertHTTPMethodToString(method); if (parameters != null) { request.ContentType = "application/x-www-form-urlencoded; charset=utf-8"; byte[] postData = Encoding.UTF8.GetBytes(parameters); using (var stream = await Task.Factory.FromAsync(request.BeginGetRequestStream, request.EndGetRequestStream, null)) { stream.Write(postData, 0, postData.Length); } } try { // Pick up the response: using (var response = (HttpWebResponse)(await Task <WebResponse> .Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null))) { var reader = new StreamReader(response.GetResponseStream()); string result = reader.ReadToEnd(); var json = (Dictionary <string, object>) new JavaScriptSerializer().DeserializeObject(result); bool success = json != null && json["code"].Equals("200"); returnResult = new ZelloAPIResult(success, json, null); } } catch (WebException exception) { returnResult = new ZelloAPIResult(false, null, exception); } return(returnResult); }