C#网络凭据没有被传递到服务器? [英] C# Network credentials not being passed to server?

查看:127
本文介绍了C#网络凭据没有被传递到服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:使用:

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



看起来工作得很好。

Seems to work fine.

我具有与一个CMS通信的应用程序。目前,我试图让这个客户端可以上传使用POST方法的文本/ xml数据到CMS。

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.

我可以通过使用curl通过这完美的罚款:

I can pass this through using curl perfectly fine:

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



然而,试图用在C#中的HttpWebRequest我不能让服务器返回什么,我希望它。所以,我发射了Wireshark的,有一个看看什么是真正被穿过,它是除了一个事实,即使用curl时,我可以看到几乎相同的:

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

在HTTP报头字段,而从我的客户端的输出,这些字段是根本不存在。 (凭证:不是纯文本实际上那里,它的一个子树授权 - 所以我不知道它是越来越它,但用户名和密码是否正确)

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.)

我试图用它来设置凭据的WebRequest的C#代码是这样的:

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;



我试过只设置凭据这样太(和不指定的serverURL):

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

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



但是,这仍然无法使其在Wireshark的显示出来。没有人有任何想法,如果:

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

a)该授权信息实际上应该是在HTTP头这个工作,以及

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

b)如果 - 那么我该如何让C#把它?我只似乎能够使用默认的凭据,这并不适用于我在做什么找到体面的例子。

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.

先谢谢了。

推荐答案

.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.

//stackoverflow.com/questions/1702426/httpwebrequest-not-passing-credentials/1702529#1702529\">More细节

More details in this post

这篇关于C#网络凭据没有被传递到服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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