C#Siteminder身份验证

时间:2015-04-29 16:49:35

标签: c# siteminder

我正在尝试编写一些代码来连接到使用Siteminder身份验证的HTTPS站点。

我一直在收到401.任何想法?

我在这里已经阅读了一些不同的东西,但没有一个看起来真的有用。我也在使用Fiddler / Firefox Tamper来窥探正在发生的事情。

以下是我迄今为止在代码方面所取得的成就:

try
        {

            Uri uri = new Uri("https://websiteaddresshere");
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri) as HttpWebRequest;

            request.Accept = "text/html, application/xhtml+xml, */*";

            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko";
        //    request.Connection = "Keep-Alive";
           // request.Method = "Get";
           // request.Accept = "text";
            request.AllowAutoRedirect = true;
            request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

            Cookie emersoncookie = new Cookie("SMCHALLENGE",  "YES");
            emersoncookie.Domain = "mydomain";
            emersoncookie.Path = "/";




           // authentication
            var cache = new CredentialCache();
            cache.Add(uri, "False", new NetworkCredential("myusername", "mypassword"));

            request.Credentials = cache;


            // response.
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (Stream stream = response.GetResponseStream())
                {
                    XmlTextReader reader = new XmlTextReader(stream);
                    MessageBox.Show(stream.ToString());
                }
            }

        }
        catch (WebException exception)
        {
            string responseText;

            using (var reader = new StreamReader(exception.Response.GetResponseStream()))
            {
                responseText = reader.ReadToEnd();
                MessageBox.Show(responseText.ToString());
            }
        }

1 个答案:

答案 0 :(得分:0)

我无法让Jasen的代码工作。也许您的SM设置与我的不同。但是使用SiteMinder,它通常是一个两步认证过程。下面的代码块适用于我:

        //Make initial request
        RestClient client = new RestClient("http://theResourceDomain/myApp");
        client.CookieContainer = new CookieContainer();
        IRestResponse response = client.Get(new RestRequest("someProduct/orders"));

        //Now add credentials.
        client.Authenticator = new HttpBasicAuthenticator("username", "password");
        //Get resource from the SiteMinder URI which will redirect to the correct API URI upon authentication.
        response = client.Get(new RestRequest(response.ResponseUri)); 

虽然这使用了RestSharp,但可以使用 HttpClient 甚至 HttpWebRequest 轻松复制。