我已经实现了一个自定义 IDispatchMessageInspector,以解析一种自定义令牌类型。解析我分配的令牌后:
ServiceSecurityContext.Current.AuthorizationContext.Properties["ClaimsPrincipal"] = claimsPrincipal;
ServiceSecurityContext.Current.AuthorizationContext.Properties["Identities"] = identities;
Thread.CurrentPrincipal = claimsPrincipal;
我想在我的 IDispatchMessageInspector 中分配了 ClaimsPrincipal 之后,它应该在我的服务方法中可用,不幸的是我在那里有一个 WindowsPrincipal(IsAuthentificated = false)。
var currentIdentity = Thread.CurrentPrincipal as ClaimsPrincipal;
有什么想法吗?
编辑: 我的 web.config:
<services>
<service name="EchoService.TestEchoService">
<endpoint address="api" bindingConfiguration="secured" binding="webHttpBinding" behaviorConfiguration="rest" contract="EchoService.IEchoService"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceCredentials useIdentityConfiguration="true">
</serviceCredentials>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="rest">
<webHttp helpEnabled="true" automaticFormatSelectionEnabled="true"/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment>
<serviceActivations>
<add relativeAddress="echo.svc" factory="System.ServiceModel.Activation.ServiceHostFactory" service="EchoService.TestEchoService"/>
</serviceActivations>
</serviceHostingEnvironment>
</system.serviceModel>
<system.identityModel>
<identityConfiguration>
<securityTokenHandlers>
<clear/>
<add type="EchoService.Host.Tokens.SimpleWebTokenHandler,EchoService.Host"></add>
</securityTokenHandlers>
<audienceUris>
<clear/>
<add value="http://securitytestrealm/"/>
</audienceUris>
<issuerTokenResolver type="System.IdentityModel.Tokens.NamedKeyIssuerTokenResolver,System.IdentityModel.Tokens.Jwt">
<securityKey symmetricKey="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX=" name="YYYYYYYYYYYYYYYYYYY" />
</issuerTokenResolver>
</identityConfiguration>
编辑2:
调用顺序:
构造函数 => GetTokenTypeIdentifiers => TokenType
在 GetTokenTypeIdentifiers 我返回:
return new string[] { "http://schemas.microsoft.com/2009/11/identitymodel/tokens/swt" };
这个序列只有在我第一次调用我的服务时才会发生。有趣的是,之后调用任何 Handlers 方法都不会被调用。