如何在 wcf 服务中的令牌身份验证阶段之前拦截消息

问题描述 投票:0回答:1

我是 WCF 新手。我有一个客户端,它通过发送用户名和密码作为令牌来建立与 WCF 服务的连接。该服务有一个自定义令牌验证器,它扩展了

UserNameSecurityTokenAuthenticator
类以实现自定义验证逻辑。

我想从服务器端找出客户端 IP 地址,并在我的自定义令牌验证器类中使用它

我尝试添加消息检查器和端点检查器,但这些点从未被命中。 我有一个

customServiceCredentials
类,它扩展了
ServiceCredentials
类。这是我可以在身份验证之前拦截消息的地方吗?

我知道使用

OperationContext
我可以获取clientIp,但是我只是不知道在消息到达令牌验证器之前在哪里拦截消息

c# .net wcf wcf-security
1个回答
0
投票

我认为BeforeSendRequest方法可以在发送请求之前获取clientip。但经过你的测试,这个方法行不通。

这个方法可以尝试一下。这个方法是在一篇有点旧的帖子上发现的,但我想你可以尝试一下。

OperationContext context = OperationContext.Current;
MessageProperties prop = context.IncomingMessageProperties;
RemoteEndpointMessageProperty endpoint = 
prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
string ip = endpoint.Address;

这是原帖:获取客户端IP地址

© www.soinside.com 2019 - 2024. All rights reserved.