我有一个继承 RoleProvider 的自定义角色提供程序。在网络应用程序中,这可以正常工作,没有问题。
但是,我也在 WCF 服务中使用它,并且在进入它时遇到了很大的问题。以至于我怀疑它根本没有被击中。如果我完全打开任何主体权限,我会被拒绝访问,并且堆栈跟踪完全没有帮助。甚至 WCF 跟踪也对确定发生了什么非常有帮助。
我知道 TennisRoleProvider 使用它的默认构造函数并通过测试验证了它的方法。这似乎是一个集成问题。
所以片段...
编辑:我已经将角色提供者移动到服务程序集中,因为阅读了一些关于需要涉及 GAK 和密钥(需要完全信任运行)的内容。我走了那条路,但事情仍然没有奏效,所以决定简单地将东西移到服务项目中以简化。仍然没有喜悦。
<roleManager defaultProvider="TennisRoleProvider"
enabled="true"
>
<providers>
<clear/>
<add name="TennisRoleProvider"
type="Tennis.Security.TennisRoleProvider, Tennis.Security" />
</providers>
</roleManager>
<bindings>
<wsHttpBinding>
<binding name="wsHttpUserName">
<security mode="TransportWithMessageCredential">
<message clientCredentialType="UserName"/>
<transport clientCredentialType="None"/>
</security>
</binding>
</wsHttpBinding>
<bindings/>
<behavior name="RoleBehavior">
<serviceCredentials>
<serviceCertificate findValue="john-pc"
storeLocation="LocalMachine"
storeName="My"
x509FindType="FindBySubjectName"/>
<userNameAuthentication userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType="Tennis.Components.TennisUserValidator, Tennis.Components"/>
</serviceCredentials>
<serviceAuthorization principalPermissionMode="UseAspNetRoles"
roleProviderName="TennisRoleProvider">
</serviceAuthorization>
<serviceMetadata httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
<errorHandler />
</behavior>
<services>
<service name="Tennis.Service.Services"
behaviorConfiguration="RoleBehavior">
<endpoint address="Family"
binding="wsHttpBinding"
bindingConfiguration="wsHttpUserName"
contract="Tennis.Service.Contracts.IFamilyAdmin"
/>
</service>
</services>
然后在服务方法上我有以下内容(Roles.Family admin 是一个字符串)
[PrincipalPermission(SecurityAction.Demand, Name = Roles.FamilyAdmin)]
public VoidResult<SuccessEnum> UpdateFamily(Family family)
{
}
所以有两个问题...... 1)我做错了什么?2) 我怎样才能进入 WCF 以确定到底出了什么问题?
干杯
日志中错误的堆栈跟踪如下注意其中的权限与我上面使用的权限不同(Namley 'authorized' 而不是 'FamilyAdmin'。但实际上这些值匹配并且用户拥有正确的权限。
<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="Warning">0</SubType>
<Level>4</Level>
<TimeCreated SystemTime="2012-06-29T12:45:30.2469191Z" />
<Source Name="System.ServiceModel" />
<Correlation ActivityID="{6e59b4f4-d59b-42eb-ad8e-4d5853f72900}" />
<Execution ProcessName="w3wp" ProcessID="9388" ThreadID="18" />
<Channel />
<Computer>JOHNN-PC</Computer>
</System>
<ApplicationData>
<TraceData>
<DataItem>
<TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Warning">
<TraceIdentifier>http://msdn.microsoft.com/en-GB/library/System.ServiceModel.Diagnostics.TraceHandledException.aspx</TraceIdentifier>
<Description>Handling an exception.</Description>
<AppDomain>/LM/W3SVC/2/ROOT/Tennis-1-129854474506679191</AppDomain>
<Exception>
<ExceptionType>System.Security.SecurityException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType>
<Message>Request for principal permission failed.</Message>
<StackTrace>
at System.Security.Permissions.PrincipalPermission.ThrowSecurityException()
at System.Security.Permissions.PrincipalPermission.Demand()
at System.Security.PermissionSet.DemandNonCAS()
at Nomical.Tennis.Service.Services.GetBookingsForUser(DateTime start, DateTime end) in c:\tfs\Tennis\TennisSolution\TennisCourts\Services.svc.cs:line 388
at SyncInvokeGetBookingsForUser(Object , Object[] , Object[] )
at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
</StackTrace>
<ExceptionString>System.Security.SecurityException: Request for principal permission failed.
at System.Security.Permissions.PrincipalPermission.ThrowSecurityException()
at System.Security.Permissions.PrincipalPermission.Demand()
at System.Security.PermissionSet.DemandNonCAS()
at Tennis.Service.Services.GetBookingsForUser(DateTime start, DateTime end) in c:\tfs\Tennis\TennisSolution\TennisCourts\Services.svc.cs:line 388
at SyncInvokeGetBookingsForUser(Object , Object[] , Object[] )
at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
The action that failed was:
Demand
The type of the first permission that failed was:
System.Security.Permissions.PrincipalPermission
The first permission that failed was:
<IPermission class="System.Security.Permissions.PrincipalPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
version="1">
<Identity Authenticated="true"
ID="Authorised"/>
</IPermission>
The demand was for:
<IPermission class="System.Security.Permissions.PrincipalPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
version="1">
<Identity Authenticated="true"
ID="Authorised"/>
</IPermission>
The assembly or AppDomain that failed was:
mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionString>
</Exception>
</TraceRecord>
</DataItem>
</TraceData>
</ApplicationData>
</E2ETraceEvent>
编辑:根据下面的答案,我在构造函数中添加了几行代码——虽然它们没有实现任何目标,但鼓励我询问线程静态类。
编辑:给出一个关于日志更新日志的问题,以表明它确实来自日志 - 或者我很困惑;)
TennisRoleProvider 在其非公共成员中被它引用 - 此外,当我覆盖 Name 以使其返回某些内容时,我会认识到这是由它返回的。