public IAuthorizationToken GetAuthorizationToken(IAuthorizationTokenIdentifier authorizationTokenIdentifier) { if (authorizationTokenIdentifier.Key.IsNullOrEmpty()) { throw new ArgumentNullException(nameof(AuthorizationTokenIdentifier.Key)); } if (SsoSettings.ApiKey.IsNullOrEmpty()) { throw new ArgumentNullException(nameof(SsoSettings.ApiKey)); } using (var wc = new WebClient()) { wc.Headers[HttpRequestHeader.Authorization] = "Bearer " + SsoSettings.ApiKey; wc.Headers[HttpRequestHeader.Accept] = "application/json"; if (_proxyUri != null) { wc.Proxy = new WebProxy(_proxyUri); } // don't bother to fiddle with WebClient's timeout as it defaults to 100 sec try { var serializedToken = wc.DownloadString(_authorizationTokenServiceUri); return(AuthorizationToken.Deserialize(serializedToken)); } catch (WebException exception) when(exception.Status == WebExceptionStatus.ProtocolError) { using (var wr = exception.Response) using (var rs = wr.GetResponseStream()) { var error = Error.Deserialize(rs); throw new HttpRequestException($"Failed to get token from '{wr.ResponseUri}'.\r\n{exception.Message}\r\n{error.Message}."); } } } }