private void HandleUnauthorized(HttpWebResponse response, WebException ex) { string authenticationHeader = response.Headers["WWW-Authenticate"]; if (!string.IsNullOrEmpty(authenticationHeader)) { int pos = authenticationHeader.IndexOf(' '); string scheme = authenticationHeader.Substring(0, pos); string parameters = authenticationHeader.Substring(pos + 1); IAuthorizationHandler handler = Session.AuthorizationDispatcher.Get(scheme); if (handler != null && handler.HandleAuthorizationRequest(new AuthorizationContext(Session, response, scheme, parameters))) { return; } } throw new NotAuthorizedException(response, ex); }