我已經實現了一個自定義IDispatchMessageInspector,以解析一個自定義標記類型。解析令牌後,我分配:IDispatchMessageInspector和Thread.CurrentPrincipal
ServiceSecurityContext.Current.AuthorizationContext.Properties["ClaimsPrincipal"] = claimsPrincipal;
ServiceSecurityContext.Current.AuthorizationContext.Properties["Identities"] = identities;
Thread.CurrentPrincipal = claimsPrincipal;
我想ClaimsPrincipal得到了我IDispatchMessageInspector分配後,它應該在我服務的方法已面市,不幸的是我有一個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>
EDIT2:
調用序列:
構造器=> GetTokenTypeIdentifiers => TokenType
在GetTokenTypeIdentifiers我返回:
return new string[] { "http://schemas.microsoft.com/2009/11/identitymodel/tokens/swt" };
如果我第一次給我的服務打電話,這個順序只會佔位。 有趣的是,Handlers方法在調用之後被調用。
我已經意識到,我在某處WCF堆棧中,CurrentPrincipal只是在服務調用之前覆蓋。問題是WCF堆棧中的正確位置在哪裏? –
我的答案中有一個鏈接。對於使用用戶名/密碼的自定義身份驗證,您可以繼承UsernamePasswordValidator。請參閱:http://msdn.microsoft.com/en-us/library/system.identitymodel.selectors.usernamepasswordvalidator.aspx –
UsernamePasswordValidator如何幫助我傳遞ClaimsPrincipal,這是我在IDispatchMessageInspector中獲得的服務方法? –