【问题标题】:Pass SecurityException through multiple hops of WCF services通过 WCF 服务的多个跃点传递 SecurityException
【发布时间】:2015-12-18 16:11:35
【问题描述】:

我有一个充当 WCF 客户端的 ASP.NET MVC/WebApi 应用程序。我的 WCF 服务正在通过 ClaimsPrincipalPermission 进行授权,如果当前用户和数据的组合不匹配(例如,用户试图访问其他用户的数据),则另外抛出 SecurityException

我在过滤器中将其捕获为 SecurityAccessDeniedException 并返回状态代码 403。

但这仅适用于涉及单跳的通信。如果服务本身与另一个服务通信,那么SecurityAccessDeniedException 在到达 Web 应用程序之前会变成FaultException

示例流程:

Web client --> Service A [SecurityException] --> [SecurityAccessDeniedException] Web

Web client --> Service A --> Service B [SecurityException] 
  --> [SecurityAccessDeniedException] Service A [FaultException] --> [FaultException] Web

我尝试实现一个处理SecurityAccessDeniedExceptionIErrorHandler,但我不能只在那里抛出一个SecurityException

我有哪些选择可以将SecurityException 传播到 Web 客户端?

或者:如何在我的错误处理程序中构造一条消息,然后客户端将其正确解释为SecurityAccessDeniedException

【问题讨论】:

    标签: c# wcf exception authorization


    【解决方案1】:

    我现在使用一个IErrorHandler,它会生成一个对应的FaultException。在调用者上,这会生成另一个SecurityAccessDeniedException,然后我可以使用相同的IErrorHandler 等处理它。

    public void ProvideFault(Exception error, MessageVersion version, ref Message message)
    {
        if (error is SecurityAccessDeniedException)
        {
            var code = FaultCode.CreateSenderFaultCode(
                "FailedAuthentication",
                "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
    
            var faultText = new FaultReasonText(error.Message, CultureInfo.CurrentCulture);
    
            var faultException = new FaultException(new FaultReason(faultText), code);
            MessageFault messageFault = faultException.CreateMessageFault();
            message = Message.CreateMessage(version, messageFault, "FailedAuthentication"); // the last parameter might not be semantically correct...
        }
    }
    

    我通过查看http://leastprivilege.com/2007/11/01/wcf-and-securityaccessdeniedexception/得到了这个想法

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-26
      • 1970-01-01
      • 1970-01-01
      • 2012-05-03
      • 1970-01-01
      相关资源
      最近更新 更多