使用.NET/WIF/WCF查询WS-Trust 1.4 STS

使用.NET/WIF/WCF查询WS-Trust 1.4 STS,.net,wcf,wif,saml-2.0,.net,Wcf,Wif,Saml 2.0,我需要使用.NET查询WS-Trust 1.4服务以启用SAML2.0身份验证场景 编辑:更准确地说,我需要在客户端支持WS-Trust 1.4中定义的用户交互挑战 我研究了WIF,它通过WSTrustChannelFactory提供对WS-Trust的直接访问(请参阅代码片段中的trustChannelFactory.TrustVersion…),但似乎只支持WS-Trust 1.3和2005年2月 WSTrustChannelFactory trustChannelF

我需要使用.NET查询WS-Trust 1.4服务以启用SAML2.0身份验证场景

编辑:更准确地说,我需要在客户端支持WS-Trust 1.4中定义的用户交互挑战

我研究了WIF,它通过WSTrustChannelFactory提供对WS-Trust的直接访问(请参阅代码片段中的trustChannelFactory.TrustVersion…),但似乎只支持WS-Trust 1.3和2005年2月

            WSTrustChannelFactory trustChannelFactory = new WSTrustChannelFactory(getBinding(), "http:/localhost...");

            trustChannelFactory.TrustVersion = TrustVersion.WSTrust13;
            WSTrustChannel channel = (WSTrustChannel)trustChannelFactory.CreateChannel();

            RequestSecurityToken rst = new RequestSecurityToken();

            RequestSecurityTokenResponse rstr = null;
            SecurityToken token = channel.Issue(rst, out rstr);
有人知道如何使用.NET实现这种直接WS-Trust查询吗

我不能使用WSHttpFederation绑定,因为我们需要使用SAML2.0,并且必须在将SAML2.0身份验证请求传递给IdP之前从应用服务器检索SAML2.0身份验证请求


我当然可以推出自己的客户端WS-Trust 1.4。实现,但可能有一种更简单的方法…

我使用.NET扩展方法扩展了WIF WS-Trust实现。在这里,您可以看到第一部分(Issue Request with RST和SAML Authn Request)作为如何重用WIF中已经定义的内容的示例。我使用了一个IL反汇编程序来查看WIF中的事情是如何完成的,这在途中非常有用

internal static RequestSecurityTokenResponseWithSAML2Assertion Issue(this WSTrustChannel pThis,
        string pSAML2AuthnRequest,
        Func<ProfileSelectionChallengeType, wsTrust14.ChoiceSelectedType> pProfileSelectionCallback)
    {
        if (pThis != null)
        {
            if (pThis.ChannelFactory != null &&
                pThis.ChannelFactory.Endpoint != null &&
                pThis.ChannelFactory.Endpoint.Binding != null)
            {
                // Create RST Request
                RequestSecurityToken rst = new RequestSecurityToken("http://docs.oasis-open.org/ws-sx/ws-trust/200512/Issue");
                rst.TokenType = "http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV2.0";

                // we use WS Trust 1.4 but .NET WIF only provides support for WS Trust 1.3
                // so we add the needed Challenge support and reuse most of the WIF stuff
                if (pThis.TrustVersion != System.ServiceModel.Security.TrustVersion.WSTrust13)
                {
                    throw new Exception("Given WS Trust Version not supported!");
                }

                // create a WS Trust 1.3 SOAP Message
                Message issueRequest = Message.CreateMessage(pThis.ChannelFactory.Endpoint.Binding.MessageVersion,
                    "http://docs.oasis-open.org/ws-sx/ws-trust/200512/RST/Issue",
                    new WSTrustRequestBodyWriter(rst,
                        pThis.WSTrustRequestSerializer,
                        pThis.WSTrustSerializationContext));

                // add SAML Authn Request to the WS Trust request
                XmlDocument messageAsXml = issueRequest.serializeToXml();
                messageAsXml = SAMLSupport.addSAMLAuthenticationRequest(messageAsXml, pSAML2AuthnRequest);
                issueRequest = issueRequest.generateFromXml(messageAsXml);

                // invoke the WS Trust service on the STS
                Message responseMessage = pThis.Issue(issueRequest);

                // check what we received as answer...
                var response = pThis.parseAndHandleResponse(responseMessage, pProfileSelectionCallback);
                return response;
            }
        }
internal static RequestSecurityTokenResponseWithSAML2Assertion问题(此WSTrustChannel pThis,
字符串pSAML2AuthnRequest,
Func pProfileSelectionCallback)
{
if(pThis!=null)
{
if(pThis.ChannelFactory!=null&&
pThis.ChannelFactory.Endpoint!=null&&
pThis.ChannelFactory.Endpoint.Binding!=null)
{
//创建RST请求
RequestSecurityToken rst=新的RequestSecurityToken(“http://docs.oasis-open.org/ws-sx/ws-trust/200512/Issue");
rst.TokenType=”http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV2.0";
//我们使用WS-Trust 1.4,但.NET WIF仅提供对WS-Trust 1.3的支持
//因此,我们添加了所需的挑战支持,并重用了大部分WIF内容
if(pThis.TrustVersion!=System.ServiceModel.Security.TrustVersion.WSTrust13)
{
抛出新异常(“不支持给定的WS-Trust版本!”);
}
//创建WS-Trust 1.3 SOAP消息
Message issueRequest=Message.CreateMessage(pThis.ChannelFactory.Endpoint.Binding.MessageVersion,
"http://docs.oasis-open.org/ws-sx/ws-trust/200512/RST/Issue",
新WSTrustRequestBodyWriter(rst,
pThis.WSTrustRequestSerializer,
pThis.WSTrustSerializationContext);
//将SAML Authn请求添加到WS-Trust请求
XmlDocument messageAsXml=issueRequest.serializeToXml();
messageAsXml=SAMLSupport.addSAMLAuthenticationRequest(messageAsXml,pSAML2AuthnRequest);
issueRequest=issueRequest.generateFromXml(messageAsXml);
//在STS上调用WS-Trust服务
Message responseMessage=p此问题(issueRequest);
//检查我们收到的答案。。。
var response=pThis.parseAndHandleResponse(responseMessage,pprofileselection回调);
返回响应;
}
}

您是否在未设置
TrustVersion
的情况下尝试了代码段?可能是WSTrust命名空间使用WS-Trust 1.4作为默认值,您只需要将
TrustVersion
设置为1.3或2005。不幸的是,我在MSDN中找不到这一点,但我确实看到命名空间中有WS-Trust 1.4.I的常量至少值得一试。我只是查看了WIF实现的内部。WS-Trust 1.4中定义的交互式用户质询扩展没有在WIF中实现。我发现了一个帖子()有人在WS-Trust 1.3的另一个质询扩展中遇到了同样的问题。因此,问题根本不是版本设置。我必须扩展WIF以支持质询扩展…太糟糕了。似乎一旦InfoCard(或其他名称)出现东西内爆了,微软开始加强/支持基于标准的安全功能。我知道WCF fame的Michelle Bustamante后来成为了安全专家,也许她写过关于扩展WIF的文章。:)