public AgsAuthenticationToken GetToken() { lock (GetTokenLock) { if (_token != null && _token.Expires.AddSeconds(-5) < DateTime.Now) { _token = null; } if (_token == null) { if (!_isPortal) { string json = RequestToken(String.Format("f=json&username={0}&password={1}&client=requestip", User, Password)); _token = AgsAuthenticationToken.Deserialize(json); } else { string json = RequestToken(String.Format("f=json&username={0}&password={1}&referer=x", User, Password)); _token = AgsAuthenticationToken.Deserialize(json); json = RequestToken(String.Format("f=json&username={0}&password={1}&referer=x&token={2}&serverURL={3}", User, Password, _token.Value, ServerUrl)); _token = AgsAuthenticationToken.Deserialize(json); } } } return(_token); }
public AgsAuthenticationToken GetToken() { lock (GetTokenLock) { if (_token != null && _token.Expires.AddSeconds(-5) < DateTime.Now) { _token = null; } if (_token == null) { string requestUrl = Url + "generateToken"; string data = String.Format("f=json&username={0}&password={1}&client=requestip", User, Password); string json; try { WebRequest request = WebRequest.Create(requestUrl); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; using (StreamWriter writer = new StreamWriter(request.GetRequestStream())) { writer.Write(data); } WebResponse response = request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream())) { json = reader.ReadToEnd(); } } catch (Exception ex) { throw new AgsException("Error communicating with the ArcGIS Server token service", ex); } _token = AgsAuthenticationToken.Deserialize(json); } } return(_token); }