填充 WCF 中的 PrimaryIdentity

2023-12-21

我使用简单的 HTTP 标头将令牌传递给 WCF 服务进行身份验证(WCF 服务需要使用 basicHTTPBinding,因此遗憾的是我无法使用固定的 ws-security 实现)。我想填充 PrimaryIdentity 对象,以便 WCF 服务可以检查它以确定经过身份验证的用户。

问题是OperationContext.Current.ServiceSecurityContext.PrimaryIdentity当我尝试填充它时,该属性是只读的。我尝试使用 SecurityTokenAuthenticators 和 IAuthorizationPolicy 对象来设置身份信息,但该路由似乎需要使用消息级安全性(例如始终发送用户名和密码),这不是我想要的。

谁能告诉我如何设置 PrimaryIdentity 字段?


您可以创建一个实现的类授权策略 http://msdn.microsoft.com/en-us/library/system.identitymodel.policy.iauthorizationpolicy.aspx。 WCF 解析所有身份令牌(X509、WS-Security 用户名/通行证等)并将它们放入您在 Evaluate 函数中获得的 evaluationContext.Properties["Identities"] 中。这将返回一个列表给您。如果您将此列表替换为包含您自己的实现类的列表身份 http://msdn.microsoft.com/en-us/library/system.security.principal.iidentity.aspx然后 WCF 会将其读入ServiceSecurityContext.Current.PrimaryIdentity。请确保该列表仅包含一项,否则您会混淆 WCF 和PrimaryIdentity.Name将是空字符串。

var myIdentity = new MyIdentity("MyId", otherstuff);
evaluationContext.Properties["Identities"] = new List<IIdentity> {myIdentity};

此外,您可能希望在替换任何令牌之前处理/读取它们。

var identities = evaluationContext.Properties.ContainsKey("Identities")
                                 ? (List<IIdentity>) evaluationContext.Properties["Identities"]
                                 : new List<IIdentity>();

var genericIdentity = identities.Find(x => x.AuthenticationType == "MyUserNamePasswordValidator");

genericIdentity.Name--> 包含来自 WSS 用户名令牌的用户名。

您需要一个用户名密码验证器(http://msdn.microsoft.com/en-us/library/system.identitymodel.selectors.usernamepasswordvalidator.aspx http://msdn.microsoft.com/en-us/library/system.identitymodel.selectors.usernamepasswordvalidator.aspx) 如果您使用 WS-Security 用户名令牌并且不希望任何默认的 WCF 验证。由于我们有一个 DataPower 设备,可以在消息到达我们的服务之前验证令牌,因此我们不需要验证用户名和密码。在我们的例子中它只是返回 true。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

填充 WCF 中的 PrimaryIdentity 的相关文章

随机推荐