【问题标题】:Cryptic "Access is Denied" exception from WCF serviceWCF 服务的神秘“拒绝访问”异常
【发布时间】:2010-07-04 08:55:36
【问题描述】:

我有一个带有自定义身份验证的安全 WCF 服务。当我对其进行压力测试时——同时有几十个客户端连接时,我的服务器端日志中经常会出现以下异常:

System.ServiceModel.FaultException: Access is denied.
   at System.ServiceModel.Dispatcher.AuthorizationBehavior.Authorize(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

我已经通过 System.Diagnostics 启用了跟踪,但这只会让我获得更长的堆栈跟踪:

System.ServiceModel.Dispatcher.AuthorizationBehavior.Authorize(MessageRpc& rpc)
System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc)
System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
System.ServiceModel.Dispatcher.ChannelHandler.DispatchAndReleasePump(RequestContext request, Boolean cleanThread, OperationContext currentOperationContext)
System.ServiceModel.Dispatcher.ChannelHandler.HandleRequest(RequestContext request, OperationContext currentOperationContext)
System.ServiceModel.Dispatcher.ChannelHandler.AsyncMessagePump(IAsyncResult result)
System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.WorkItem.Invoke2()
System.Security.SecurityContext.Run(SecurityContext securityContext, ContextCallback callback, Object state)
System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.WorkItem.Invoke()
System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.ProcessCallbacks()
System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.CompletionCallback(Object state)
System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.ScheduledOverlapped.IOCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
System.ServiceModel.Diagnostics.Utility.IOCompletionThunk.UnhandledExceptionFrame(UInt32 error, UInt32 bytesRead, NativeOverlapped* nativeOverlapped)
System.Threading._IOCompletionCallback.PerformIOCompletionCallback

为什么会发生这种情况,我如何才能了解更多关于这里出了什么问题?

谢谢, 尿尿

【问题讨论】:

    标签: wcf wcf-security


    【解决方案1】:

    仍然没有解决问题,但我确信它确实在我自己的自定义身份验证机制中 - 所以我接受 Henk 的回答。

    对我来说,当我像这样为 System.IdentityModel 添加诊断时发现了确凿的证据:

    <system.diagnostics>
            <sources>
                <source name="System.IdentityModel" switchValue="All">
                    <listeners>
                        <add type="System.Diagnostics.DefaultTraceListener" name="Default">
                            <filter type="" />
                        </add>
                        <add name="IdentityModelListener">
                            <filter type="" />
                        </add>
                    </listeners>
                </source>
            </sources>
            <sharedListeners>
                <add initializeData="C:\Tracing\App_identitymodellog.svclog" type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                 name="IdentityModelListener" traceOutputOptions="Timestamp, Callstack">
                    <filter type="" />
                </add>
            </sharedListeners>
            <trace autoflush="true" />
        </system.diagnostics>
    

    在结果痕迹中看到了这个:

    System.Environment.get_StackTrace() System.Diagnostics.TraceEventCache.get_Callstack() System.Diagnostics.XmlWriterTraceListener.WriteFooter(TraceEventCache 事件缓存) System.Diagnostics.TraceSource.TraceData(TraceEventType eventType,Int32 id,对象数据) System.ServiceModel.Diagnostics.DiagnosticTrace.TraceEvent(TraceEventType 类型、TraceCode 代码、字符串 描述,TraceRecord 跟踪, 异常异常,对象来源) System.ServiceModel.Diagnostics.DiagnosticTrace.TraceEvent(TraceEventType 类型、TraceCode 代码、字符串 描述) System.IdentityModel.SecurityUtils.CreateDefaultAuthorizationContext(IList`1 授权政策) System.ServiceModel.ServiceSecurityContext.get_AuthorizationContext() Foo.Bar..Core.Security.SessionAuthorizationManager.CheckClaimSet(OperationContext 操作上下文)在... Foo.Bar..Core.Security.SessionAuthorizationManager.CheckAccess(OperationContext operationContext,消息&消息)在 ... > System.ServiceModel.Dispatcher.AuthorizationBehavior.Authorize(MessageRpc& RPC) System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& RPC) System.ServiceModel.Dispatcher.MessageRpc.Process(布尔 isOperationContextSet) System.ServiceModel.Dispatcher.ChannelHandler.DispatchAndReleasePump(RequestContext 请求,布尔型 cleanThread, 操作上下文 当前操作上下文) System.ServiceModel.Dispatcher.ChannelHandler.HandleRequest(RequestContext 请求,操作上下文 当前操作上下文) System.ServiceModel.Dispatcher.ChannelHandler.AsyncMessagePump(IAsyncResult 结果) System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.WorkItem.Invoke2() System.Security.SecurityContext.Run(SecurityContext 安全上下文,上下文回调 回调,对象状态) System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.WorkItem.Invoke() System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.ProcessCallbacks() System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.CompletionCallback(对象 状态) System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.ScheduledOverlapped.IOCallback(UInt32 errorCode,UInt32 numBytes, NativeOverlapped* nativeOverlapped) System.ServiceModel.Diagnostics.Utility.IOCompletionThunk.UnhandledExceptionFrame(UInt32 错误,UInt32 bytesRead, NativeOverlapped* nativeOverlapped) System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode,UInt32 numBytes, NativeOverlapped* pOVERLAP)

    在 Dispatcher.AuthorizationBehavior.Authorize() 之后的下一个调用是我自己的 AuthorizationManager 实现。这很可能是问题的来源。 Authorize() 方法只是抛出 FaultException 作为结果。

    【讨论】:

    • 这条评论帮助我了解一些继承的代码库“自己的 AuthorizationManager 实现”
    【解决方案2】:

    从“自定义身份验证”和“[当] 几十个客户端同时连接”我猜想您的自定义身份验证(来自堆栈跟踪:授权部分)不是完全线程安全的。授权部分中的任何错误都可能被(错误)诊断为“拒绝访问”错误。

    InnerException 属性中可能包含更多信息。但除此之外,也许您可​​以发布一些您自己的授权代码的详细信息。

    【讨论】:

    • 谢谢亨克。这是一个很好的分析。我正在为我的自定义身份验证添加更多日志记录,稍后会更新。如何通过代码/日志访问 innerException?抛出 FaultException 的是 WCF,那么你会在哪里为它放置一个 catch 子句?
    猜你喜欢
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    • 2011-03-13
    • 2015-05-26
    • 2012-11-25
    • 1970-01-01
    • 2011-04-02
    • 2014-11-19
    相关资源
    最近更新 更多