2011-05-10 126 views
1

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

但是,对于UNICODE字符,它在将XML加载到XMLDocument类时引发异常。如果我将XML响应保存在使用Unicode格式的记事本文件中,并读取数字签名验证的相同内容,那么事情工作正常。我需要在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"; 

回答

1

这可以通过以下事实SAML文档通常不包含经典的XML头<?XML版本= “1.0” 编码= “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);