I want to reference a web service, and it requires user/pass authentication. In VS 2008, if I try to "add reference", or "add service reference", all I can type is the URL, there's no way to input my credentials. Obviously, if I try to load the ws, it shows me a nice message:
我想引用一个Web服务,它需要用户/通过身份验证。在VS 2008中,如果我尝试“添加引用”或“添加服务引用”,我可以输入的只是URL,无法输入我的凭据。显然,如果我尝试加载ws,它会向我显示一条很好的消息:
The request failed with HTTP status 403: Forbidden. Metadata contains a reference that cannot be resolved: The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Basic realm="weblogic"'. The remote server returned an error: (401) Unauthorized.
请求失败,HTTP状态为403:Forbidden。元数据包含无法解析的引用:HTTP请求未经授权,客户端身份验证方案为“匿名”。从服务器收到的身份验证标头是'Basic realm =“weblogic”'。远程服务器返回错误:(401)未经授权。
So my question is: Is it possible (using VS 2008) to add a reference to a web service that is protected? How?
所以我的问题是:是否有可能(使用VS 2008)添加对受保护的Web服务的引用?怎么样?
4 个解决方案
#1
6
It sounds like you're trying to use a Web Reference, and not a Service Reference (although I believe a Web Reference is a one kind of Service Reference). In VS08, after you have launched the "Add Service Reference", typed the URL to the web service, click the "Advanced" button, then click the "Add Web Reference". Type the URL again, then click "Add Web Reference". Now you should have a web reference instead, then authentication is similar to below:
听起来你正在尝试使用Web引用,而不是服务引用(尽管我相信Web引用是一种服务引用)。在VS08中,在启动“添加服务引用”后,键入Web服务的URL,单击“高级”按钮,然后单击“添加Web引用”。再次键入URL,然后单击“添加Web引用”。现在您应该使用Web引用,然后身份验证类似于以下内容:
WebService.Service myService = new WebService.Service();
myService.Credentials = new System.Net.NetworkCredential("username", "password");
WebService.ResultType results = myService.MyMethodReturnsResultType();
#2
4
Depending on how the service is authenticated, you may be sol.
根据服务的身份验证方式,您可能会遇到问题。
If it uses ASP.Net membership, no joy to be found. If the service code is yours, temporarily disable authentication to generate a proxy.
如果它使用ASP.Net成员资格,那么找不到快乐。如果服务代码是您的,请暂时禁用身份验证以生成代理。
Otherwise, try using a standard mechanism:
否则,请尝试使用标准机制:
http://username:password@host.domain/service
Best choice: Get the WSDL from your vendor and use wsdl.exe to generate your proxy.
最佳选择:从供应商处获取WSDL并使用wsdl.exe生成代理。
Update in response to comment:
更新以回应评论:
Yes, mocking the service in order to generate a proxy is a perfectly reasonable plan, If the target service is an ASP.net service or only accepts and returns simple types.
是的,模拟服务以生成代理是一个非常合理的计划,如果目标服务是ASP.net服务或只接受并返回简单类型。
The web service constructor has an overload that accepts an Uri or you could just modify the generated source.
Web服务构造函数具有接受Uri的重载,或者您可以只修改生成的源。
If you choose to modify the generated source, you will probably want to just extract the proxy class and delete the webservice reference:
如果您选择修改生成的源,您可能只想提取代理类并删除webservice引用:
After you generate the proxy with VS, if you 'show all files' and drill down into the WebService Reference, you will find a file called Reference.cs
. This is the only file you need. Copy the contents to another file and then just delete the web service reference.
使用VS生成代理后,如果“显示所有文件”并深入查看WebService Reference,您将找到名为Reference.cs的文件。这是您需要的唯一文件。将内容复制到另一个文件,然后只删除Web服务引用。
If you do this, you can potentially add your authentication logic into the proxy at this point.
如果这样做,您可以在此时将您的身份验证逻辑添加到代理中。
But again, getting the WSDL from the vendor is your best bet.
但同样,从供应商处获取WSDL是最好的选择。
Good luck.
祝你好运。
#3
3
Late reply, but you can also get it working if you open your site in the Visual Studio web browser and log in. Only works if your auth model supports cookies though.
延迟回复,但如果您在Visual Studio Web浏览器中打开您的站点并登录,也可以使其正常工作。仅在您的auth模型支持cookie时才有效。
#4
0
If you generate the code with WSDL you can override the GetWebRequest() method which will allow you to add the Authorization header
如果使用WSDL生成代码,则可以覆盖GetWebRequest()方法,该方法允许您添加Authorization标头
protected override System.Net.WebRequest GetWebRequest(Uri uri)
{
var req = base.GetWebRequest(uri);
req.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes("username:password")));
return req;
}
#1
6
It sounds like you're trying to use a Web Reference, and not a Service Reference (although I believe a Web Reference is a one kind of Service Reference). In VS08, after you have launched the "Add Service Reference", typed the URL to the web service, click the "Advanced" button, then click the "Add Web Reference". Type the URL again, then click "Add Web Reference". Now you should have a web reference instead, then authentication is similar to below:
听起来你正在尝试使用Web引用,而不是服务引用(尽管我相信Web引用是一种服务引用)。在VS08中,在启动“添加服务引用”后,键入Web服务的URL,单击“高级”按钮,然后单击“添加Web引用”。再次键入URL,然后单击“添加Web引用”。现在您应该使用Web引用,然后身份验证类似于以下内容:
WebService.Service myService = new WebService.Service();
myService.Credentials = new System.Net.NetworkCredential("username", "password");
WebService.ResultType results = myService.MyMethodReturnsResultType();
#2
4
Depending on how the service is authenticated, you may be sol.
根据服务的身份验证方式,您可能会遇到问题。
If it uses ASP.Net membership, no joy to be found. If the service code is yours, temporarily disable authentication to generate a proxy.
如果它使用ASP.Net成员资格,那么找不到快乐。如果服务代码是您的,请暂时禁用身份验证以生成代理。
Otherwise, try using a standard mechanism:
否则,请尝试使用标准机制:
http://username:password@host.domain/service
Best choice: Get the WSDL from your vendor and use wsdl.exe to generate your proxy.
最佳选择:从供应商处获取WSDL并使用wsdl.exe生成代理。
Update in response to comment:
更新以回应评论:
Yes, mocking the service in order to generate a proxy is a perfectly reasonable plan, If the target service is an ASP.net service or only accepts and returns simple types.
是的,模拟服务以生成代理是一个非常合理的计划,如果目标服务是ASP.net服务或只接受并返回简单类型。
The web service constructor has an overload that accepts an Uri or you could just modify the generated source.
Web服务构造函数具有接受Uri的重载,或者您可以只修改生成的源。
If you choose to modify the generated source, you will probably want to just extract the proxy class and delete the webservice reference:
如果您选择修改生成的源,您可能只想提取代理类并删除webservice引用:
After you generate the proxy with VS, if you 'show all files' and drill down into the WebService Reference, you will find a file called Reference.cs
. This is the only file you need. Copy the contents to another file and then just delete the web service reference.
使用VS生成代理后,如果“显示所有文件”并深入查看WebService Reference,您将找到名为Reference.cs的文件。这是您需要的唯一文件。将内容复制到另一个文件,然后只删除Web服务引用。
If you do this, you can potentially add your authentication logic into the proxy at this point.
如果这样做,您可以在此时将您的身份验证逻辑添加到代理中。
But again, getting the WSDL from the vendor is your best bet.
但同样,从供应商处获取WSDL是最好的选择。
Good luck.
祝你好运。
#3
3
Late reply, but you can also get it working if you open your site in the Visual Studio web browser and log in. Only works if your auth model supports cookies though.
延迟回复,但如果您在Visual Studio Web浏览器中打开您的站点并登录,也可以使其正常工作。仅在您的auth模型支持cookie时才有效。
#4
0
If you generate the code with WSDL you can override the GetWebRequest() method which will allow you to add the Authorization header
如果使用WSDL生成代码,则可以覆盖GetWebRequest()方法,该方法允许您添加Authorization标头
protected override System.Net.WebRequest GetWebRequest(Uri uri)
{
var req = base.GetWebRequest(uri);
req.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes("username:password")));
return req;
}