从ASP.NET服务发送Windows身份验证的请求

发布于 2025-02-01 18:49:56 字数 983 浏览 3 评论 0原文

我有一个工作的Python脚本,该脚本将请求使用Windows身份验证发送到Web服务,以获取令牌。

我要复制的位是:

url = 'https://ssologin.company.com/Logon/Logon.aspx?permission=permission'
h = WC.Dispatch('WinHTTP.WinHTTPRequest.5.1')
h.SetAutoLogonPolicy(0)
h.SetOption(6, False)
h.Open('GET', url, False)
h.Send()

我有一个ASP.NET核心服务,试图这样做:

var uri = new Uri("https://ssologin.company.com/Logon/Logon.aspx?permission=permission");
WinHttpHandler handler = new WinHttpHandler();
handler.ServerCredentials = CredentialCache.DefaultNetworkCredentials;           
HttpClient httpClient = new HttpClient(handler);
var httpResponseMessage = await httpClient.GetAsync(uri);

现在,我收到以下错误: winhttpexception:错误12029调用winhttp_callback_status_request_error,“无法建立与服务器的连接”。

但是,如果我注释// Handler.ServerCredentials = recredentialCache.defaultNetworkCredentials;,那么我将获得错误401未经授权。我还注意到recredentialcache.defaultnetworkcredentials是无效的。

你能让我知道我在这里缺少什么吗?谢谢。

I have a working Python script which sends a request to an web service using Windows Authentication in order to get a token.

The bit that I'm trying to replicate is:

url = 'https://ssologin.company.com/Logon/Logon.aspx?permission=permission'
h = WC.Dispatch('WinHTTP.WinHTTPRequest.5.1')
h.SetAutoLogonPolicy(0)
h.SetOption(6, False)
h.Open('GET', url, False)
h.Send()

I have an ASP.NET Core service which tries to do it this way:

var uri = new Uri("https://ssologin.company.com/Logon/Logon.aspx?permission=permission");
WinHttpHandler handler = new WinHttpHandler();
handler.ServerCredentials = CredentialCache.DefaultNetworkCredentials;           
HttpClient httpClient = new HttpClient(handler);
var httpResponseMessage = await httpClient.GetAsync(uri);

Right now I am getting the following error:
WinHttpException: Error 12029 calling WINHTTP_CALLBACK_STATUS_REQUEST_ERROR, 'A connection with the server could not be established'.

However, if I comment // handler.ServerCredentials = CredentialCache.DefaultNetworkCredentials;, then I get Error 401 Unauthorised. I also noticed that CredentialCache.DefaultNetworkCredentials is NULL.

Can you please let me know what I am missing here? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柏拉图鍀咏恒 2025-02-08 18:49:56

实际上,身份验证工作正常,但是请求将其重定向到拒绝连接的服务。简单的解决方法是仅禁用重定向。另外,在 @spzvtbg的建议下,我使用了默认的httpclienthandler

最终代码最终以这样的方式:

var uri = new Uri("https://ssologin.company.com/Logon/Logon.aspx?permission=permission");
HttpClientHandler handler = new HttpClientHandler();
handler.UseDefaultCredentials = true;
handler.PreAuthenticate = true;
handler.AllowAutoRedirect = false;
HttpClient httpClient = new HttpClient(handler);
var httpResponseMessage = await httpClient.GetAsync(uri);

Actually, the authentication worked fine, but the request was redirecting to a service which refuses to connect. The easy fix to this was to just disable redirecting. Also, at @spzvtbg's suggestion, I used the default HttpClienthandler.

The final code ended up like this:

var uri = new Uri("https://ssologin.company.com/Logon/Logon.aspx?permission=permission");
HttpClientHandler handler = new HttpClientHandler();
handler.UseDefaultCredentials = true;
handler.PreAuthenticate = true;
handler.AllowAutoRedirect = false;
HttpClient httpClient = new HttpClient(handler);
var httpResponseMessage = await httpClient.GetAsync(uri);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文