wcf 访问控制

public class PasswordDigestChannelFactory<TPortTypeClient, TPlugin>
    where TPortTypeClient : ClientBase<TPlugin>, TPlugin, new()
    where TPlugin : class
{
    public PasswordDigestChannelFactory(string endpointConfigurationName)
    {
        _endpointConfigurationName = endpointConfigurationName;
    }

    private readonly string _endpointConfigurationName;

    public TPlugin GetClient()
    {
        var args = new[] {_endpointConfigurationName};
        var portInstance = Activator.CreateInstance(typeof (TPortTypeClient), args) as ClientBase<TPlugin>;
        
        // replace ClientCredentials with UsernameClientCredentials
        var username = "username";
        var password = "password";

        var credentials = new UsernameClientCredentials(new UsernameInfo(username, password));
        portInstance.ChannelFactory.Endpoint.Behaviors.Remove(typeof (ClientCredentials));
        portInstance.ChannelFactory.Endpoint.Behaviors.Add(credentials);
    
        return portInstance as TPlugin;
    }
}

//  StructureMap injection
For<PortType>()
    .Use((new PasswordDigestChannelFactory<PortTypeClient, PortType>("endPoint")).GetClient());

 

posted on 2015-09-25 08:01  武胜-阿伟  阅读(262)  评论(0编辑  收藏  举报