EndGetResponse 返回 401 未经授权。 Apache 错误日志:无法生成上下文
我有一台运行 Apache 的服务器。现在我希望能够读取那里的文件夹的内容,并且该文件夹在 Apache httpd.conf 中配置如下:
Alias /folder "E:/Folder"
<Directory "E:/Folder">
AuthName "Public Folder"
AuthType SSPI
SSPIAuth On
SSPIAuthoritative On
SSPIDomain DOMAIN
SSPIOfferBasic On
SSPIOfferSSPI On
SSPIBasicPreferred Off
SSPIOmitDomain On
SSPIUsernameCase lower
require group "DOMAIN\Domain Users"
</Directory>
现在,当我使用浏览器时,我已经可以使用 URL http://ServerName/Folder/
所以这已经很好了。但现在我正在尝试为我的 C# 应用程序接收此文件夹数据。我正在使用 DefaultNetworkCredentials,我的代码看起来像这样:
public void CheckFolder()
{
WebRequest request = HttpWebRequest.Create(_uri);
request.Credentials = CredentialCache.DefaultNetworkCredentials;
request.BeginGetResponse(new AsyncCallback(HandleGetAvailableUpdates), request);
}
void HandleGetAvailableUpdates(IAsyncResult state)
{
try
{
if (state.IsCompleted)
{
WebRequest req = state.AsyncState as WebRequest;
WebResponse response = req.EndGetResponse(state);
Stream wStream = null;
wStream = response.GetResponseStream();
byte[] data = new byte[response.ContentLength];
etc.
.
.
.
}
catch (WebException ex)
{
Console.WriteLine(ex.ToString());
}
}
}
req.EndGetResponse(state) 现在抛出 WebException:
The remote server returned an error: (401) Unauthorized.
返回服务器端 我查看了 Apache error.log,在那里我发现:
(OS 87)The parameter is incorrect. : authentication failure for "/folder": user unknown, reason: cannot generate context
该程序的此功能已经大约两个月前工作得很好。 C# 代码根本没有改变,但我不知道服务器或 Apache 配置是否做了某些操作。我还尝试在 C# 代码中手动提供我的凭据(用户名和密码,与通过 Web 浏览器访问时使用的相同),而不是使用 DefaultNetworkCredentials,但没有效果。所以最后我的问题是:我忘记了什么吗?服务器上是否需要进行任何其他设置才能使其正常工作?
I got a server with Apache running. Now I want to be able to read the contents of a folder there and for that this folder is configured in the Apache httpd.conf something like this:
Alias /folder "E:/Folder"
<Directory "E:/Folder">
AuthName "Public Folder"
AuthType SSPI
SSPIAuth On
SSPIAuthoritative On
SSPIDomain DOMAIN
SSPIOfferBasic On
SSPIOfferSSPI On
SSPIBasicPreferred Off
SSPIOmitDomain On
SSPIUsernameCase lower
require group "DOMAIN\Domain Users"
</Directory>
Now when I use a Browser I can already access this Information with the URL http://ServerName/Folder/
So this workes just fine already. But now I am trying to receive this Folder-Data for my C# application. I am using DefaultNetworkCredentials and my Code looks something like this:
public void CheckFolder()
{
WebRequest request = HttpWebRequest.Create(_uri);
request.Credentials = CredentialCache.DefaultNetworkCredentials;
request.BeginGetResponse(new AsyncCallback(HandleGetAvailableUpdates), request);
}
void HandleGetAvailableUpdates(IAsyncResult state)
{
try
{
if (state.IsCompleted)
{
WebRequest req = state.AsyncState as WebRequest;
WebResponse response = req.EndGetResponse(state);
Stream wStream = null;
wStream = response.GetResponseStream();
byte[] data = new byte[response.ContentLength];
etc.
.
.
.
}
catch (WebException ex)
{
Console.WriteLine(ex.ToString());
}
}
}
req.EndGetResponse(state) now throws the WebException:
The remote server returned an error: (401) Unauthorized.
Going back on Server-Side I have a look at the Apache error.log and there I find:
(OS 87)The parameter is incorrect. : authentication failure for "/folder": user unknown, reason: cannot generate context
This feature of the Program already worked well about two month ago. The C#-Code did not change at all but I dont know if something had been done with the server or the Apache configuration. I also tried to give my Credentials (username and pw, the same I use when accessing via Web-Browser) manually in the C#-Code rather than using the DefaultNetworkCredentials but with no avail. So finally my question would be: Is there something I forgot? Are there any other settings on the server to be made to make it work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的查找方法:
用户 Fiddler、Live Http headers 或任何其他 HTTP 数据包嗅探器来查看浏览器发送了哪些标头/post 参数,然后以编程方式复制所有这些标头/post 参数。
Easiest way to find out:
User Fiddler, or Live Http Headers, or any other HTTP packet sniffer to see which headers/post parameters are sent by the browser, and then duplicate all of them programatically.