身份模型声明中包含 XML 字符

发布于 2024-11-25 17:26:46 字数 428 浏览 0 评论 0原文

我想做一些类似的事情

 outputIdentity.Claims.Add(new Claim("Claim1", "<test>Hi</test>"))

,但是响应标头本身中的安全节点会显示它,因为

<Attribute Name="Claim1"><AttributeValue>&lt;test&gt;Hi&lt;/test&gt;</AttributeValue></Attribute>

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七秒鱼° 2024-12-02 17:26:47

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

如果您想自定义序列化,则必须通过扩展 Saml11SecurityTokenHandler 类来覆盖默认行为:

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 文件中添加自定义安全令牌处理程序:

<securityTokenHandlers>
  <add type="Your.Namespace.CustomHandler, Your.Dll.Name, Version=1.0.0.0, Culture=neutral" />
</securityTokenHandlers>

编辑:删除了

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 />

红焚 2024-12-02 17:26:47

您可以尝试将值包装在 CDATA 部分中吗?作为:

<![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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文