C# 网络凭据未传递到服务器?

发布于 2024-11-29 21:47:46 字数 1397 浏览 0 评论 0原文

编辑:使用:

byte[] authBytes = System.Text.Encoding.UTF8.GetBytes(user + ":" + password);
wr.Headers["Authorization"] = "Basic " + Convert.ToBase64String(authBytes);

似乎工作正常。

我有一个与 CMS 通信的应用程序。我目前正在尝试让该客户端能够使用“POST”方法将文本/xml 数据上传到 CMS。

我可以使用curl 完美地传递这个:

curl -u user:password -H "Content-Type:text/xml" -d "<element>myXML</element>" serverURL

但是,尝试在C# 中使用HttpWebRequest 我无法让服务器返回我想要的内容。因此,我启动了 Wireshark 并查看了实际传递的内容,除了使用curl 时我可以看到以下事实之外,它几乎完全相同:

Authorization: Basic <a bunch of hex>=\r\n
Credentials: user:password

在 HTTP 标头字段中,而在客户端的输出中,这些标头字段根本不存在。 (“凭据:”实际上并不以纯文本形式存在,它是“授权:”的子树 - 所以我不确定它从哪里获取,但用户名和密码是正确的。)

我的 C# 代码尝试用于设置网络请求的凭据是这样的:

NetworkCredential myCred = new NetworkCredential(
                    user, password, serverURL);

CredentialCache myCache = new CredentialCache();

myCache.Add(new Uri(serverURL), "Basic", myCred);

HttpWebRequest wr = (HttpWebRequest) HttpWebRequest.Create(serverURL);
wr.Credentials = myCache;

我也尝试过像这样设置凭据(并且不指定 serverURL):

wr.Credentials = new NetworkCredential(user,password,serverURL);

但这仍然无法使其显示在wireshark中。有谁知道是否:

A) 授权信息实际上应该位于 HTTP 标头中才能正常工作,

B) 如果是 - 那么我该如何让 C# 将其放入?我似乎只能使用默认凭据找到合适的示例,这不适用于我正在做的事情。

提前致谢。

Edit: Using:

byte[] authBytes = System.Text.Encoding.UTF8.GetBytes(user + ":" + password);
wr.Headers["Authorization"] = "Basic " + Convert.ToBase64String(authBytes);

Seems to work fine.

I have an application that communicates with a CMS. I'm currently trying to get this client to be able to upload text/xml data to the CMS using a "POST" method.

I can pass this through using curl perfectly fine:

curl -u user:password -H "Content-Type:text/xml" -d "<element>myXML</element>" serverURL

However, trying to use the HttpWebRequest in C# I can't get the server to return what I want it to. So I fired up Wireshark and had a look at what was actually being passed through, and it's pretty much identical except for the fact that when using curl I can see:

Authorization: Basic <a bunch of hex>=\r\n
Credentials: user:password

In the HTTP header fields, while in the output from my client, these header fields are simply not present. ("Credentials:" isn't actually there in plain text, it's a subtree of "Authorization:" - so I'm not sure where it's getting it from, but the username and password are correct.)

The C# code I'm trying to use to set the credentials for the webrequest is something like this:

NetworkCredential myCred = new NetworkCredential(
                    user, password, serverURL);

CredentialCache myCache = new CredentialCache();

myCache.Add(new Uri(serverURL), "Basic", myCred);

HttpWebRequest wr = (HttpWebRequest) HttpWebRequest.Create(serverURL);
wr.Credentials = myCache;

I've tried just setting the credentials like this too (and without specifying serverURL):

wr.Credentials = new NetworkCredential(user,password,serverURL);

But that still doesn't make it show up in wireshark. Does anyone have any idea if:

A) That authorization information should actually be in the HTTP header for this to work, and

B) If it is - then how do I make C# put it in? I only seem to be able to find decent examples using the default credentials, which doesn't apply to what I'm doing.

Thanks in advance.

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

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

发布评论

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

评论(1

辞取 2024-12-06 21:47:46

.NET 的 WebRequest 有一个令人恼火的默认行为,它仅在收到 HTTP 401 未授权响应后才发送凭据。

手动添加凭据标头(正如您所做的那样)似乎是最好的解决方案。

本文中有更多详细信息

.NET's WebRequest has an infuriating default behavior where it only sends credentials after receiving an HTTP 401 Not Authorized response.

Manually adding the credentials header (as you've done) seems to be the best solution available.

More details in this post

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文