简体   繁体   English

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

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

I have a web based application where SSO using SAML(as a service provider) is implemented. 我有一个基于Web的应用程序,其中实现了使用SAML(作为服务提供者)的SSO。 It involves Digital Signature verification. 它涉及数字签名验证。 Everything works fine with following code if there is no unicode character like 'ü' in the XML response being post to system. 如果在XML响应中没有像ü这样的unicode字符被发布到系统中,那么以下代码将一切正常。

But with UNICODE character it throws exception while loading xml into XMLDocument class. 但是使用UNICODE字符时,将xml加载到XMLDocument类时会引发异常。 If i save the XML response in a notepad file using Unicode format and read the same for digital signature verification, things are working fine. 如果我使用Unicode格式将XML响应保存在记事本文件中,并读取该响应以进行数字签名验证,则一切正常。 I need to have alternative of notepad manual steps in C# implementation. 我需要在C#实现中使用记事本手动步骤的替代方法。

Following is the code i am using. 以下是我正在使用的代码。

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";

This may be caused by the fact that SAML documents often don't contain the classic xml header <?xml version="1.0" encoding="utf-8"?> 这可能是由于SAML文档通常不包含经典xml标头<?xml version =“ 1.0” encoding =“ utf-8”?>

Did you try to force the encoding to UTF-8 ? 您是否尝试将编码强制为UTF-8?

You could try something similar to this example: 您可以尝试类似于以下示例的操作:

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