开发者

How add service reference in visual studio 2008 authenticating against password-protected web service

开发者 https://www.devze.com 2022-12-26 09:07 出处:网络
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 inpu

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 messag开发者_如何学Goe:

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.

So my question is: Is it possible (using VS 2008) to add a reference to a web service that is protected? How?


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:

WebService.Service myService = new WebService.Service();
myService.Credentials = new System.Net.NetworkCredential("username", "password");
WebService.ResultType results = myService.MyMethodReturnsResultType();


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.

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.

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.

The web service constructor has an overload that accepts an Uri or you could just modify the generated source.

If you choose to modify the generated source, you will probably want to just extract the proxy class and delete the webservice reference:

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.

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.

Good luck.


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.


If you generate the code with WSDL you can override the GetWebRequest() method which will allow you to add the Authorization header

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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消