我们目前正在Sitecore中实施WCF服务以执行某些任务。但是,我们希望保护和验证这些交互,以保持Sitecore安全模型的完整性。
我们使用以下配置进行身份验证(仅相关配置和匿名化):
<service name="Services.MailService" behaviorConfiguration="serviceBehavior">
<endpoint address="" binding="wsHttpBinding" contract="Interfaces.IMailService"/>
</service>
<behavior name="serviceBehavior">
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="Services.Authentication.CustomServiceAuthentication, MyLibrary" />
</serviceCredentials>
</behavior>
<wsHttpBinding>
<binding>
<security mode="TransportWithMessageCredential">
<message clientCredentialType="UserName" />
<transport clientCredentialType="None">
</transport>
</security>
</binding>
</wsHttpBinding>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true"/>
定制验证器从UserNamePasswordValidator继承,并使用标准Sitecore.Security.Authentication.AuthenticationManager.Login()方法登录用户。在此确切时刻,用户确实已登录,并显示为Sitecore.Context.User。但是当到达WCF方法本身时,此身份验证就消失了。(由于匿名用户而导致来自Sitecore的访问异常没有添加项权限)
经过一些测试并研究了交互之后,我注意到问题是WCF使用了多个消息,因此使用了多个HttpContext。Cookie和登录名不会在请求之间保留。更深入地看,我注意到System.ServiceModel.ServiceSecurityContext.Current确实保留了安全性登录名,但是只有在输入WCF方法后才会显示(如果无法在Sitecore httpBeginRequest管道中使用此方法来在UserResolver上标识和登录用户, )
如何确保整个调用过程中对asp.net和wcf都进行了正确的身份验证?
最后,由于我们将InstanceContextMode设置为PerCall,因此通过在服务的构造函数中包含以下内容来解决此问题:
// Handle login for Sitecore to sync with the WCF security context
if (ServiceSecurityContext.Current != null)
{
AuthenticationManager.Login(
string.Format("{0}\\{1}", "yoursitecoredomain", ServiceSecurityContext.Current.PrimaryIdentity.Name));
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句