Esempio n. 1
0
        private static HttpWebRequest CreateBaseWebRequest(string method, Uri serviceUri, WebProxy webProxy, FedAuthSupport.STSWrapper stsWrapper)
        {
            HttpWebRequest httpWebRequest = WebRequest.Create(serviceUri) as HttpWebRequest;

            httpWebRequest.Method = method;
            if (method == "POST")
            {
                httpWebRequest.ContentType = "application/x-www-form-urlencoded";
            }
            httpWebRequest.CookieContainer   = FedAuthSupport.GetMSIAuthCookies(new Uri(stsWrapper.STSLoginUri), new string[0]);
            httpWebRequest.AllowAutoRedirect = false;
            httpWebRequest.UserAgent         = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
            httpWebRequest.Accept            = "*/*";
            httpWebRequest.Proxy             = webProxy;
            httpWebRequest.Credentials       = CredentialCache.DefaultNetworkCredentials;
            httpWebRequest.Timeout           = 300000;
            return(httpWebRequest);
        }
Esempio n. 2
0
        public static CookieContainer GetCookies(Uri serviceUri, WebProxy webproxy = null, Uri loginUri = null, string stsHost = null)
        {
            FedAuthSupport.STSWrapper sTSWrapper = new FedAuthSupport.STSWrapper(serviceUri, stsHost);
            string         text           = null;
            HttpWebRequest httpWebRequest = FedAuthSupport.CreateBaseWebRequest("GET", serviceUri, webproxy, sTSWrapper);

            using (WebResponse response = httpWebRequest.GetResponse())
            {
                string text2 = response.Headers["Location"];
                if (text2 != null)
                {
                    if (text2.Contains("wctx"))
                    {
                        Uri uri = new Uri(text2);
                        NameValueCollection nameValueCollection = HttpUtility.ParseQueryString(uri.Query);
                        text = nameValueCollection["wctx"];
                    }
                    else
                    {
                        loginUri = new Uri(serviceUri, text2);
                    }
                }
            }
            if (loginUri == null)
            {
                loginUri = serviceUri;
            }
            StringBuilder stringBuilder = new StringBuilder();
            XmlTextWriter writer        = new XmlTextWriter(new StringWriter(stringBuilder));
            WSTrust13ResponseSerializer wSTrust13ResponseSerializer = new WSTrust13ResponseSerializer();

            wSTrust13ResponseSerializer.WriteXml(sTSWrapper.TokenResponse, writer, new WSTrustSerializationContext());
            string         xml             = stringBuilder.ToString();
            HttpWebRequest httpWebRequest2 = FedAuthSupport.CreateBaseWebRequest("POST", loginUri, webproxy, sTSWrapper);
            XmlDocument    xmlDocument     = new XmlDocument();

            xmlDocument.LoadXml(xml);
            XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(xmlDocument.NameTable);

            xmlNamespaceManager.AddNamespace("trust", "http://docs.oasis-open.org/ws-sx/ws-trust/200512");
            XmlNode xmlNode = xmlDocument.SelectSingleNode("//trust:RequestSecurityTokenResponse", xmlNamespaceManager);

            if (xmlNode == null)
            {
                throw new InvalidDataException("No RequestSecurityTokenResponse found in ADFS query.");
            }
            string s;

            if (!string.IsNullOrWhiteSpace(text))
            {
                s = string.Format("wa=wsignin1.0&wctx={0}&wresult={1}", HttpUtility.UrlEncode(text), HttpUtility.UrlEncode(xmlNode.OuterXml));
            }
            else
            {
                s = string.Format("wa=wsignin1.0&wresult={1}", HttpUtility.UrlEncode(xmlNode.OuterXml));
            }
            byte[] bytes = Encoding.ASCII.GetBytes(s);
            using (Stream requestStream = httpWebRequest2.GetRequestStream())
            {
                requestStream.Write(bytes, 0, bytes.Length);
            }
            CookieContainer result;

            using (HttpWebResponse httpWebResponse = httpWebRequest2.GetResponse() as HttpWebResponse)
            {
                if (httpWebResponse != null)
                {
                    if (httpWebResponse.Cookies != null && httpWebResponse.Cookies.Count > 0)
                    {
                        CookieContainer cookieContainer = new CookieContainer();
                        cookieContainer.Add(serviceUri, httpWebResponse.Cookies);
                        result = cookieContainer;
                        return(result);
                    }
                    if (httpWebResponse.Headers.AllKeys.Contains("Set-Cookie"))
                    {
                        CookieContainer cookieContainer = new CookieContainer();
                        string[]        array           = httpWebResponse.Headers["Set-Cookie"].Split(new char[]
                        {
                            ','
                        });
                        string[] array2 = array;
                        for (int i = 0; i < array2.Length; i++)
                        {
                            string   text3  = array2[i];
                            string[] array3 = text3.Split(new char[]
                            {
                                ';'
                            });
                            int num = array3[0].IndexOf('=');
                            if (num > 0)
                            {
                                Uri    uri2  = serviceUri;
                                string name  = array3[0].Substring(0, num);
                                string value = array3[0].Substring(num + 1);
                                for (int j = 1; j < array3.Length; j++)
                                {
                                    string[] array4 = array3[j].Split(new char[]
                                    {
                                        '='
                                    });
                                    if (array4[0].Trim().Equals("domain"))
                                    {
                                        uri2 = new Uri(uri2.Scheme + "://" + array4[1].Trim());
                                    }
                                }
                                Cookie cookie = new Cookie(name, value);
                                cookieContainer.Add(uri2, cookie);
                            }
                        }
                        if (cookieContainer.Count > 0)
                        {
                            result = cookieContainer;
                            return(result);
                        }
                    }
                }
                result = null;
            }
            return(result);
        }