Sign In
.NET Security Blog
Translate This Page
Translate this page
Powered by
Microsoft® Translator
Options
Blog Home
Share this
RSS for posts
Atom
RSS for comments
Search
Advanced search options...
Search In:
Everything
Blogs
Forums
People
Groups
Places
Pages
Date range:
All Time
Last Year
Last 6 Months
Last 3 Months
Last Month
Last Week
Last Two Days
Tags
CAS
ClickOnce
CLR v4
CNG
Cryptography
Debugging
Orcas
Other
Pages
Policy
SecAnnotate
Security
Silverlight
SSCLI
StrongName
Transparency
Under the Hood
Visual Studio
Windows
XML
Archive
Archives
April 2010
(1)
February 2010
(1)
November 2009
(7)
June 2009
(4)
May 2009
(6)
March 2009
(1)
December 2008
(2)
August 2008
(1)
July 2008
(2)
May 2008
(2)
March 2008
(2)
February 2008
(1)
January 2008
(1)
October 2007
(2)
June 2007
(1)
May 2007
(5)
April 2007
(1)
March 2007
(5)
February 2007
(3)
January 2007
(5)
December 2006
(2)
November 2006
(3)
October 2006
(5)
September 2006
(2)
August 2006
(1)
July 2006
(6)
June 2006
(6)
May 2006
(7)
April 2006
(7)
March 2006
(6)
February 2006
(7)
January 2006
(9)
December 2005
(7)
November 2005
(8)
October 2005
(8)
September 2005
(13)
August 2005
(7)
July 2005
(8)
June 2005
(4)
May 2005
(10)
April 2005
(6)
March 2005
(10)
February 2005
(9)
January 2005
(10)
December 2004
(27)
November 2004
(12)
October 2004
(12)
September 2004
(10)
August 2004
(10)
July 2004
(10)
June 2004
(11)
May 2004
(7)
April 2004
(15)
March 2004
(21)
February 2004
(12)
January 2004
(3)
December 2003
(1)
November 2003
(5)
October 2003
(1)
June 2003
(2)
XmlIdSignedXml.cs
MSDN Blogs
>
.NET Security Blog
>
XmlIdSignedXml.cs
XmlIdSignedXml.cs
shawnfa
27 Apr 2004 2:25 PM
Comments
2
using
System;
using
System
.
Security
.
Cryptography
.
Xml;
using
System
.
Xml;
/// <summary>
/// Provides xml:id support for XML digital signatures
/// </summary>
/// <remarks>
/// This class allows the .NET XML Digital Signature system
/// uniquely identify nodes based upon xml:id's. The xml:id
/// working draft can be found on the W3C's website:
/// http://www.w3.org/TR/2004/WD-xml-id-20040407/
/// </remarks>
public
sealed
class
XmlIdSignedXml
:
SignedXml
{
/// <summary>
/// Namespace URI to map to the xml prefix
/// </summary>
public
static
readonly
string
XmlIdUrl
=
"http://www.w3.org/XML/1998/namespace"
;
private
bool
m_strict;
// operate in strict mode?
/// <summary>
/// Create a signed XML class that can sign and verify signatures
/// using xml:id's
/// </summary>
/// <see cref='System.Security.Cryptography.Xml.SignedXml'/>
public
XmlIdSignedXml
(
)
:
base
(
)
{
Strict
=
false
;
return
;
}
/// <summary>
/// Create a signed XML class that can sign and verify signatures
/// using xml:id's, using a specific document context
/// </summary>
/// <see cref='System.Security.Cryptography.Xml.SignedXml'/>
public
XmlIdSignedXml
(
XmlDocument document
)
:
base
(
document
)
{
Strict
=
false
;
return
;
}
/// <summary>
/// Create a signed XML class that can sign and verify signatures
/// using xml:id's, initialized with an XML element
/// </summary>
/// <see cref='System.Security.Cryptography.Xml.SignedXml'/>
public
XmlIdSignedXml
(
XmlElement element
)
:
base
(
element
)
{
Strict
=
false
;
return
;
}
/// <summary>
/// Flag to indicate if xml:id's should be matched exclusively
/// or if fallback on default behavior
/// </summary>
public
bool
Strict
{
get {
return
m_strict; }
set { m_strict
=
value; }
}
/// <summary>
/// Return the XmlElement with the given id from the given document
/// </summary>
/// <remarks>
/// First attempts to match to an xml:id, and if that fails will only
/// fall back on the default behavior if the Strict flag is unset.
/// </remarks>
/// <param name="document">document to search for matching nodes in</param>
/// <param name="idValue">id of the node to find</param>
/// <exception cref="System.ArgumentNullException"><paramref name="idValue"/> is null</exception>
/// <exception cref="System.ArgumentException"><paramref name="idValue"/> contains both single and double quotes</exception>
/// <exception cref="System.InvalidOperationException"><paramref name="idValue"/> matches multiple nodes</exception>
/// <returns>
/// null if no match is found
/// node with the given xml:id if one is found
/// node with the given id if no xml:id is found and Strict is false
/// </returns>
/// <see cref='Strict'/>
public
override
XmlElement GetIdElement
(
XmlDocument document,
string
idValue
)
{
if
(
idValue
==
null
)
throw
new
ArgumentNullException
(
"idValue"
,
"Need an ID value to search for"
)
;
if
(
document
==
null
)
return
null
;
// following the pattern defined in the default
// a null document provides no search results, but
// also does not throw an exception.
// setup the namespace mapping for the xml:id namespace
XmlNamespaceManager nsManager
=
new
XmlNamespaceManager
(
document
.
NameTable
)
;
nsManager
.
AddNamespace
(
"xml"
, XmlIdUrl
)
;
// quote the id to search for
string
searchString
=
null
;
if
(
idValue
.
IndexOf
(
'\'
')
==
-
1)
searchString
=
"'"
+
idValue
+
"'"
;
else
if
(
idValue
.
IndexOf
(
'\"
')
==
-
1)
searchString
=
"\""
+
idValue
+
"\""
;
else
throw
new
ArgumentException
(
"idValue"
,
"Cannot search for an xml:id containing both single and double quotes."
)
;
// get the nodes that have xml:ids which mach the given id
XmlNodeList xmlIdNodes
=
document
.
SelectNodes
(
"//*[@xml:id="
+
searchString
+
"]"
, nsManager
)
;
// xml:id's must be unique in the document
if
(
xmlIdNodes
.
Count
>
1)
throw
new
InvalidOperationException
(
"Search for a non-unique xml:id"
)
;
// we found an xml:id that matches, so return that one
else
if
(
xmlIdNodes
.
Count
==
1)
return
xmlIdNodes
[
0]
as
XmlElement;
// there are no matching xml:id's, if strict matching was requested fail, otherwise
// default to the SignedXml search
if
(
Strict
)
return
null
;
else
return
base
.
GetIdElement
(
document, idValue
)
;
}
}
2 Comments
Pages
Blog - Comment List MSDN TechNet
Comments
Loading...
Leave a Comment
Name
Comment
Please add 2 and 5 and type the answer here:
Post