控制台应用程序将HttpClient与ADFS身份验证一起使用

控制台应用程序将HttpClient与ADFS身份验证一起使用

问题描述:

我想通过C#控制台应用程序访问我们的API(主要用于测试).我们的Web应用程序由ADFS STS保护.我的问题是如何使用HttpClient进行身份验证和重定向(或类似的方法,我不着急).

I would like to access our API (for testing mostly) via a C# console application. Our web application is secured with an ADFS STS. My issue is how to do the authentication and redirection using an HttpClient (or similar, I am not fussed).

我已经阅读了该页面(https://msdn.microsoft.com/zh-cn/library/cc236591.aspx),其中包含请求和响应的所有示例,但我似乎无法使其正常工作.我的代码是基本的:

I've read this page (https://msdn.microsoft.com/en-us/library/cc236591.aspx) with all the examples of the requests and responses, but I cannot seem to get it to work. My code is basic:

Uri baseAddress = new Uri("http://localhost:64022");

using (HttpClient client = new HttpClient() { BaseAddress = baseAddress })
{
    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "#");
    HttpResponseMessage response = client.SendAsync(request).Result;

    var encoding = ASCIIEncoding.ASCII;
    using (var reader = new System.IO.StreamReader(response.Content.ReadAsStreamAsync().Result, encoding))
    {
        string responseText = reader.ReadToEnd();
    }
}

响应是一个HTML表单,其中包含我的应用程序的操作(localhost:64022)和"wa","wresult"和"wctx"有效负载.如果我使用这些表单输入来生成第二个请求,则会收到未授权的错误.谁能指导我有关我需要采取的步骤

The response is an HTML form with an action of my application (localhost:64022) and a 'wa', 'wresult' and 'wctx' payload. If I use those form inputs to generate a second request I get an unauthorised error. Can anyone guide me as to the steps I need to take to accomplish this?

西玛,

下面的链接可能对您有用.

The link below might be useful to you.

此致

Tony