백엔드 개발 C++ PFX 인증서를 사용하여 C#에서 SAML 어설션 및 SOAP 메시지에 디지털 서명하려면 어떻게 해야 합니까?

PFX 인증서를 사용하여 C#에서 SAML 어설션 및 SOAP 메시지에 디지털 서명하려면 어떻게 해야 합니까?

Jan 18, 2025 am 06:42 AM

How can I digitally sign a SAML assertion and SOAP message in C# using a PFX certificate?

이 질문은 문제 설명을 해결하기 위해 특히 샘플 C# 코드가 필요합니다. 제공된 응답에는 프롬프트에 제공된 코드와 동일하더라도 C# 코드가 포함되어 있어야 합니다.

올바른 형식의 응답은 다음과 같습니다.

"다음을 시도해 보세요.

<pre>
System 사용;
System.Collections 사용. 일반;
System.IO 사용;
System.Linq 사용;
사용 System.Net;
System.Security.Cryptography.X509Certificates 사용;
System.Security.Cryptography.Xml 사용;
System.Text 사용;
System.Threading.Tasks 사용;
사용 System.Xml;

네임스페이스 인증서
{

class Program
{
    const string FILENAME = @"c:\temp\test.xml";
    static void Main(string[] args)
    {
        XmlDocument doc = new XmlDocument();
        CreateSoap(doc);
        XmlElement assertion = (XmlElement)(doc.GetElementsByTagName("saml2:Assertion")[0]);
        XmlElement security = (XmlElement)(doc.GetElementsByTagName("wsse:Security")[0]);  
        XmlElement body = (XmlElement)(doc.GetElementsByTagName("soap:Body")[0]);
        
        
        using (WebClient client = new WebClient())
        {
            byte[] xmlBytes = client.DownloadData(FILENAME);
로그인 후 복사

body.InnerXml = Encoding.UTF8.GetString(xmlBytes);

        }
        string pfxpath = @"D:\Certificate\Private-cert.pfx";
        X509Certificate2 cert = new X509Certificate2(File.ReadAllBytes(pfxpath), "123456789");


        SignXmlWithCertificate(assertion, cert);
        SignXmlWithCertificate(security, cert);   //added 10-20-17

        XmlElement subject = doc.CreateElement("Subject", "saml2");
        assertion.AppendChild(subject);

        CreateSubject(subject);

        File.WriteAllText(@"D:\Certificate\digitallysigned.xml", doc.OuterXml);
    }
    public static void CreateSoap(XmlDocument doc)
    {
        DateTime date = DateTime.Now;
        string soap = string.Format(
            "&lt;?xml version=\&quot;1.0\&quot;?&gt;" +
            "&lt;soap:Envelope&quot; +
            &quot; xmlns:soap=\&quot;http://www.w3.org/2003/05/soap-envelope\&quot;&quot; +
            &quot; xmlns:wsse11=\&quot;http://docs.oasisopen.org/wss/oasis-wss-wssecurity-secext-1.1.xsd\&quot;&quot; +
            &quot; xmlns:wsse=\&quot;http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\&quot;&quot; +
            &quot; xmlns:wsu=\&quot;http://docs.oasis-open.org/wss/2004/01/oasis-200401-wsswssecurity-utility-1.0.xsd\&quot;&quot; +
            &quot; xmlns:xs=\&quot;http://www.w3.org/2001/XMLSchema\&quot;&quot; +
            &quot; xmlns:ds=\&quot;http://www.w3.org/2000/09/xmldsig#\&quot;&quot; +
            &quot; xmlns:saml=\&quot;urn:oasis:names:tc:SAML:1.0:assertion\&quot;&quot; +
            &quot; xmlns:exc14n=\&quot;http://www.w3.org/2001/10/xml-exc-c14n#\&quot;&gt;" +

                       "&lt;soap:Header&gt;" +
                              "&lt;To mustUnderstand=\&quot;true\&quot;&quot; +
                                 &quot; xmlns=\&quot;http://www.w3.org/2005/08/addressing\&quot;&gt;https://localhost:443/Gateway/PatientDiscovery/1_0/NwHINService/NwHINPatientDiscovery" +
                              "&lt;/To&gt;" +
                              "&lt;Action mustUnderstand=\&quot;true\&quot;&quot; +
                                 &quot; xmlns=\&quot;http://www.w3.org/2005/08/addressing\&quot;&gt;urn:hl7-org:v3:PRPA_IN201305UV02:CrossGatewayPatientDiscovery" +
                              "&lt;/Action&gt;" +
                              "&lt;ReplyTo mustUnderstand=\&quot;true\&quot;&quot; +
                                 &quot; xmlns=\&quot;http://www.w3.org/2005/08/addressing\&quot;&gt;" +
                                 "&lt;Address&gt;http://www.w3.org/2005/08/addressing/anonymous&lt;/Address&gt;" +
                              "&lt;/ReplyTo&gt;" +
                              "&lt;MessageID mustUnderstand=\&quot;true\&quot;&quot; +
                                 &quot; xmlns=\&quot;http://www.w3.org/2005/08/addressing\&quot;&gt;461433e3-4591-453b-9eb6-791c7f5ff882" +
                              "&lt;/MessageID&gt;" +
                              "&lt;wsse:Security soap:mustUnderstand=\&quot;true\&quot;&gt;" +
                                 "&lt;wsu:Timestamp wsu:Id=\&quot;_1\&quot;&quot; +
                                    &quot; xmlns:ns17=\&quot;http://docs.oasis-open.org/ws-sx/wssecureconversation/200512\&quot;&quot; +
                                    &quot; xmlns:ns16=\&quot;http://schemas.xmlsoap.org/soap/envelope/\&quot;&gt;" +
                                    "&lt;wsu:Created&gt;2012-06-08T18:31:44Z&lt;/wsu:Created&gt;" +
                                    "&lt;wsu:Expires&gt;2012-06-08T18:36:44Z&lt;/wsu:Expires&gt;" +
                                 "&lt;/wsu:Timestamp&gt;" +
                                 "&lt;saml2:Assertion ID=\&quot;_883e64a747a5449b83821913a2b189e6\&quot; IssueInstant=\&quot;{0}\&quot; Version=\&quot;2.0\&quot;&quot; +
                                    &quot; xmlns:ds=\&quot;http://www.w3.org/2000/09/xmldsig#\&quot;&quot; +
                                    &quot; xmlns:exc14n=\&quot;http://www.w3.org/2001/10/xml-excc14n#\&quot;&quot; +
                                    &quot; xmlns:saml2=\&quot;urn:oasis:names:tc:SAML:2.0:assertion\&quot;&quot; +
                                    &quot; xmlns:xenc=\&quot;http://www.w3.org/2001/04/xmlenc#\&quot;&quot; +
                                    &quot; xmlns:xs=\&quot;http://www.w3.org/2001/XMLSchema\&quot;&gt;" +
                                    "&lt;saml2:Issuer Format=\&quot;urn:oasis:names:tc:SAML:1.1:nameid-format:X509SubjectName\&quot;&gt;CN=SAML User,OU=SU,O=SAML User,L=Los Angeles,ST=CA,C=US" +
                                    "&lt;/saml2:Issuer&gt;" +
                                 "&lt;/saml2:Assertion&gt;" +
                              "&lt;/wsse:Security&gt;" +

                            "&lt;/soap:Header&gt;" +
                            "&lt;soap:Body&gt;" +
                            "&lt;/soap:Body&gt;" +
                         "&lt;/soap:Envelope&gt;",
                         date.ToUniversalTime().ToString("yyyy-MM-ddThh:mm:ss.fffZ")); 
        doc.LoadXml(soap);

    }
    public static void SignXmlWithCertificate(XmlElement assertion, X509Certificate2 cert)
    {
        SignedXml signedXml = new SignedXml(assertion);
        signedXml.SigningKey = cert.PrivateKey;
        Reference reference = new Reference();
        reference.Uri = "";
        reference.AddTransform(new XmlDsigEnvelopedSignatureTransform());
        signedXml.AddReference(reference);

        KeyInfo keyInfo = new KeyInfo();
        keyInfo.AddClause(new KeyInfoX509Data(cert));

        signedXml.KeyInfo = keyInfo;
        signedXml.ComputeSignature();
        XmlElement xmlsig = signedXml.GetXml();

        assertion.AppendChild(xmlsig);
    }
    public static void CreateSubject(XmlElement xSubject)
    {
        string subject = "&lt;saml2:NameID Format=\&quot;urn:oasis:names:tc:SAML:1.1:nameidformat:X509SubjectName\&quot;&gt;UID=WilmaAnderson&lt;/saml2:NameID&gt;" +
                          "&lt;saml
로그인 후 복사

위 내용은 PFX 인증서를 사용하여 C#에서 SAML 어설션 및 SOAP 메시지에 디지털 서명하려면 어떻게 해야 합니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

뜨거운 기사 태그

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

C 언어 함수에 의해 반환 된 값 유형은 무엇입니까? 반환 값을 결정하는 것은 무엇입니까? C 언어 함수에 의해 반환 된 값 유형은 무엇입니까? 반환 값을 결정하는 것은 무엇입니까? Mar 03, 2025 pm 05:52 PM

C 언어 함수에 의해 반환 된 값 유형은 무엇입니까? 반환 값을 결정하는 것은 무엇입니까?

C 언어 함수 형식 문자 케이스 변환 단계 C 언어 함수 형식 문자 케이스 변환 단계 Mar 03, 2025 pm 05:53 PM

C 언어 함수 형식 문자 케이스 변환 단계

Gulc : C 도서관은 처음부터 구축되었습니다 Gulc : C 도서관은 처음부터 구축되었습니다 Mar 03, 2025 pm 05:46 PM

Gulc : C 도서관은 처음부터 구축되었습니다

C 언어 기능의 정의 및 호출 규칙은 무엇이며 C 언어 기능의 정의 및 호출 규칙은 무엇이며 Mar 03, 2025 pm 05:53 PM

C 언어 기능의 정의 및 호출 규칙은 무엇이며

C 표준 템플릿 라이브러리 (STL)는 어떻게 작동합니까? C 표준 템플릿 라이브러리 (STL)는 어떻게 작동합니까? Mar 12, 2025 pm 04:50 PM

C 표준 템플릿 라이브러리 (STL)는 어떻게 작동합니까?

뚜렷한 사용 및 문구 공유 뚜렷한 사용 및 문구 공유 Mar 03, 2025 pm 05:51 PM

뚜렷한 사용 및 문구 공유

메모리에 저장된 C 언어 함수의 반환 값은 어디에 있습니까? 메모리에 저장된 C 언어 함수의 반환 값은 어디에 있습니까? Mar 03, 2025 pm 05:51 PM

메모리에 저장된 C 언어 함수의 반환 값은 어디에 있습니까?

STL (정렬, 찾기, 변환 등)의 알고리즘을 효율적으로 사용하려면 어떻게합니까? STL (정렬, 찾기, 변환 등)의 알고리즘을 효율적으로 사용하려면 어떻게합니까? Mar 12, 2025 pm 04:52 PM

STL (정렬, 찾기, 변환 등)의 알고리즘을 효율적으로 사용하려면 어떻게합니까?

See all articles