[英]How to apply basic authentication in WCF?
I'm implementing a WcfClientFactory我正在实施 WcfClientFactory
public class WcfClientFactory : IDisposable
{
internal const string WildcardConfigurationName = "*";
//We track all channels created by this instance so they can be destroyed
private readonly List<WeakReference<IDisposable>> _disposableItems = new List<WeakReference<IDisposable>>();
public T CreateClient<T>(string configurationName = WildcardConfigurationName, string address=null)
{
var factory = new ChannelFactory<T>(configurationName);
if (!string.IsNullOrWhiteSpace(address))
{
factory.Endpoint.Address = new EndpointAddress(address);
}
var channel = factory.CreateChannel();
var clientChannel = (IClientChannel)channel;
clientChannel.Open();
_disposableItems.Add(new WeakReference<IDisposable>(clientChannel,false));
return channel;
}
void IDisposable.Dispose()
{
//No finalizer is implemented as there are no directly held scarce resources.
//Presumably the finalizers of items in disposableItems will handle their own teardown
//if it comes down to it.
foreach (var reference in _disposableItems)
{
IDisposable disposable;
if (reference.TryGetTarget(out disposable))
{
disposable.Dispose();
}
}
}
}
So I can create a WCF clientChannel所以我可以创建一个 WCF clientChannel
var client = _wcfClientFactory.CreateClient<ICrmService>(address);
It works fine if the WCF does not have any authentication.如果 WCF 没有任何身份验证,它工作正常。 Now, we want to add authentication to this factory.
现在,我们要向该工厂添加身份验证。 How can I do it?
我该怎么做? I tried below code
我试过下面的代码
public T CreateClientWithBasicAuthentication<T>(string address)
{
WSHttpBinding myBinding = new WSHttpBinding();
myBinding.Security.Mode = SecurityMode.Transport;
myBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
var factory = new ChannelFactory<T>(myBinding, new EndpointAddress(address));
var channel = factory.CreateChannel();
var clientChannel = (IClientChannel)channel;
////CrmServiceClient csc = (CrmServiceClient)channel;
////csc.ClientCredentials.UserName.UserName = _UserName;
////csc.ClientCredentials.UserName.Password = _Password;
clientChannel.Open();
_disposableItems.Add(new WeakReference<IDisposable>(clientChannel, false));
return channel;
}
But it generates exception and asks for UserName and Password.但它会生成异常并要求输入用户名和密码。 How can I set the password and username?
如何设置密码和用户名?
The variable factory has a member of Credential, but it is get only.变量factory有一个Credential成员,不过是get only。 That's why I think it has to be a way to set credential before call CreateChannel
这就是为什么我认为它必须是一种在调用 CreateChannel 之前设置凭据的方法
Thanks谢谢
There may be a problem with the description here, your idea is right, here we can set the client authentication credentials.这里的描述可能有问题,你的想法是对的,这里我们可以设置客户端的认证凭证。
var factory = new ChannelFactory<T>(myBinding, new EndpointAddress(address));
//for HttpClientCredentialType.Basic
factory.Credentials.UserName.UserName = "administrator";
factory.Credentials.UserName.Password = "abcd1234!";
//for window credentials
//factory.Credentials.Windows.ClientCredential.UserName = "adminsitrator";
//factory.Credentials.Windows.ClientCredential.Password = "abcd1234!";
var channel = factory.CreateChannel();
Besides, please note that the binding type that the channel factory uses should be consistent with the binding type on the server-side.此外,请注意通道工厂使用的绑定类型应与服务器端的绑定类型保持一致。
Feel free to let me know if the problem still exists.如果问题仍然存在,请随时告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.