【发布时间】:2011-10-30 23:33:15
【问题描述】:
我使用 Silverlight4、RIA 服务构建了一个应用程序,并且我正在使用 ASP.NET Membership 进行身份验证/授权。
我的 web.config 有这个:
<system.web>
<sessionState timeout="20"/>
<authentication mode="Forms">
<forms name="_ASPXAUTH" timeout="20"/>
</authentication>
我已经阅读了许多关于如何在客户端处理身份验证/会话超时的不同策略。也就是说:如果客户端空闲了 x 分钟(此处为 20 分钟),然后他们对触发 RIA/WCF 调用的 UI 执行了某些操作,我想捕获该事件并进行适当处理(例如,将它们带回登录屏幕)——简而言之:我需要一种方法来区分真正的服务器端 DomainException 和身份验证失败,因为会话超时。
AFAIK:没有类型的异常或属性可以确定这一点。我能够确定这一点的唯一方法 - 这似乎是一个黑客:检查错误的消息字符串并寻找类似“拒绝访问”或“拒绝”的内容。例如:像这样:
if (ex.Message.Contains("denied"))
// this is probably an auth failure b/c of a session timeout
所以,这就是我目前正在做的事情,如果我使用 VS2010 的内置服务器运行和调试,或者我在 localhost IIS 中运行,它就可以工作。如果我将超时设置为 1 分钟,登录,等待超过一分钟并触发另一个调用,我在异常上断点并输入上面的 if 代码块,一切都很好。
然后我将应用程序部署到远程 IIS7 服务器并尝试相同的测试,但它不起作用。所以,我添加了日志跟踪,这是发生异常的事件:
<E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent">
<System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system">
<EventID>131076</EventID>
<Type>3</Type>
<SubType Name="Error">0</SubType>
<Level>2</Level>
<TimeCreated SystemTime="2011-10-30T22:13:54.6425781Z" />
<Source Name="System.ServiceModel" />
<Correlation ActivityID="{20c26991-372f-430f-913b-1b72a261863d}" />
<Execution ProcessName="w3wp" ProcessID="4316" ThreadID="24" />
<Channel />
<Computer>TESTPROD-HOST</Computer>
</System>
<ApplicationData>
<TraceData>
<DataItem>
<TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Error">
<TraceIdentifier>http://msdn.microsoft.com/en-US/library/System.ServiceModel.Diagnostics.TraceHandledException.aspx</TraceIdentifier>
<Description>Handling an exception.</Description>
<AppDomain>/LM/W3SVC/1/ROOT/sla-2-129644844652558594</AppDomain>
<Exception>
<ExceptionType>System.ServiceModel.FaultException`1[[System.ServiceModel.DomainServices.Hosting.DomainServiceFault, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]], System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType>
<Message></Message>
<StackTrace>
at System.ServiceModel.DomainServices.Hosting.QueryOperationBehavior`1.QueryOperationInvoker.InvokeCore(Object instance, Object[] inputs, Object[]& outputs)
at System.ServiceModel.DomainServices.Hosting.DomainOperationInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
</StackTrace>
<ExceptionString>System.ServiceModel.FaultException`1[System.ServiceModel.DomainServices.Hosting.DomainServiceFault]: (Fault Detail is equal to System.ServiceModel.DomainServices.Hosting.DomainServiceFault).</ExceptionString>
</Exception>
</TraceRecord>
</DataItem>
</TraceData>
</ApplicationData>
</E2ETraceEvent>
问题是我在错误消息中没有指示“拒绝”或“访问被拒绝”的字符串 - 我不确定为什么此解决方案适用于 localhost IIS 或 VS2010 主机,但不适用于远程IIS7 服务器。我在这里缺少一些晦涩的配置设置吗?一般来说,有没有更好的方法来做到这一点?
【问题讨论】:
标签: silverlight silverlight-4.0 asp.net-membership wcf-ria-services