解析SAML文件

        string msg = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope//">";
        msg += "<SOAP-ENV:Body>";
        msg += "  <samlp:ArtifactResponse xmlns:samlp=\"urn:oasis:names:tc:SAML:2.0:protocol\"";
        msg += "   xmlns:saml=\"urn:oasis:names:tc:SAML:2.0:assertion\" ID=\"8292855779514728a303b68636649b90\"";
        msg += "   IssueInstant=\"2010-07-13 02:42:58\" InResponseTo=\"faa4fee5be6b4726ad559c450171fdca\"";
        msg += "   Version=\"2.0\">";
        msg += "   <samlp:Status>";
        msg += "    <samlp:StatusCode Value=\"urn:oasis:names:tc:SAML:2.0:status:Success\" />";
        msg += "   </samlp:Status>";
        msg += "   <samlp:Response xmlns:saml=\"urn:oasis:names:tc:SAML:2.0:assertion\"";
        msg += "     xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance/" ID=\"fc8ef5d6487149c4a9195f6a55628dba\"";
        msg += "     IssueInstant=\"2010-07-13 02:44:34\" InResponseTo=\"0f37aa7a104a41ab8804f69f414bd06a\"";
        msg += "     Version=\"2.0\">";
        msg += "     <saml:Issuer>https://ac.chinamobile.com/</saml:Issuer>";
        msg += "     <samlp:Status>";
        msg += "      <samlp:StatusCode Value=\"urn:oasis:names:tc:SAML:2.0:status:Success\" />";
        msg += "     </samlp:Status>";
        msg += "     <saml:Assertion ID=\"8b208dd8ee8b4570b44ae201e0f1a75f\"";
        msg += "      Version=\"2.0\" IssueInstant=\"2010-07-13 02:44:34\">";
        msg += "      <saml:Issuer>https://ac.chinamobile.com/</saml:Issuer>";
        msg += "      <saml:Subject>";
        msg += "       <saml:NameID Format=\"urn:oasis:names:tc:SAML:2.0:nameid-format:transient\">13612345678</saml:NameID>";
        msg += "      </saml:Subject>";
        msg += "      <saml:Conditions NotBefore=\"2010-07-13 02:44:34\"";
        msg += "       NotOnOrAfter=\"2010-07-13 04:14:34\" />";
        msg += "       <saml:AttributeStatement>";
        msg += "        <saml:Attribute";
        msg += "        NameFormat=\"urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified\"";
        msg += "        Name=\"userinfo\">";
        msg += "           <saml:AttributeValue xsi:type=\"xs:string\">MSISDN</saml:AttributeValue>";
        msg += "             <saml:AttributeValue xsi:type=\"xs:string\">Province</saml:AttributeValue>";
        msg += "             <saml:AttributeValue xsi:type=\"xs:string\">name</saml:AttributeValue>";
        msg += "             <saml:AttributeValue xsi:type=\"xs:string\">brand</saml:AttributeValue>";
        msg += "             <saml:AttributeValue xsi:type=\"xs:string\">status</saml:AttributeValue>";
        msg += "             <saml:AttributeValue xsi:type=\"xs:string\"> Date </saml:AttributeValue>";
        msg += "             <saml:AttributeValue xsi:type=\"xs:string\">NickName</saml:AttributeValue>";
        msg += "             <saml:AttributeValue xsi:type=\"xs:string\">139MailStatus</saml:AttributeValue>";
        msg += "           <saml:AttributeValue xsi:type=\"xs:string\">FetionStatus</saml:AttributeValue>";
        msg += "        </saml:Attribute>";
        msg += "        </saml:AttributeStatement>";
        msg += "       </saml:Assertion>";
        msg += "      </samlp:Response>";
        msg += "  </samlp:ArtifactResponse>";
        msg += " </SOAP-ENV:Body>";
        msg += "</SOAP-ENV:Envelope>";

 

************************

 

public void ParseSAML(string strSAML)
    {
        XmlDocument xDoc = new XmlDocument();
        xDoc.LoadXml(strSAML);

        XmlNamespaceManager xMan = new XmlNamespaceManager(xDoc.NameTable);
        xMan.AddNamespace("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/");
        xMan.AddNamespace("samlp", "urn:oasis:names:tc:SAML:2.0:protocol");
        xMan.AddNamespace("saml", "urn:oasis:names:tc:SAML:2.0:assertion");

        XmlNode xNode = null;

        xNode = xDoc.SelectSingleNode("SOAP-ENV:Envelope/SOAP-ENV:Body/samlp:ArtifactResponse/samlp:Response/samlp:Status/samlp:StatusCode/@Value", xMan);
        if (xNode != null)
        {
            string statusCode = xNode.Value;
            Response.Write("statusCode=" + statusCode + "<br />");
            Regex reg = new Regex(@"urn:oasis:names:tc:SAML:2.0:status:\w+");
            statusCode = reg.Match(statusCode).Value.Replace("urn:oasis:names:tc:SAML:2.0:status:", "");
            Response.Write("statusCode=" + statusCode + "<br />");
        }

        xNode = xDoc.SelectSingleNode("SOAP-ENV:Envelope/SOAP-ENV:Body/samlp:ArtifactResponse/samlp:Response/saml:Assertion/saml:Subject/saml:NameID", xMan);
        if (xNode != null)
        {
            string NameID = xNode.InnerText;
            Response.Write("NameID=" + NameID + "<br />");
        }

        XmlNodeList nl = xDoc.SelectSingleNode("SOAP-ENV:Envelope/SOAP-ENV:Body/samlp:ArtifactResponse/samlp:Response/saml:Assertion/saml:AttributeStatement/saml:Attribute[@Name = 'userinfo']", xMan).ChildNodes;
        foreach (XmlNode xn in nl)
        {
            if (xn != null)
            {
                string firstName = xn.InnerText;
                Response.Write("firstName=" + firstName + "<br />");
            }
        }
    }

 

********************************
posted @   94cool  阅读(970)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
< 2010年9月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 1 2
3 4 5 6 7 8 9
点击右上角即可分享
微信分享提示