简体   繁体   English

在C#中使用Web服务:基本身份验证和动态端点URL

[英]Consuming Web Service in C#: Basic Authentification and dynamic endpoint url

I am trying to call a soap web service from c#. 我试图从c#调用soap web服务。 With static URL and without authentification everything works well. 使用静态URL并且没有身份验证一切正常。 I used wsdl and csc to generate an dll and also worked with web references. 我使用wsdl和csc生成一个dll,并使用Web引用。 This point was the easy part. 这一点很简单。

For the dynamic URL I have seen http://www.codeproject.com/KB/XML/wsdldynamicurl.aspx but this is from 2005 and I don't known if this is obsolete. 对于动态URL,我见过http://www.codeproject.com/KB/XML/wsdldynamicurl.aspx,但这是2005年,我不知道这是否已过时。 Is it correct to use the "normal" references? 使用“正常”引用是否正确?

My Web Service is using basic authentication but I cannot figure out how to give the user/password. 我的Web服务正在使用基本身份验证,但我无法弄清楚如何提供用户/密码。

I have already seen http://benpowell.org/supporting-the-ws-i-basic-profile-password-digest-in-a-wcf-client-proxy/ but this looks really complicated and I hope that there will be a simpler way to implement the basic authentication. 我已经看过http://benpowell.org/supporting-the-ws-i-basic-profile-password-digest-in-a-wcf-client-proxy/但这看起来很复杂,我希望会有一种更简单的方法来实现基本身份验证。

To implement basic authentication for your web request you will have to use NetworkCredential for your request. 要为您的Web请求实施基本身份验证,您必须使用NetworkCredential来处理您的请求。

NetworkCredential creds = new NetworkCredential(user,password);

WebRequest req = WebRequest.Create(Url);
req.Credentials = creds;

If you are using Web Reference for accessing web service; 如果您使用Web Reference访问Web服务; while you create a object of your proxy class just assign credentials to it. 在创建代理类的对象时,只需为其分配凭据即可。

NetworkCredential creds = new NetworkCredential(user,password);
proxy.Credentials = creds ;
//call your web methods here.

For dynamic URL the article, you have mentioned, should work. 对于动态URL,您提到的文章应该可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM