SSO SAML 数字签名验证 - XML 统一码字符

本文关键字:一码 字符 数字 SAML 签名验证 XML SSO | 更新日期: 2023-09-27 17:56:15

我有一个基于Web的应用程序,其中实现了使用SAML(作为服务提供商)的SSO。它涉及数字签名验证。如果发布到系统的 XML 响应中没有像"ü"这样的 unicode 字符,则以下代码一切正常。

但是使用UNICODE字符,它在将xml加载到XMLDocument类时会引发异常。如果我使用 Unicode 格式将 XML 响应保存在记事本文件中并读取相同的内容以进行数字签名验证,则一切正常。我需要在 C# 实现中替代记事本手动步骤。

以下是我正在使用的代码。

if (m_b64SamlResponse == null || m_b64SamlResponse == string.Empty)
return "SAMLResponse null or empty";
string xml = Decode(m_b64SamlResponse);
m_xmlDoc = new XmlDocument();
m_xmlDoc.PreserveWhitespace = true;
m_xmlDoc.LoadXml(xml);
XmlNamespaceManager nsm = new XmlNamespaceManager(new NameTable());
nsm.AddNamespace("dsig", SignedXml.XmlDsigNamespaceUrl);
XmlElement sigElt = (XmlElement)xd.SelectSingleNode(
"//dsig:Signature", nsm);
// Load the signature for verification
SignedXml sig = new SignedXml(m_xmlDoc);
sig.LoadXml(sigElt);
if (!sig.CheckSignature())
return "Invalid Signature";
else
return "Valid Signature";

SSO SAML 数字签名验证 - XML 统一码字符

这可能是

由于SAML文档通常不包含经典的xml标头<?xml version="1.0" encoding="utf-8"?>

您是否尝试强制编码为 UTF-8?

您可以尝试类似于此示例的操作:

string xml = Decode(m_b64SamlResponse);
byte[] xmlUTF8 = Encoding.UTF8.GetBytes(xml);
MemoryStream ms = new MemoryStream(encodedString);
ms.Flush();
ms.Position = 0;
m_xmlDoc = new XmlDocument();
m_xmlDoc.PreserveWhitespace = true;
m_xmlDoc.Load(ms);