如何通过用户凭证访问AD FS声明?

我正在开发一个WCF Web服务,以在用户的​​登录操作与其活动目录角色和权限之间建立中介。 我不希望我的主机应用程序直接与AD FS通信。 我希望任何主机应用程序都使用我的Web服务,它将根据给定的凭据提供必要的信息。

在我的Web方法中,我需要通过用户的登录凭据从AD FS(WIF)获取声明。

我的Web方法将有两个输入参数,Window用户的电子邮件ID / Windows帐户名和密码。

因此,我希望通过给定用户的凭证在我的Web方法中访问AD FS声明。

如何通过给定用户的凭证获得AD FS声明?

您可以从ADFS请求DisplayTokem并使用它,它与令牌中的信息基本相同。

public DisplayClaimCollection GetDisplayClaims(string username, string password) { WSTrustChannelFactory factory = null; try { // use a UserName Trust Binding for username authentication factory = new WSTrustChannelFactory( new UserNameWSTrustBinding(SecurityMode.TransportWithMessageCredential), "https://.../adfs/services/trust/13/usernamemixed"); factory.TrustVersion = TrustVersion.WSTrust13; factory.Credentials.UserName.UserName = username; factory.Credentials.UserName.Password = password; var rst = new RequestSecurityToken { RequestType = RequestTypes.Issue, AppliesTo = "Relying party endpoint address", KeyType = KeyTypes.Symmetric, RequestDisplayToken = true }; IWSTrustChannelContract channel = factory.CreateChannel(); RequestSecurityTokenResponse rstr; SecurityToken token = channel.Issue(rst, out rstr); return rstr.RequestedDisplayToken.DisplayClaims; } finally { if (factory != null) { try { factory.Close(); } catch (CommunicationObjectFaultedException) { factory.Abort(); } } } } 

但这不是正确的做法! 您应该使用您的RelyingParty证书来解密加密的令牌并从中读取声明。

您应该对使用集成Windows身份validation的AD FS 2.0的https://…/adfs/services/trust/13/usernamemixed端点执行Web服务调用,提供用户的凭据以便可以设置连接。 在此端点上,调用http://docs.oasis-open.org/ws-sx/ws-trust/200512/RST/Issue操作。 (更多细节在WS-Trust 1.3规范的4.1节中 。)此操作的输入是RequestSecurityToken请求。 响应包含一个包含所需声明的SAML令牌。

请注意,AD FS 2.0 WSDL可以在https://…/adfs/services/trust/mex上找到:如果您将Visual Studio 添加服务引用向导或Java wsimport指向该URL,那么您将很容易生成可用于执行RST问题操作的客户端代码。