Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#生成saml2响应can';t获取标签的响应部分_C#_Saml 2.0 - Fatal编程技术网

C#生成saml2响应can';t获取标签的响应部分

C#生成saml2响应can';t获取标签的响应部分,c#,saml-2.0,C#,Saml 2.0,因此,我尝试使用C#with.NET4.5System.IdentityModel.Tokens.Saml2Assertion类创建一个saml2响应 到目前为止,我已经创建了一个断言xml,但是我在这里看到的示例在开头有一些响应标记。我的第一个问题是,如何获得添加到我的断言中的响应?(我将在下面发布代码)。我的第二个问题是这个示例有如下标记 Saml2NameIdentifier=新的Saml2NameIdentifier(“myid.com”) Saml2Assertion assert=新

因此,我尝试使用C#with.NET4.5System.IdentityModel.Tokens.Saml2Assertion类创建一个saml2响应

到目前为止,我已经创建了一个断言xml,但是我在这里看到的示例在开头有一些响应标记。我的第一个问题是,如何获得添加到我的断言中的响应?(我将在下面发布代码)。我的第二个问题是这个示例有如下标记 Saml2NameIdentifier=新的Saml2NameIdentifier(“myid.com”)

Saml2Assertion assert=新的Saml2Assertion(标识符);
assert.Subject=newsaml2subject(标识符);
字符串名称空间=”http://id.certmetrics.com/";
Saml2AttributeStatement attrs=新的Saml2AttributeStatement();
DataRow row=qGetCanData.SqlExecDataTable(null,SelectedCandidate.CmcID)。行[0];
//json字符串中有一个键值对列表,为我们提供了属性名和列名。专栏
//name是使用eval string函数从数据行获得的值
foreach(cset.Attributes中的KeyValuePair项)
{
string val=EvalString(行、项、值、字符串、空);
如果(!string.IsNullOrEmpty(val))
{
Saml2Attribute attr=新的Saml2Attribute(item.Key);
属性值添加(val);
attrs.Attributes.Add(attr);
}
}
assert.Statements.Add(attrs);
StringBuilder sb=新的StringBuilder();
XmlWriterSettings=new XmlWriterSettings(){OmitXmlDeclaration=true,Encoding=Encoding.UTF8};
//使用此行将.p12文件转换为十六进制格式。然后将十六进制复制/粘贴到字符串中,以将其存储在源代码中。
//字符串sslCertBytesInHex=Utility.BytesToHex(File.ReadAllBytes(Server.MapPath(“~/app\u data/ssl.p12”));
字符串sslCertBytesInHex=“ABigLongHexHere”;
//这将加载证书。通过使用“MachineKeySet”,我们避免了在AppPool中使用“Load User Profile”=true。
X509Certificate2客户端证书=新的X509Certificate2();
clientCert.Import(Utility.HexToBytes(sslCertBytesInHex),“1234”,x509keystrageflags.MachineKeySet);
X509SigningCredentials creds=新的X509SigningCredentials(客户端证书);
assert.SigningCredentials=creds;
Saml2SecurityToken令牌=新的Saml2SecurityToken(断言);
StringWriter sw=新的StringWriter();
Saml2SecurityTokenHandler tokenHandler=新Saml2SecurityTokenHandler();
WriteToken(新的XmlTextWriter(sw),令牌);
//在这里,我不知道如何获得“response”标签,再加上将saml:添加到所有标签中
它生成的XML如下所示

<Assertion ID="_6d1bd525-b460-42a7-9def-fe28d26f5713" IssueInstant="2017-06-14T12:54:28.141Z" Version="2.0" xmlns="urn:oasis:names:tc:SAML:2.0:assertion">
    <Issuer>myid.com</Issuer>
    <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
        <SignedInfo>
            <CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
            <SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" />
            <Reference URI="#_6d1bd525-b460-42a7-9def-fe28d26f5713">
                <Transforms>
                    <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
                    <Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
                </Transforms>
                <DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
                <DigestValue>SomeString</DigestValue>
            </Reference>
        </SignedInfo>
        <SignatureValue>TheSigValue</SignatureValue>
        <KeyInfo>
            <X509Data>
                <X509Certificate>TheCertValue</X509Certificate>
            </X509Data>
        </KeyInfo>
    </Signature>
    <Subject>
        <NameID>myid.com</NameID>
    </Subject>
    <AttributeStatement>
        <Attribute Name="att1">
            <AttributeValue>att1</AttributeValue>
        </Attribute>
    </AttributeStatement>
</Assertion>

myid.com
一些字符串
奇异值
临界值
myid.com
附件1

对SAML2的.NET Framework支持仅适用于SAML2令牌-这是XML中的
。要生成
XML,您需要一些支持SAML2协议的东西。要么自己构建,要么使用现有库。我做的一个开源的是Kentor.AuthServices,但也有其他选择,开源和商业

<Assertion ID="_6d1bd525-b460-42a7-9def-fe28d26f5713" IssueInstant="2017-06-14T12:54:28.141Z" Version="2.0" xmlns="urn:oasis:names:tc:SAML:2.0:assertion">
    <Issuer>myid.com</Issuer>
    <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
        <SignedInfo>
            <CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
            <SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" />
            <Reference URI="#_6d1bd525-b460-42a7-9def-fe28d26f5713">
                <Transforms>
                    <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
                    <Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
                </Transforms>
                <DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
                <DigestValue>SomeString</DigestValue>
            </Reference>
        </SignedInfo>
        <SignatureValue>TheSigValue</SignatureValue>
        <KeyInfo>
            <X509Data>
                <X509Certificate>TheCertValue</X509Certificate>
            </X509Data>
        </KeyInfo>
    </Signature>
    <Subject>
        <NameID>myid.com</NameID>
    </Subject>
    <AttributeStatement>
        <Attribute Name="att1">
            <AttributeValue>att1</AttributeValue>
        </Attribute>
    </AttributeStatement>
</Assertion>