C# 如何使用自定义IChannel或IChannelFactory访问客户端凭据?

C# 如何使用自定义IChannel或IChannelFactory访问客户端凭据?,c#,wcf,credentials,C#,Wcf,Credentials,我正在WCF中创建一个自定义通道,以实现自定义安全协议。不,不要逃跑!没那么可怕 验证服务上的协议相对简单。困难的部分是根据客户端凭据向请求添加安全信息 我要做的是从通道实现中访问ClientCredentials对象(连接到使用中的ClientProxy)。通常,我会通过ServiceEndpoint实例上的属性访问我试图访问的端点: var credentials = channel.Endpoint.Behaviors.Find<ClientCredentials>(); v

我正在WCF中创建一个自定义通道,以实现自定义安全协议。不,不要逃跑!没那么可怕

验证服务上的协议相对简单。困难的部分是根据客户端凭据向请求添加安全信息

我要做的是从通道实现中访问
ClientCredentials
对象(连接到使用中的
ClientProxy
)。通常,我会通过
ServiceEndpoint
实例上的属性访问我试图访问的端点:

var credentials = channel.Endpoint.Behaviors.Find<ClientCredentials>();
var-credentials=channel.Endpoint.Behaviors.Find();
然而,我似乎找不到一种方法从通道本身内部访问与该通道相关联的服务端点-从
ChannelBase
类中几乎没有元数据可用


是否有方法获取与我的通道关联的端点?是否有其他方法可以访问客户端上的客户端凭据?

实现您自己的客户端服务

比如,

    using System;
    using System.ServiceModel;
    using System.ServiceModel.Channels;


public class UserClient : ClientBase<IAsyncESPUserService> , IESPUserService
{
        /// <summary>
        /// Constructor - No Parameters, this will use a default target endpoint.
        /// </summary>
        public UserClient() : base() { }

        /// <summary>
        /// Constructor - Binding and Address Parameters
        /// </summary>
        /// <param name="binding">How we are communicating.</param>
        /// <param name="address">The address we are communicating to.</param>
        public UserClient(Binding binding, EndpointAddress address) : base(binding, address) { }

        /// <summary>
        /// Constructor - Configuration Name Parameter
        /// </summary>
        /// <param name="endpointConfigurationName">The name of the configuration in ServiceReferences.ClientConfig. </param>
        public UserClient(string endpointConfigurationName) : base(endpointConfigurationName) { }

           //Implement your async service calls here       
}
使用系统;
使用System.ServiceModel;
使用System.ServiceModel.Channel;
公共类UserClient:ClientBase,IESPUserService
{
/// 
///构造函数-无参数,这将使用默认的目标端点。
/// 
public UserClient():base(){}
/// 
///构造函数-绑定和地址参数
/// 
///我们是如何沟通的。
///我们通讯的地址。
公共用户客户端(绑定,端点地址):基(绑定,地址){}
/// 
///构造函数-配置名称参数
/// 
///ServiceReferences.ClientConfig中配置的名称。
公共用户客户端(字符串endpointConfigurationName):基本(endpointConfigurationName){}
//在这里实现异步服务调用
}
现在叫它

//Create using the default endpoint
UserClient client = new UserClient();


//Set user name and password with call
System.ServiceModel.Description.ClientCredentials loginCredentials = new System.ServiceModel.Description.ClientCredentials();
loginCredentials.UserName.UserName = "test";
loginCredentials.UserName.Password = "test";

//Attach Credentials, Can't do this in config file
var defaultCredentials = client.ChannelFactory.Endpoint.Behaviors.Find<System.ServiceModel.Description.ClientCredentials>();
client.ChannelFactory.Endpoint.Behaviors.Remove(defaultCredentials);
client.ChannelFactory.Endpoint.Behaviors.Add(loginCredentials);

//Now make a call to a service method...
//使用默认端点创建
UserClient=newuserclient();
//使用call设置用户名和密码
System.ServiceModel.Description.ClientCredentials loginCredentials=新的System.ServiceModel.Description.ClientCredentials();
loginCredentials.UserName.UserName=“测试”;
loginCredentials.UserName.Password=“测试”;
//附加凭据,无法在配置文件中执行此操作
var defaultCredentials=client.ChannelFactory.Endpoint.Behaviors.Find();
client.ChannelFactory.Endpoint.Behaviors.Remove(defaultCredentials);
client.ChannelFactory.Endpoint.Behaviors.Add(loginCredentials);
//现在调用一个服务方法。。。

标准安全通道在内部不使用
客户端凭据。他们转而与
SecurityTokenManager
对话,后者由
ClientCredentials
构建。我建议使用一些反汇编程序来浏览整个实现


通常,您的
BindingElement
应该同时构建
ChannelLister
ChannelFactory
,并向它们传递它们所需的所有信息。

实际上,这根本不能使内部通道获得这些信息。多么有趣
SecurityTokenManager
是客户端和服务器令牌凭据管理器的基类。我将思考如何在WCF管道中使用它们以获得更多答案。这花了很多时间,但最终我将
BindingContext
添加到我的侦听器和工厂构造函数中,这使我能够访问所需的行为和公开的凭据。嗨,悲剧。。。我正试图做同样的事情…但我不能做…你能做到同样的事情吗。如果是的话,你能给我一些指导吗。