如何使用自定义 IChannel 或 IChannelFactory 访问 ClientCredentials?

发布于 2024-12-02 20:17:44 字数 643 浏览 0 评论 0原文

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

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

我想要做的是从我的通道实现中访问 ClientCredentials 对象(附加到正在使用的 ClientProxy 的对象)。通常,我可以通过 访问此内容我尝试访问的端点的 ServiceEndpoint 实例上的 Behaviors 属性:

var credentials = channel.Endpoint.Behaviors.Find<ClientCredentials>();

但是,我似乎找不到访问服务端点的方法该频道关联于从通道本身内部 - 从 ChannelBase 类中可用的元数据几乎为零。

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

I'm creating a custom channel in WCF in order to implement a custom security protocol. No, don't run away! It's not that scary!

Verifying the protocol on the service is relatively simple. The hard part is adding the security information to the request based on the client credentials.

What I want to do is access the ClientCredentials object (the one attached to the ClientProxy in use) from within my channel implementation. Normally, I'd get access to this through the Behaviors property on the ServiceEndpoint instance for the endpoint I'm trying to reach:

var credentials = channel.Endpoint.Behaviors.Find<ClientCredentials>();

However, I can't seem to find a way to access the service endpoint the channel is associated with from within the channel itself - almost zero metadata is available from the ChannelBase class.

Is there a way to get the endpoint my channel is associated with? Is there any alternative way to access the client credentials on the client-side?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

櫻之舞 2024-12-09 20:17:44

标准安全通道不在内部使用 ClientCredentials。相反,它们与由 ClientCredentials 构造的 SecurityTokenManager 进行通信。我建议使用一些反汇编程序来浏览整个实现。

一般来说,您的 BindingElement 应该构建 ChannelListerChannelFactory 并向它们传递所需的所有信息。

Standard security channels don't use ClientCredentials internally. They instead talk with SecurityTokenManager which is constructed from ClientCredentials. I recommend using some disassembler to browse whole implementation.

Generally your BindingElement should build both ChannelLister and ChannelFactory and pass them all information they need.

蓝眼睛不忧郁 2024-12-09 20:17:44

实施您自己的客户服务。

例如;

    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       
}

现在称呼它...

//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...

Implement you own client service.

For example;

    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       
}

Now call it...

//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...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文