private bool isTokenExpired(HttpResponseMessage httpResponse) { try { if (httpResponse.IsSuccessStatusCode) { return(false); } var ex = DirectSpException.fromHttpResponse(httpResponse); //check database AccessDeniedOrObjectNotExists error; it means token has been validated if (ex.errorName == "AccessDeniedOrObjectNotExists") { return(false); } //noinspection JSUnresolvedVariable if (ex.errorName == "invalid_grant") { return(true); } return(ex.statusCode == System.Net.HttpStatusCode.Unauthorized); } catch (Exception) { return(false); } }
private async Task <string> getResponseString(HttpResponseMessage response) { var responseContent = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { throw DirectSpException.fromHttpResponse(response); } return(responseContent); }
private static DirectSpException fromSpException(HttpStatusCode statusCode, string content) { var spCallError = JsonConvert.DeserializeObject <SpCallError>(content); var ret = new DirectSpException() { statusCode = statusCode, errorType = spCallError.errorType, errorName = spCallError.errorName, errorNumber = spCallError.errorNumber, errorMessage = spCallError.errorMessage, errorDescription = spCallError.errorDescription, errorProcName = spCallError.errorProcName, errorData = spCallError.errorData, }; return(ret); }
internal static DirectSpException fromHttpResponse(HttpStatusCode statusCode, string content) { //parse content as OpenId exception try { var obj = JObject.Parse(content); var ex = new DirectSpException() { statusCode = statusCode, errorName = obj["error"].Value <string>(), errorDescription = obj["error_description"].Value <string>(), }; // recover from ErrorDescription try { ex = fromSpException(statusCode, ex.errorDescription); } catch { } return(ex); } catch { } //parse content as SpException exception try { return(fromSpException(statusCode, content)); } catch { } // simple text message var ret = new DirectSpException() { statusCode = statusCode, errorMessage = content }; return(ret); }