开发者

Set ClientCredentials without using a Proxy?

Is it possible to set ClientCredentials without using a generated proxy? I have seen something about using a ChannelFactory, but is i开发者_Go百科t possible to do it without this as well?


In order to call a WCF service from a client, you need a proxy (unless you want to hardcode the SOAP request yourself, which isn't something too easy to do, especially if you're dealing with security). The proxy can be created either using one of the tools to generate the proxy (Add Service Reference, svcutil, etc), or by using ChannelFactory<T>. If you're using a generated proxy, you'd use the ClientCredentials property of the proxy (inherited from the base class ClientBase<T>. If you use the ChannelFactory<T>, you'd set them in the Credentials property of the channel factory.


Here is the sample code:

EndpointAddress endpointAddress = new EndpointAddress("http://localhost:8888/TestSevice");
WSHttpBinding wsHttpBinding = new WSHttpBinding();
ChannelFactory<ISomeServiceInterface> iFactory = new ChannelFactory<ISomeServiceInterface>(wsHttpBinding, endpointAddress);

var clientCredentials = new ClientCredentials();
clientCredentials.UserName.UserName = "sudipto";
clientCredentials.UserName.Password = "sudipto";

iFactory.Endpoint.Behaviors.RemoveAll<ClientCredentials>();
iFactory.Endpoint.Behaviors.Add(clientCredentials);

var isomeService= iFactory.CreateChannel();
isomeService.SomeFunctionToCall("parameterToTheService");
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜