包含 XML 字符的身份模型声明 [英] Identity Model Claims With XML Characters Within Them

查看:59
本文介绍了包含 XML 字符的身份模型声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做类似的事情

 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>

我知道它们是被翻译的保留 XML 字符,但我不能在我的属性中指定我想要该节点结构吗?

I know they are reserved XML characters getting translated but can't I specify that I want that node structure in my attribute?

注意:我也尝试将它包装在 CDATA 中,但它也会序列化该标签.当我替换翻译的字符时,它起作用了.

NOTE: I've also tried wrapping it in CDATA however it serializes that tag too. When I replace the translated characters, it works.

推荐答案

安全令牌的序列化由 SecurityTokenHandler(在您的情况下可能是 Saml11SecurityTokenHandler)完成.

Serialization of security tokens is done by the SecurityTokenHandler (in your case probably the Saml11SecurityTokenHandler).

如果您想自定义序列化,您必须通过扩展 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
    }
}

您还必须在 web.config 文件中添加自定义安全令牌处理程序:

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:删除了

这篇关于包含 XML 字符的身份模型声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆