我创建了一个IIS托管的WCF OData 服务,两个从MS-SQL 数据库发布表。当我尝试从自定义C#项目访问服务时,我收到一条错误消息,指出“NT-AUTHORITY\ANONYMOUS”无法访问数据库。
IIS被配置为模拟用户,当我从浏览器请求相同的 URL 时,我会获取数据,所以我怀疑当我自己的C#应用程序正在执行请求时,由于某种原因,用户没有被正确模拟。
以下是事实:
- IIS上的ASP 模拟已启用。
- Windows-Authentication with NTLM -only 是唯一激活的身份验证方法。
validateIntegratedModeConfiguration
应用程序池在关闭的情况下运行集成(v4.0) 。- 如上所述,我可以通过浏览器访问所有数据。
- 我能够从我的客户端应用程序访问 OData 元数据,因此身份验证基本上可以正常工作。
这是我的客户端代码片段:
var serviceContextContext = new DataServiceContext(_url);
_serviceContextContext.IgnoreMissingProperties = true;
_serviceContextContext.IgnoreResourceNotFoundException = true;
_serviceContextContext.SendingRequest += DataServiceContextContextSendingRequest;
// Here is where I get an internal server error
var response = _serviceContextContext.Execute<ODataEntity>(entitySetRequestUri);
private void DataServiceContextContextSendingRequest(object sender, SendingRequestEventArgs e)
{
var request = (HttpWebRequest)e.Request;
request.Credentials = new NetworkCredential(_user, _password,_domain);
}
这是提琴手给我的:
浏览器请求 1 - 响应为 401
GET http://localhost/WcfDataService.svc/Table/?$select=Something HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Accept-Language: de-DE,de;q=0.8,en-US;q=0.5,en;q=0.3
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko
Accept-Encoding: gzip, deflate
Host: localhost
DNT: 1
Connection: Keep-Alive
浏览器请求 2 - 响应为 401
GET http://localhost/WcfDataService.svc/Table/?$select=Something HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Accept-Language: de-DE,de;q=0.8,en-US;q=0.5,en;q=0.3
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko
Accept-Encoding: gzip, deflate
Authorization: NTLM TlRMTVNTUAABAAAAB7IIogIAAgA0AAAADAAMACgAAAAGA9ckAAAAD0RFTEwyMDEzMDYwMUwy
DNT: 1
Connection: Keep-Alive
Host: localhost
浏览器请求 3 - 响应为 200 --> 工作!
GET http://localhost/WcfDataService.svc/Table/?$select=Zusatz HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Accept-Language: de-DE,de;q=0.8,en-US;q=0.5,en;q=0.3
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko
Accept-Encoding: gzip, deflate
Host: localhost
DNT: 1
Connection: Keep-Alive
Authorization: NTLM TlRMTVNTUAADAAAAAAAAAFgAAAAAAAAAWAAAAAAAAABYAAAAAAAAAFgAAAAAAAAAWAAAAAAAAABYAAAABcKIogYD1yQAAAAP9oJm9T9LmMQahBlQzRd2XA==
自定义应用程序请求 1 - 响应为 401
GET http://localhost/WcfDataService.svc/Table/?$select=Something HTTP/1.1
User-Agent: Microsoft ADO.NET Data Services
MaxDataServiceVersion: 2.0;NetFx
Accept: application/atom+xml,application/xml
Accept-Charset: UTF-8
Host: localhost
自定义应用程序请求 2 - 响应为 401
GET http://localhost/WcfDataService.svc/Table/?$select=Zusatz HTTP/1.1
User-Agent: Microsoft ADO.NET Data Services
MaxDataServiceVersion: 2.0;NetFx
Accept: application/atom+xml,application/xml
Accept-Charset: UTF-8
Authorization: NTLM TlRMTVNTUAABAAAAB4IIogAAAAAAAAAAAAAAAAAAAAAGA9ckAAAADw==
Host: localhost
自定义应用程序请求 3 - 响应为 500 --> 失败!
GET http://localhost/WcfDataService.svc/Table/?$select=Zusatz HTTP/1.1
User-Agent: Microsoft ADO.NET Data Services
MaxDataServiceVersion: 2.0;NetFx
Accept: application/atom+xml,application/xml
Accept-Charset: UTF-8
Authorization: NTLM TlRMTVNTUAADAAAAGAAYAIQAAABWAVYBnAAAAAQABABYAAAAEAAQAFwAAAAYABgAbAAAAAAAAADyAQAABYKIogYD1yQAAAAPgBZZaTO9a5O7HFjtoe2STmwAMgBmAGgAZQByAHQAZQBsAGwARABFAEwATAAyADAAMQAzADAANgAwADEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAS+24pX5KSeSdFgQ3uQ9wrwEBAAAAAAAAr9kbQ+2UzgHeWK0bawVaUgAAAAACAAQATAAyAAEAGABEAEUATABMADIAMAAxADMAMAA2ADAAMQAEABgATABhAHkAZQByADIALgBsAG8AawBhAGwAAwAyAEQARQBMAEwAMgAwADEAMwAwADYAMAAxAC4ATABhAHkAZQByADIALgBsAG8AawBhAGwABQAYAEwAYQB5AGUAcgAyAC4AbABvAGsAYQBsAAcACACv2RtD7ZTOAQYABAACAAAACAAwADAAAAAAAAAAAAAAAAAwAABAWMQ+SOQSOHP+7RN68GTo7ZpLSdYRekIYD8N3LRL0xwoAEAAAAAAAAAAAAAAAAAAAAAAACQAsAEgAVABUAFAALwBsAG8AYwBhAGwAaABvAHMAdAAuAGYAaQBkAGQAbABlAHIAAAAAAAAAAAAAAAAA
Host: localhost
在响应中,我收到以下错误消息:
Cannot open database "TEST" requested by the login. The login failed. Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'
所以我得出结论,我的请求与浏览器请求有所不同,这会阻止模拟正常工作。
我真的很感激任何提示。