我正在尝试使用带有 REST 服务的基本 HTTP 身份验证,但调试代理显示根本没有发送身份验证标头,它们被忽略了!这是我的代码:
WebChannelFactory<IMyService> factory = new WebChannelFactory<IMyService>(
new WebHttpBinding(),
new Uri("http://10.6.90.45:8081"));
WebHttpBinding http = (WebHttpBinding) factory.Endpoint.Binding;
http.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
http.Security.Mode = WebHttpSecurityMode.TransportCredentialOnly;
factory.Credentials.UserName.UserName = "username";
factory.Credentials.UserName.Password = "password";
IMyService 代理 = factory.CreateChannel();
proxy.SomeMethod();
...
HTTP 请求未经客户端身份验证方案“基本”授权。从服务器收到的身份验证标头是“”。
服务器堆栈跟踪:在 System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest 请求,HttpWebResponse 响应,HttpChannelFactory 工厂,WebException responseException 的 System.ServiceModel.Channels.HttpChannelUtilities.ValidateAuthentication(HttpWebRequest 请求,HttpWebResponse 响应,WebException responseException,HttpChannelFactory 工厂), ChannelBinding channelBinding) at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout) at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout) at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message消息,TimeSpan 超时)在 System.ServiceModel.Channels.ServiceChannel.Call(字符串操作,布尔单向,System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs) at System.ServiceModel 的 ProxyOperationRuntime 操作,Object[] 输入,Object[] 输出,TimeSpan 超时) .Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation) at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)调用(IMessage 消息)调用(IMessage 消息)
在 [0] 处重新抛出异常:在 System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) 处的 System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) ...