C# 使用通道工厂将身份验证令牌添加到WCF服务调用中

C# 使用通道工厂将身份验证令牌添加到WCF服务调用中,c#,wcf,http-headers,bearer-token,channelfactory,C#,Wcf,Http Headers,Bearer Token,Channelfactory,我已经使用以下方法创建了通道工厂 var client = GetMyChannelFactory<MyService>(); var myService = client.CreateChannel(); //Add token before this as following method cannot be called by anonymous var result = myService.GetResult(); internal ChannelFactory<T

我已经使用以下方法创建了通道工厂

var client = GetMyChannelFactory<MyService>();
var myService = client.CreateChannel();
//Add token before this as following method cannot be called by anonymous
var result = myService.GetResult();


internal ChannelFactory<T> GetFirmChannelFactory<T>()
{
    BasicHttpBinding basicHttpBinding = GetBasicHttpBinding();
    string url = "example.com";
    EndpointAddress address = new EndpointAddress(url);
    return new ChannelFactory<T>(basicHttpBinding, address);
}
我尝试添加端点行为,但没有成功


如何将授权承载令牌添加到WCF请求头中?

您似乎希望在特定请求中添加自定义Http头。添加HTTP头通常有两种方法

Uri uri = new Uri("http://10.157.13.69:16666");
            BasicHttpBinding binding = new BasicHttpBinding();
            ChannelFactory<ITestService> factory = new ChannelFactory<ITestService>(binding, new EndpointAddress(uri));
            ITestService service = factory.CreateChannel();
            using (new OperationContextScope((IClientChannel)service))
            {
                //first method to add HTTP header.
                //HttpRequestMessageProperty request = new HttpRequestMessageProperty();
                //request.Headers["MyHttpheader"] = "myvalue";
                //OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = request;
                OperationContext oc = OperationContext.Current;
                WebOperationContext woc = new WebOperationContext(oc);
                woc.OutgoingRequest.Headers.Add("myhttpheader", "myvalue");
                //invocation, only valid in this request.
                var result = service.GetResult();
                Console.WriteLine(result);
            }
结果。 我们需要注意的是,请求和特定的Http头仅在OperationContextScope中有效。释放OperationContextScope后,即OperationContextScope外部的调用不会附加特定的Http头。 如果我们希望在每个请求中添加一个持久的HTTP报头,我们可以考虑使用下面的接口。 如果有什么我能帮忙的,请随时告诉我

Uri uri = new Uri("http://10.157.13.69:16666");
            BasicHttpBinding binding = new BasicHttpBinding();
            ChannelFactory<ITestService> factory = new ChannelFactory<ITestService>(binding, new EndpointAddress(uri));
            ITestService service = factory.CreateChannel();
            using (new OperationContextScope((IClientChannel)service))
            {
                //first method to add HTTP header.
                //HttpRequestMessageProperty request = new HttpRequestMessageProperty();
                //request.Headers["MyHttpheader"] = "myvalue";
                //OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = request;
                OperationContext oc = OperationContext.Current;
                WebOperationContext woc = new WebOperationContext(oc);
                woc.OutgoingRequest.Headers.Add("myhttpheader", "myvalue");
                //invocation, only valid in this request.
                var result = service.GetResult();
                Console.WriteLine(result);
            }