WIF 的自定义 SecurityTokenHandler

发布于 2024-11-19 14:40:00 字数 1081 浏览 5 评论 0原文

我正在尝试实现自定义 SecurityTokenSecurityTokenHandler 用于使用 Microsoft.IndentityModel (= Windows Indentity Foundation) 的 STS。

令牌被序列化为带有签名的简单 xml 文档(使用 X509 证书),并且有时(并非总是)被加密(取决于目标领域)。

到目前为止,它运行得很好,但我陷入了 SecurityTokenHandler.CreateSecurityTokenReference(SecurityToken token, bool Attached) 应该返回一个 SecurityKeyIndetifierClause

我的问题是:什么是 SecurityKeySecurityKeyIndentifierSecurityKeyIndentifierClause 一般来说和我的 sceanrio (rsa 签名(和加密) xml 令牌)具体来说?

MSDN 中几乎没有任何文档,而且我找不到任何关于此主题的其他有用信息。

提前致谢。

PS:我知道最简单和推荐的方法是使用像 saml 这样的令牌格式构建,但令牌是由遗留系统评估的,该遗留系统需要我无法影响的特定格式。

I am trying to implement a custom SecurityToken and SecurityTokenHandler for a STS using Microsoft.IndentityModel (= Windows Indentity Foundation).

The token is serialized to a simple xml document with signature (using a X509 certificate) and is sometimes (not always) encrypted (depends on the target realm).

Till now it worked quite well, but i got stuck on SecurityTokenHandler.CreateSecurityTokenReference(SecurityToken token, bool attached) which should return a SecurityKeyIndetifierClause.

My question is: What is a SecurityKey, SecurityKeyIndentifier and SecurityKeyIndentifierClause in general and for my sceanrio (rsa signed (and encrypted) xml token) in specific?

There is almost no documentation in MSDN and I couldn't find anything else helpful on this topic.

Thanks in advance.

P.S.: I know the easiest and recommended way is to use a build in token format like saml, but the token is evaluated by a legacy system which expects a specific format i have no influence on.

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

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

发布评论

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

评论(2

峩卟喜欢 2024-11-26 14:40:00

与此同时,我自己找到了问题的答案:

SecurityKey

SecurityKey 用于加密操作。不记名令牌实现不需要这样做。因此,您可以在 SecurityToken 的相应属性中返回一个空列表:

public override ReadOnlyCollection<SecurityKey> SecurityKeys
{
    get { return new List<SecurityKey>().AsReadOnly(); }
}

SecurityKeyIdentifierClause

正如其他答案已经指出的,SecurityKeyIdentifierClause 是友善的安全令牌的唯一标识符。 SecurityTokenResolver 使用它来返回指定 SecurityKeyIdentifierClause 对应的 SecurityToken

您自己的 SecurityTokenHandler 实现的最佳解决方案可能是返回 LocalIdKeyIdentifierClause,将您的令牌 ID 作为 localId 参数:

public override SecurityKeyIdentifierClause CreateSecurityTokenReference(SecurityToken token, bool attached)
{
    if (token == null)
        throw new ArgumentNullException("token");

    return new LocalIdKeyIdentifierClause(token.Id);
}

SecurityKeyIdentifier

A SecurityKeyIdentifierSecurityKeyIdentifierClauses 的集合。当需要时,您可以在此处使用 System.IdentityModel.Tokens 中的实现。通常不需要您自己处理这个问题。

In the meantime I found answers to the questions my self:

SecurityKey

A SecurityKey is used for cryptographic operations. This is not needed by bearer token implementations. Therefore you can just return an empty list in the corresponding property of the SecurityToken:

public override ReadOnlyCollection<SecurityKey> SecurityKeys
{
    get { return new List<SecurityKey>().AsReadOnly(); }
}

SecurityKeyIdentifierClause

As already pointed out by the other answer a SecurityKeyIdentifierClause is kind of the unique identifier of a security token. It is used by a SecurityTokenResolver to return the corresponding SecurityToken for a specified SecurityKeyIdentifierClause.

Probably the best solution for your own SecurityTokenHandler implementation is to return a LocalIdKeyIdentifierClause with the id of your token as localId parameter:

public override SecurityKeyIdentifierClause CreateSecurityTokenReference(SecurityToken token, bool attached)
{
    if (token == null)
        throw new ArgumentNullException("token");

    return new LocalIdKeyIdentifierClause(token.Id);
}

SecurityKeyIdentifier

A SecurityKeyIdentifier is a collection of SecurityKeyIdentifierClauses. When ever needed you can use the implementation in System.IdentityModel.Tokens here. There is usually no need to take care of this by your self.

提笔书几行 2024-11-26 14:40:00

密钥标识符与自定义令牌一起使用可以完成几件事。它们描述令牌,和/或指向其他相关令牌(因为令牌可以只是指针 - 也许出于性能原因等)。如果您不需要密钥标识符,您可以执行以下操作:

  • 从 CanWriteKeyIdentifierClause 返回 false:

    public override bool CanWriteKeyIdentifierClause(SecurityKeyIdentifierClause securityKeyIdentifierClause)
    {
    返回假;
    public

  • 从 CreateSecurityTokenReference 返回默认(或 null)值:

    公共覆盖SecurityKeyIdentifierClause CreateSecurityTokenReference(SecurityToken令牌,附加布尔值)
    {
    返回默认值(SecurityKeyIdentifierClause);
    从CreateSecurityTokenReference

The key identifiers are used with custom tokens to do a couple things. They describe the token, and/or point to other related tokens (because tokens can just be pointers - perhaps for performance reasons et al). if you do not need key identifier, you can do two things:

  • Return false from CanWriteKeyIdentifierClause:

    public override bool CanWriteKeyIdentifierClause(SecurityKeyIdentifierClause securityKeyIdentifierClause)
    {
    return false;
    }

  • Return a default (or null) value from CreateSecurityTokenReference:

    public override SecurityKeyIdentifierClause CreateSecurityTokenReference(SecurityToken token, bool attached)
    {
    return default(SecurityKeyIdentifierClause);
    }

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