【问题标题】:How to apply basic authentication in WCF?如何在 WCF 中应用基本身份验证?
【发布时间】:2020-02-14 01:05:36
【问题描述】:

我正在实现 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();
            }
        }
    }
}

所以我可以创建一个 WCF clientChannel

var client = _wcfClientFactory.CreateClient<ICrmService>(address);

如果 WCF 没有任何身份验证,它可以正常工作。现在,我们要向这个工厂添加身份验证。我该怎么做?我试过下面的代码

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

但它会产生异常并要求输入用户名和密码。如何设置密码和用户名?

变量 factory 有一个 Credential 成员,但它是 get only。这就是为什么我认为它必须是一种在调用 CreateChannel 之前设置凭据的方法

谢谢

【问题讨论】:

    标签: c# .net wcf wcf-security


    【解决方案1】:

    这里的描述可能有问题,你的想法是对的,这里我们可以设置客户端认证凭证。

    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();
    

    另外请注意,通道工厂使用的绑定类型要与服务端的绑定类型保持一致。
    如果问题仍然存在,请随时告诉我。

    【讨论】:

    • 酷,工作。有一个可能的 .net 错误。当我编写 factory.Credentials.UserName 代码时,VS 显示 UserName 只能获取,这就是为什么我认为我们不能使用 factory.Credentials.UserName.UseName 来设置值!
    猜你喜欢
    • 2012-01-03
    • 1970-01-01
    • 1970-01-01
    • 2011-12-06
    • 2011-11-03
    • 1970-01-01
    • 1970-01-01
    • 2011-03-18
    • 2011-10-25
    相关资源
    最近更新 更多