问题
I'd like to do something like
outputIdentity.Claims.Add(new Claim("Claim1", "<test>Hi</test>"))
However the security node within the response header itself shows it as
<Attribute Name="Claim1"><AttributeValue><test>Hi</test></AttributeValue></Attribute>
I know they are reserved XML characters getting translated but can't I specify that I want that node structure in my attribute?
NOTE: I've also tried wrapping it in CDATA however it serializes that tag too. When I replace the translated characters, it works.
回答1:
Serialization of security tokens is done by the SecurityTokenHandler
(in your case probably the Saml11SecurityTokenHandler
).
If you want to customize serialization you have to overwrite the default behaviour by extending the Saml11SecurityTokenHandler
class:
class CustomHandler : Saml11SecurityTokenHandler
{
public Saml11SecurityTokenHandler()
: base()
{
}
public Saml11SecurityTokenHandler(SamlSecurityTokenRequirement samlSecurityTokenRequirement)
: base(samlSecurityTokenRequirement)
{
}
public Saml11SecurityTokenHandler(XmlNodeList customConfigElements)
: base(customConfigElements)
{
}
protected override void WriteAttribute(XmlWriter writer, SamlAttribute attribute)
{
// your code here
}
}
You also have to add your custom security token handler in the web.config file:
<securityTokenHandlers>
<add type="Your.Namespace.CustomHandler, Your.Dll.Name, Version=1.0.0.0, Culture=neutral" />
</securityTokenHandlers>
EDIT: removed <clear />
回答2:
Can you try wrapping the value in CDATA section? As:
<![CDATA[<test>Hi</test>]]>
Not sure whether your SecurityTokenHandler class will handle that properly, but it's worth a try, and easier than introducing custom handlers.
来源:https://stackoverflow.com/questions/6795371/identity-model-claims-with-xml-characters-within-them