【问题标题】:WCF service with siteminder protected c#带有站点管理员保护的 C# 的 WCF 服务
【发布时间】:2016-09-21 05:40:48
【问题描述】:

我正在尝试使用受站点管理员保护的 WCF 网络服务。问题是当我尝试在浏览器中浏览 Web 服务 URL 时,它与我提供的凭据一起工作正常。

但是当我尝试以编程方式执行相同操作时,它会引发错误 - 错误 #401 未经授权。

供参考- http://www.codeproject.com/Articles/80314/How-to-Connect-to-a-SiteMinder-Protected-Resource

        CookieContainer cookies = null;
        HttpWebRequest request = null;
        HttpWebResponse response = null;
        string responseString = null;
        NameValueCollection tags = null;
        string url = null;
        url = PROTECTED_URL;
        Debug.WriteLine("Step 1: Requesting page @" + url);
        request = (HttpWebRequest)WebRequest.Create(url);
        request.AllowAutoRedirect = false;
        response = (HttpWebResponse)request.GetResponse();
        ShowResponse(response);
        // Step 2: Get the redirection location
        // make sure we have a valid response
        if (response.StatusCode != HttpStatusCode.Found)
        {
            throw new ApplicationException();
        }
        url = response.Headers["Location"];
        // Step 3: Open a connection to the redirect and load the login form, 
        // from this screen we will capture the required form fields.
        Debug.WriteLine("Step 3: Requesting page @" + url);
        request = (HttpWebRequest)WebRequest.Create(url);
        request.AllowAutoRedirect = false;

        try
        {
            response = (HttpWebResponse)request.GetResponse();
        }
        catch (Exception ex)
        {
            string str = ex.Message.ToString();
        }

【问题讨论】:

  • 所以当你从浏览器调用示例方法时它工作正常,但是当你以编程方式调用相同的方法时它不是吗?首先显示您的代码。
  • 当我用我的凭据点击 URL 时,它工作正常,但是当以编程方式尝试获取响应时 - 它抛出错误
  • 显示代码。你如何尝试得到它
  • 请看我的 cmets - 我指的是codeproject.com/Articles/80314/…
  • 您在 C# 项目中使用 VB?

标签: c# wcf wcf-security siteminder


【解决方案1】:

我的HtTpClient 可以拨打WCF

 public async Task<string> webClient(string method, string uri)
        {
                try
                {

                    var client = new HttpClient();
                    client.Timeout =new TimeSpan(0,0,0,10);
                    client.BaseAddress = new Uri(uri);
                    client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));
                    var response = client.GetAsync(method).Result;

                    string content = await response.Content.ReadAsStringAsync();
                    return content;

                }
                catch (Exception ex)
                {
                    return "Error";
                }
}

Uri 是基址,method 是你的方法名。

string response = webClient(uri + "/GetSomething/", uri).Result;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-20
    • 2017-01-19
    • 1970-01-01
    • 2020-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多