繁体   English   中英

SSO SAML数字签名验证-XML Unicode字符

[英]SSO SAML digital signature verification - XML unicode character

我有一个基于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";

这可能是由于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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM