Identity Model Claims With XML Characters Within Them

我的未来我决定 提交于 2020-01-06 13:11:19

问题


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>&lt;test&gt;Hi&lt;/test&gt;</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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!