我正在迁移服务引用以使用频道工厂。
我将接口从服务实现拆分为一个单独的类库。
IService
Service
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请求未经授权使用客户端身份验证方案&#39; Anonymous&#39;。从服务器收到的身份验证标头是“Negotiate,NTLM&#39;
”
我在这里缺少什么?
答案 0 :(得分:1)
如果您查看自己的配置,就会发现自己定义的basicHttpBinding
的安全模式为TransportCredentialOnly
。
但是,在代码中创建basicHttpBinding
时,您没有指定安全模式(默认值为BasicHttpSecurityMode.None
)。我认为您需要将构建调用更改为:
BasicHttpBinding basicHttpBinding =
new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly);
然后您应该具有与配置相同的设置