public void LoadFromFile(String cacheId) { try { System.Reflection.Assembly asm = System.Reflection.Assembly.GetAssembly(this.GetType()); String tokenFile = Path.GetFullPath(asm.Location) + "-" + cacheId + ".zToken"; if (!File.Exists(tokenFile)) { return; } String jData = File.ReadAllText(tokenFile, Encoding.UTF8); ZabbixAccessToken item = JSON.Deserialize <ZabbixAccessToken>(jData); this.access_token = item.access_token; this.create_time = item.create_time; this.error = item.error; this.expires_in = item.expires_in; } catch { return; } }
public static ZabbixAccessToken GetAccessToken(Uri zabbixApi, String username, String password, JSON.DebugMessage dbg) { try { JSON.DebugMessage dbg2 = new JSON.DebugMessage(delegate(String data, String debug) { if (dbg != null) { dbg(data, debug); } }); WebClient client = new WebClient(); String jData = ""; Byte[] content = new Byte[0]; //Get Zabbix API Version String sData = JSON.Serialize2(new { jsonrpc = "2.0", method = "apiinfo.version", id = 1 }); APIVerResult ver = null; try { ver = JSON.JsonWebRequest <APIVerResult>(zabbixApi, sData, "application/json", null, "POST", null, dbg2); } catch (Exception ex) { if (dbg != null) { try { dbg("Error: " + ex.Message, ""); } catch { } } ; ZabbixAccessToken err = new ZabbixAccessToken(); err.error = ex.Message; return(err); } sData = JSON.Serialize2(new { jsonrpc = "2.0", method = "user.login", _params = new { user = username, password = password }, id = 1 }); sData = sData.Replace("_params", "params"); AuthResult auth = null; try { auth = JSON.JsonWebRequest <AuthResult>(zabbixApi, sData, "application/json", null, "POST", null, dbg2); if (auth.error == null) { ZabbixAccessToken ok = new ZabbixAccessToken(); ok.api_ver = new Version(ver.result); ok.access_token = auth.result; ok.expires_in = 180; if (dbg != null) { try { dbg(JSON.Serialize2(ok), ""); } catch { } } ; return(ok); } else { ZabbixAccessToken err = new ZabbixAccessToken(); err.error = auth.error.message; return(err); } } catch (Exception ex) { if (dbg != null) { try { dbg("Error: " + ex.Message, ""); } catch { } } ; ZabbixAccessToken err = new ZabbixAccessToken(); err.error = ex.Message; return(err); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Erro on GetAccessToken: " + ex.Message); throw ex; } }