【问题标题】:Migrating Service reference to Channel Factory将服务引用迁移到通道工厂
【发布时间】:2014-09-26 05:31:43
【问题描述】:

我正在迁移服务引用以使用通道工厂。

我将接口从服务实现拆分到一个单独的类库中。

  • 类库:IService
  • 类库:Service
  • Web 应用程序:参考IService

代码:

配置

<bindings>
      <basicHttpBinding>
        <binding name="basicHttpBindingEndpoint" maxReceivedMessageSize="5242880">
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Windows" proxyCredentialType="None" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>

类:

public class ProxyManager
{
        internal static ConcurrentDictionary<string, ChannelFactory<IService>> proxies = new ConcurrentDictionary<string, ChannelFactory<IService>>();

        internal static ChannelFactory<IService> CreateChannelFactory()
        {
            Global.Logger.Info("ProxyManager:CreateChannelFactory");
            BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
            basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
            basicHttpBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;            
            EndpointAddress endpointAddress = new EndpointAddress("http://domain/Service.svc");
            var channelFactory = new ChannelFactory<IService>(basicHttpBinding, endpointAddress);
            channelFactory.Credentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;
            return channelFactory;
        }

        internal static IService GetProxy(string key)
         {
             Global.Logger.Info("ProxyManager:GetProxy");
             return proxies.GetOrAdd(key, m => CreateChannelFactory()).CreateChannel();
         }

        internal static bool RemoveProxy(string key)
        {
            Global.Logger.Info("ProxyManager:RemoveProxy");
            ChannelFactory<IService> proxy;
            return proxies.TryRemove(key, out proxy);
        }
}

全球:

public static IService ServiceProxy
{
            get
            {
                return ProxyManager.GetProxy("Service");
            }
}

用法:

ServiceProxy.Method();

错误:

HTTP 请求未经客户端身份验证方案“匿名”授权。从服务器收到的身份验证标头是“Negotiate,NTLM”。

我在这里错过了什么?

【问题讨论】:

    标签: c# wcf authentication proxy


    【解决方案1】:

    如果您查看您的配置,您会发现您已经使用TransportCredentialOnly 的安全模式定义了basicHttpBinding

    但是,当您在代码中创建 basicHttpBinding 时,您并未指定该安全模式(默认值为 BasicHttpSecurityMode.None)。我认为您需要将构造调用更改为:

    BasicHttpBinding basicHttpBinding = 
       new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly);
    

    那么你应该有和你的配置一样的设置

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多