Ejemplo n.º 1
0
        public static ResultType SendGenericOAuthRequest(ADUser user, string orgDomain, Uri targetUri, out string diagnosticMessage, bool appOnly = false, bool useCachedToken = false, bool reloadConfig = false)
        {
            string domain = TestOAuthConnectivityHelper.GetDomain(user, orgDomain);

            if (domain == null)
            {
                diagnosticMessage = Strings.NullUserError;
                return(ResultType.Error);
            }
            ICredentials     icredentials     = TestOAuthConnectivityHelper.GetICredentials(appOnly, user, domain);
            OAuthCredentials oauthCredentials = icredentials as OAuthCredentials;

            if (icredentials == null)
            {
                diagnosticMessage = Strings.NullUserError;
                return(ResultType.Error);
            }
            ValidationResultCollector resultCollector    = new ValidationResultCollector();
            LocalConfiguration        localConfiguration = LocalConfiguration.Load(resultCollector);

            oauthCredentials.Tracer             = new TestOAuthConnectivityHelper.TaskOauthOutboundTracer();
            oauthCredentials.LocalConfiguration = localConfiguration;
            Guid value = Guid.NewGuid();

            oauthCredentials.ClientRequestId = new Guid?(value);
            HttpWebResponse httpWebResponse = null;
            ResultType      result          = ResultType.Success;
            string          text            = string.Empty;
            string          s             = string.Empty;
            StringBuilder   stringBuilder = new StringBuilder();
            string          value2        = TestOAuthConnectivityHelper.CheckReloadConfig(reloadConfig);
            string          value3        = TestOAuthConnectivityHelper.CheckUseCachedToken(useCachedToken);

            stringBuilder.AppendLine(value2);
            stringBuilder.AppendLine(value3);
            try
            {
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(new Uri(targetUri.ToString()));
                httpWebRequest.Method      = "GET";
                httpWebRequest.ContentType = "text/xml";
                httpWebRequest.Headers.Add("X-ExCompId", "OauthPartnerProbe:");
                httpWebRequest.Headers.Add("client-request-id", value.ToString());
                httpWebRequest.Headers.Add("request-id", value.ToString());
                httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, "Bearer");
                httpWebRequest.UserAgent   = "AMProbe/OAUTH/Exchange";
                httpWebRequest.Credentials = icredentials;
                httpWebResponse            = (httpWebRequest.GetResponse() as HttpWebResponse);
                if (httpWebResponse != null && httpWebResponse.StatusCode != HttpStatusCode.OK)
                {
                    throw new Exception(Strings.ResponseMessage(httpWebResponse.StatusCode.ToString()));
                }
                using (StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream()))
                {
                    s = streamReader.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                text   = ex.ToString();
                result = ResultType.Error;
            }
            finally
            {
                if (httpWebResponse != null)
                {
                    TestOAuthConnectivityHelper.LogHttpResponseHeaders(httpWebResponse.Headers, ref stringBuilder);
                    httpWebResponse.Close();
                }
            }
            stringBuilder.AppendLine(Strings.TestOutboundOauthLog);
            stringBuilder.AppendLine(Strings.ClientRequestId(value.ToString()));
            stringBuilder.AppendLine(oauthCredentials.Tracer.ToString());
            stringBuilder.AppendLine(Strings.TestOAuthResponseDetails("Generic"));
            stringBuilder.AppendLine(Strings.ResponseMessage(s));
            if (text != string.Empty)
            {
                stringBuilder.AppendLine(Strings.ExceptionHeader);
                stringBuilder.AppendLine(text);
            }
            diagnosticMessage = stringBuilder.ToString();
            return(result);
        }
Ejemplo n.º 2
0
        public static ResultType SendSPOAuthRequest(ADUser user, Uri targetUri, out string diagnosticMessage, bool appOnly = false, bool useCachedToken = false, bool reloadConfig = false)
        {
            int              startIndex       = user.GetFederatedIdentity().Identity.IndexOf('@') + 1;
            string           domain           = user.GetFederatedIdentity().Identity.Substring(startIndex);
            ICredentials     icredentials     = TestOAuthConnectivityHelper.GetICredentials(appOnly, user, domain);
            OAuthCredentials oauthCredentials = icredentials as OAuthCredentials;

            if (icredentials == null)
            {
                diagnosticMessage = Strings.NullUserError;
                return(ResultType.Error);
            }
            Guid value = Guid.NewGuid();

            oauthCredentials.ClientRequestId = new Guid?(value);
            ValidationResultCollector resultCollector    = new ValidationResultCollector();
            LocalConfiguration        localConfiguration = LocalConfiguration.Load(resultCollector);

            oauthCredentials.Tracer             = new TestOAuthConnectivityHelper.TaskOauthOutboundTracer();
            oauthCredentials.LocalConfiguration = localConfiguration;
            string         text           = targetUri.Scheme + "://" + targetUri.Host + "/_vti_bin/listdata.svc";
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(text);

            httpWebRequest.Method      = "GET";
            httpWebRequest.ContentType = "text/xml";
            httpWebRequest.Headers.Add("X-ExCompId", "OauthPartnerProbe:");
            httpWebRequest.Headers.Add("client-request-id", value.ToString());
            httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, "Bearer");
            httpWebRequest.UserAgent       = "AMProbe/OAUTH/Sharepoint";
            httpWebRequest.Credentials     = icredentials;
            httpWebRequest.PreAuthenticate = true;
            string        value2        = string.Empty;
            StringBuilder stringBuilder = new StringBuilder(1024);

            stringBuilder.AppendLine(Strings.ClientRequestId(value.ToString()));
            string value3 = TestOAuthConnectivityHelper.CheckReloadConfig(reloadConfig);
            string value4 = TestOAuthConnectivityHelper.CheckUseCachedToken(useCachedToken);

            stringBuilder.AppendLine(value3);
            stringBuilder.AppendLine(value4);
            HttpWebResponse httpWebResponse = null;
            ResultType      result          = ResultType.Success;
            string          text2           = string.Empty;

            try
            {
                stringBuilder.AppendLine(Strings.OAuthRequestEndPoint(text));
                httpWebResponse = (httpWebRequest.GetResponse() as HttpWebResponse);
                if (httpWebResponse != null && httpWebResponse.StatusCode != HttpStatusCode.OK)
                {
                    throw new Exception(Strings.HttpWebRequestFailure(httpWebResponse.StatusCode.ToString()));
                }
                using (StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream()))
                {
                    value2 = streamReader.ReadToEnd();
                }
            }
            catch (WebException ex)
            {
                text2  = ex.ToString();
                result = ResultType.Error;
            }
            finally
            {
                if (httpWebResponse != null)
                {
                    TestOAuthConnectivityHelper.LogHttpResponseHeaders(httpWebResponse.Headers, ref stringBuilder);
                }
                if (httpWebResponse != null)
                {
                    httpWebResponse.Close();
                }
            }
            stringBuilder.AppendLine(Strings.TestOutboundOauthLog);
            stringBuilder.AppendLine(Strings.TestOAuthResponseDetails("SharePoint"));
            stringBuilder.AppendLine(oauthCredentials.Tracer.ToString());
            stringBuilder.AppendLine(Strings.ResponseHeader);
            stringBuilder.AppendLine(value2);
            if (text2 != string.Empty)
            {
                stringBuilder.AppendLine(Strings.ExceptionHeader);
                stringBuilder.AppendLine(text2);
            }
            diagnosticMessage = stringBuilder.ToString();
            return(result);
        }
Ejemplo n.º 3
0
        public static ResultType SendExchangeOAuthRequest(ADUser user, string orgDomain, Uri targetUri, out string diagnosticMessage, bool appOnly = false, bool useCachedToken = false, bool reloadConfig = false)
        {
            string domain = TestOAuthConnectivityHelper.GetDomain(user, orgDomain);

            if (domain == null)
            {
                diagnosticMessage = Strings.NullUserError;
                return(ResultType.Error);
            }
            ICredentials     icredentials     = TestOAuthConnectivityHelper.GetICredentials(false, user, domain);
            OAuthCredentials oauthCredentials = icredentials as OAuthCredentials;

            if (icredentials == null)
            {
                diagnosticMessage = Strings.NullUserError;
                return(ResultType.Error);
            }
            string value = "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\">\r\n                                <soap:Header>\r\n                                <t:RequestServerVersion Version=\"Exchange2012\"/>\r\n                                </soap:Header>\r\n                                <soap:Body>\r\n                                <GetFolder xmlns=\"http://schemas.microsoft.com/exchange/services/2006/messages\">\r\n                                    <FolderShape>\r\n                                    <t:BaseShape>IdOnly</t:BaseShape>\r\n                                    </FolderShape>\r\n                                    <FolderIds>\r\n                                    <t:DistinguishedFolderId Id=\"inbox\"/>\r\n                                    </FolderIds>\r\n                                </GetFolder>\r\n                                </soap:Body>\r\n                            </soap:Envelope>";
            ValidationResultCollector resultCollector    = new ValidationResultCollector();
            LocalConfiguration        localConfiguration = LocalConfiguration.Load(resultCollector);

            oauthCredentials.Tracer             = new TestOAuthConnectivityHelper.TaskOauthOutboundTracer();
            oauthCredentials.LocalConfiguration = localConfiguration;
            Guid value2 = Guid.NewGuid();

            oauthCredentials.ClientRequestId = new Guid?(value2);
            HttpWebResponse httpWebResponse = null;
            ResultType      result          = ResultType.Success;
            string          text            = string.Empty;
            string          s             = string.Empty;
            StringBuilder   stringBuilder = new StringBuilder();
            string          value3        = TestOAuthConnectivityHelper.CheckReloadConfig(reloadConfig);
            string          value4        = TestOAuthConnectivityHelper.CheckUseCachedToken(useCachedToken);

            stringBuilder.AppendLine(value3);
            stringBuilder.AppendLine(value4);
            try
            {
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(new Uri(targetUri.Scheme + "://" + targetUri.Host + "/ews/Exchange.asmx"));
                httpWebRequest.Method      = "POST";
                httpWebRequest.ContentType = "text/xml";
                httpWebRequest.Headers.Add("X-ExCompId", "OauthPartnerProbe:");
                httpWebRequest.Headers.Add("client-request-id", value2.ToString());
                httpWebRequest.Headers.Add("request-id", value2.ToString());
                httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, "Bearer");
                httpWebRequest.UserAgent   = "AMProbe/OAUTH/Exchange";
                httpWebRequest.Credentials = icredentials;
                StreamWriter streamWriter = new StreamWriter(httpWebRequest.GetRequestStream());
                streamWriter.Write(value);
                streamWriter.Close();
                httpWebResponse = (httpWebRequest.GetResponse() as HttpWebResponse);
                if (httpWebResponse != null && httpWebResponse.StatusCode != HttpStatusCode.OK)
                {
                    throw new Exception(Strings.HttpWebRequestFailure(httpWebResponse.StatusCode.ToString()));
                }
                using (StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream()))
                {
                    s = streamReader.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                text   = ex.ToString();
                result = ResultType.Error;
            }
            finally
            {
                if (httpWebResponse != null)
                {
                    TestOAuthConnectivityHelper.LogHttpResponseHeaders(httpWebResponse.Headers, ref stringBuilder);
                    httpWebResponse.Close();
                }
            }
            stringBuilder.AppendLine(Strings.TestOutboundOauthLog);
            stringBuilder.AppendLine(Strings.ClientRequestId(value2.ToString()));
            stringBuilder.AppendLine(oauthCredentials.Tracer.ToString());
            stringBuilder.AppendLine(Strings.TestOAuthResponseDetails("Exchange"));
            stringBuilder.AppendLine(Strings.ResponseMessage(s));
            if (text != string.Empty)
            {
                stringBuilder.AppendLine(Strings.ExceptionHeader);
                stringBuilder.AppendLine(text);
            }
            diagnosticMessage = stringBuilder.ToString();
            return(result);
        }