我正在迁移到 Azure,它使用安全 token 来对用户进行身份验证。因此,现在我在消息 header 中传递安全 token ,并使用服务器端的 IDispatchMessageInspector 行为来读取它。基础机制工作得很好,但是当 token 无法通过身份验证时,我需要拒绝它,就像它未能通过 UserNamePasswordValidator 一样。我如何完成这段代码的任何想法:
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
if (!this.IsAuthenticated(securityToken))
{
<Return Error Message Structure>
}
}
最佳答案
简单的选择是抛出 FaultException .
object IDispatchMessageInspector.AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
{
try
{
if (!this.IsAuthenticated(securityToken))
{
throw new FaultException<string>("some message");
}
}
catch (FaultException e)
{
throw new FaultException<string>(e.Message);
}
return null;
}
如果您想要更优雅的东西并控制返回的 HttpStatus 代码(默认为 500),那么您可以实现一个端点行为扩展,它注册一个监视错误的调度消息检查器。请参阅以下帖子:
http://www.shulerent.com/2016/05/31/returning-custom-http-status-codes-for-wcf-soap-exceptions/
关于wcf - 如何在 WCF 中拒绝消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42355885/