控制台应用程序将HttpClient与ADFS身份验证一起使用 [英] Console application to use HttpClient with ADFS authentication

查看:127
本文介绍了控制台应用程序将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

这篇关于控制台应用程序将HttpClient与ADFS身份验证一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆