C# 客户端请求中SAML令牌后需要签名

C# 客户端请求中SAML令牌后需要签名,c#,wcf,interop,saml,self-signed,C#,Wcf,Interop,Saml,Self Signed,我有一个序列化的SOAP请求消息,其中包含一个SAML令牌持有者密钥,该密钥可用于供应商服务。我想用C#创建一个演示程序来生成类似的请求。为此,我想编写一个创建自己的SAML令牌的客户机 我已从自签名证书成功创建SAML2令牌,并且我能够使用ChannelFactoryOperations.CreateChannelWithIssuedToken方法(.Net 4.0)将其与请求关联。一切都很顺利,但我无法找出将签名放在断言之后并使用SAML令牌作为签名密钥标识符来签名时间戳所需的C。我甚至不知

我有一个序列化的SOAP请求消息,其中包含一个SAML令牌持有者密钥,该密钥可用于供应商服务。我想用C#创建一个演示程序来生成类似的请求。为此,我想编写一个创建自己的SAML令牌的客户机

我已从自签名证书成功创建SAML2令牌,并且我能够使用ChannelFactoryOperations.CreateChannelWithIssuedToken方法(.Net 4.0)将其与请求关联。一切都很顺利,但我无法找出将签名放在断言之后并使用SAML令牌作为签名密钥标识符来签名时间戳所需的C。我甚至不知道我在问什么,但似乎令牌后面的签名本身应该是最简单的部分。但是,我让SAML出现在请求中的唯一方法是将其声明为BearerKey类型。但是BearerKey似乎省略了断言后的签名。似乎我想要SymmetricKey,但是令牌“没有密钥”。如何使这样的签名元素出现在断言之后


这里URI=“#_1”指的是上面的WS-Security时间戳(未显示)。

嗨,伙计们,我真不敢相信我终于解决了所有这些问题。这段代码加载一个自签名证书,生成一个SAML令牌,然后用SAML令牌认可消息。我遇到的问题是“令牌没有密钥”错误。通过创建一个issuerToken和一个密钥并将其传递给令牌构造函数,可以解决这个问题。见下文。我认为我在网上找到的最有用的信息就是这篇很棒的帖子

X509Certificate2 cert=new X509Certificate2(“C:\\Users\\foobar\\desktop\\test.pfx”,“test”,X509keystrageFlags.MachineKeySet);
RSACryptoServiceProvider rsa=cert.PrivateKey作为RSACryptoServiceProvider;
RsaSecurityKey rsaKey=新的RsaSecurityKey(rsa);
RsaKeyIdentifierClause rsaClause=新的RsaKeyIdentifierClause(rsa);
SecurityKeyIdentifier signingSki=新的SecurityKeyIdentifier(新的SecurityKeyIdentifierClause[]{rsaClause});
SigningCredentials SigningCredentials=新的签名凭证(rsaKey,SecurityAlgorithms.rsasha1签名,SecurityAlgorithms.Sha1Digest,signingSki);
Saml2NameIdentifier Saml2NameIdentifier=新Saml2NameIdentifier(“C=US,O=hi-mom,CN=test”,new-System.Uri(“urn:oasis:names:tc:SAML:1.1:nameid格式:X509SubjectName”);
Saml2Assertion saml2Assertion2=新的Saml2Assertion(saml2NameIdentifier);
saml2Assertion2.SigningCredentials=签名凭证;
Saml2Subject Saml2Subject=新的Saml2Subject();
saml2NameIdentifier=新的saml2NameIdentifier(“foo@bar.edu,new System.Uri(“urn:oasis:names:tc:SAML:1.1:nameid格式:X509SubjectName”);
saml2Subject.NameId=saml2NameIdentifier;
Saml2SubjectConfirmationData subjectConfirmationData=新Saml2SubjectConfirmationData();
Saml2SubjectConfirmation subjectConfirmation=newsaml2subjectconfirmation(新Uri(“urn:oasis:names:tc:SAML:2.0:cm:holder of key”);
subjectConfirmation.SubjectConfirmationData=SubjectConfirmationData;
subjectConfirmationData.KeyIdentifiers.Add(signingSki);
saml2Subject.SubjectConfirmations.Add(subjectConfirmation);
saml2Assertion2.Subject=saml2Subject;
Saml2AuthenticationContext saml2AuthCtxt=newsaml2authenticationcontext(新Uri(“urn:oasis:names:tc:SAML:2.0:ac:classes:X509”);
Saml2AuthenticationStatement Saml2AuthenticationStatement=新的Saml2AuthenticationStatement(saml2AuthCtxt);
saml2AuthStatement.SessionIndex=“123456”;
saml2Assertion2.Statements.Add(saml2AuthStatement);
Saml2AttributeStatement saml2AttStatement=new Saml2AttributeStatement();
Saml2Attribute Saml2Attribute=newsaml2attribute(“urn:oasis:names:tc:xspa:1.0:subject:subject:subject-id”,“foo-bar测试”);
saml2AttStatement.Attributes.Add(saml2Attribute);
saml2Attribute=newsaml2attribute(“urn:oasis:names:tc:xspa:1.0:subject:organization”,“urn:oid:+senderOid”);
saml2AttStatement.Attributes.Add(saml2Attribute);
saml2Attribute=newsaml2attribute(“urn:oasis:names:tc:xspa:1.0:subject:organization id”,“urn:oid:+senderOid”);
saml2AttStatement.Attributes.Add(saml2Attribute);
saml2Attribute=newsaml2attribute(“urn:nhin:names:saml:homeCommunityId”,“urn:oid:+senderOid”);
saml2AttStatement.Attributes.Add(saml2Attribute);
saml2Attribute=新的saml2Attribute(“urn:oasis:names:tc:xacml:2.0:subject:role”);
saml2AttStatement.Attributes.Add(saml2Attribute);
saml2Assertion2.Statements.Add(saml2AttStatement);
List keyList=新列表();
keyList.Add(rsaKey);
ReadOnlyCollection键=新的ReadOnlyCollection(键列表);
X509SecurityToken issuerToken=新的X509SecurityToken(证书);
Saml2SecurityToken token2=新的Saml2SecurityToken(saml2Assertion2,key,issuerToken);
XcpdRespondingGatewaySyncService.RespondingGatewaySyncClient myClient=新的XcpdRespondingGatewaySyncService.RespondingGatewaySyncClient(“IRespondingGatewaySync2”);
CustomBinding CustomBinding=myClient.Endpoint.Binding作为CustomBinding;
SecurityBindingElement=customBinding.Elements.Find();
IssuedSecurityTokenParameters tokenParameters=element.EndpointSupportingTokenParameters.Signed[0]。克隆()作为IssuedSecurityTokenParameters;
tokenParameters.TokenType=System.IdentityModel.Tokens.SecurityTokenTypes.Saml;
tokenParameters.RequiredDriveKeys=false;
tokenParameters.KeyType=SecurityKeyType.SymmetricKey;
element.EndpointSupportingTokenParameters.Signed.Clear();
元素.EndpointSupportingTokenParameters.Endorsing.Add(tokenParameters);
myClient.ChannelFactory.Credentials.SupportInteractive=false;
myClient.ChannelFactory.ConfigureChannelFactory();
xCPD响应
        X509Certificate2 cert = new X509Certificate2("C:\\Users\\foobar\\desktop\\test.pfx", "test", X509KeyStorageFlags.MachineKeySet);
        RSACryptoServiceProvider rsa = cert.PrivateKey as RSACryptoServiceProvider;
        RsaSecurityKey rsaKey = new RsaSecurityKey(rsa);
        RsaKeyIdentifierClause rsaClause = new RsaKeyIdentifierClause(rsa);
        SecurityKeyIdentifier signingSki = new SecurityKeyIdentifier(new SecurityKeyIdentifierClause[] { rsaClause });
        SigningCredentials signingCredentials = new SigningCredentials(rsaKey, SecurityAlgorithms.RsaSha1Signature, SecurityAlgorithms.Sha1Digest, signingSki);
        Saml2NameIdentifier saml2NameIdentifier = new Saml2NameIdentifier("C=US,O=hi mom,CN=test", new System.Uri("urn:oasis:names:tc:SAML:1.1:nameid-format:X509SubjectName"));
        Saml2Assertion saml2Assertion2 = new Saml2Assertion(saml2NameIdentifier);
        saml2Assertion2.SigningCredentials = signingCredentials;
        Saml2Subject saml2Subject = new Saml2Subject();
        saml2NameIdentifier = new Saml2NameIdentifier("foo@bar.edu", new System.Uri("urn:oasis:names:tc:SAML:1.1:nameid-format:X509SubjectName"));
        saml2Subject.NameId = saml2NameIdentifier;
        Saml2SubjectConfirmationData subjectConfirmationData = new Saml2SubjectConfirmationData();
        Saml2SubjectConfirmation subjectConfirmation = new Saml2SubjectConfirmation(new Uri("urn:oasis:names:tc:SAML:2.0:cm:holder-of-key"));
        subjectConfirmation.SubjectConfirmationData = subjectConfirmationData;
        subjectConfirmationData.KeyIdentifiers.Add(signingSki);
        saml2Subject.SubjectConfirmations.Add(subjectConfirmation);
        saml2Assertion2.Subject = saml2Subject;
        Saml2AuthenticationContext saml2AuthCtxt = new Saml2AuthenticationContext(new Uri("urn:oasis:names:tc:SAML:2.0:ac:classes:X509"));
        Saml2AuthenticationStatement saml2AuthStatement = new Saml2AuthenticationStatement(saml2AuthCtxt);
        saml2AuthStatement.SessionIndex = "123456";
        saml2Assertion2.Statements.Add(saml2AuthStatement);
        Saml2AttributeStatement saml2AttStatement = new Saml2AttributeStatement();
        Saml2Attribute saml2Attribute = new Saml2Attribute("urn:oasis:names:tc:xspa:1.0:subject:subject-id", "foo bar test");
        saml2AttStatement.Attributes.Add(saml2Attribute);
        saml2Attribute = new Saml2Attribute("urn:oasis:names:tc:xspa:1.0:subject:organization", "urn:oid:"+senderOid);
        saml2AttStatement.Attributes.Add(saml2Attribute);
        saml2Attribute = new Saml2Attribute("urn:oasis:names:tc:xspa:1.0:subject:organization-id", "urn:oid:" + senderOid);
        saml2AttStatement.Attributes.Add(saml2Attribute);
        saml2Attribute = new Saml2Attribute("urn:nhin:names:saml:homeCommunityId", "urn:oid:" + senderOid);
        saml2AttStatement.Attributes.Add(saml2Attribute);
        saml2Attribute = new Saml2Attribute("urn:oasis:names:tc:xacml:2.0:subject:role");
        saml2AttStatement.Attributes.Add(saml2Attribute);
        saml2Assertion2.Statements.Add(saml2AttStatement);
        List<SecurityKey> keyList = new List<SecurityKey>();
        keyList.Add(rsaKey);
        ReadOnlyCollection<SecurityKey> keys = new ReadOnlyCollection<SecurityKey>(keyList);
        X509SecurityToken issuerToken = new X509SecurityToken(cert);
        Saml2SecurityToken token2 = new Saml2SecurityToken(saml2Assertion2,keys,issuerToken);
        XcpdRespondingGatewaySyncService.RespondingGatewaySyncClient myClient = new XcpdRespondingGatewaySyncService.RespondingGatewaySyncClient("IRespondingGatewaySync2");   
        CustomBinding customBinding = myClient.Endpoint.Binding as CustomBinding;
        SecurityBindingElement element = customBinding.Elements.Find<SecurityBindingElement>();
        IssuedSecurityTokenParameters tokenParameters = element.EndpointSupportingTokenParameters.Signed[0].Clone() as IssuedSecurityTokenParameters;
        tokenParameters.TokenType = System.IdentityModel.Tokens.SecurityTokenTypes.Saml;
        tokenParameters.RequireDerivedKeys = false;
        tokenParameters.KeyType = SecurityKeyType.SymmetricKey;
        element.EndpointSupportingTokenParameters.Signed.Clear();
        element.EndpointSupportingTokenParameters.Endorsing.Add(tokenParameters);
        myClient.ChannelFactory.Credentials.SupportInteractive = false;
        myClient.ChannelFactory.ConfigureChannelFactory();
        XcpdRespondingGatewaySyncService.IRespondingGatewaySync myChannel = ChannelFactoryOperations.CreateChannelWithIssuedToken(myClient.ChannelFactory, token2);