【问题标题】:Convert Identity Foundation Token to ClaimsPrincipal将 Identity Foundation 令牌转换为 ClaimsPrincipal
【发布时间】:2013-01-30 14:06:53
【问题描述】:

我正在开发一个控制台应用程序,作为通过 AD FS 进行身份验证的概念证明。我到目前为止的代码是

using System.IdentityModel.Protocols.WSTrust;
using System.IdentityModel.Tokens;
using System.IO;
using System.Linq;
using System.Security.Claims;
using System.ServiceModel;
using System.ServiceModel.Security;
using System.Xml;
using Thinktecture.IdentityModel.WSTrust;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var factory = new WSTrustChannelFactory(new WindowsWSTrustBinding(SecurityMode.TransportWithMessageCredential), new EndpointAddress("https://dcadfs.security.net/adfs/services/trust/13/windowsmixed"))
            {
                TrustVersion = TrustVersion.WSTrust13
            };

            var rst = new RequestSecurityToken
            {
                RequestType = RequestTypes.Issue,
                KeyType = KeyTypes.Bearer,
                AppliesTo = new EndpointReference("https://dcadfs.security.net/adfs/services/trust/13/windowsmixed")
            };

            var channel = factory.CreateChannel();
            var genericToken= channel.Issue(rst) as GenericXmlSecurityToken;

            if (genericToken != null)
            {
                var sh = new WindowsUserNameSecurityTokenHandler();
                //Next line errors with ID4065: Cannot read security token. Exepected elemt is username
                var token = sh.ReadToken(new XmlTextReader(new StringReader(genericToken.TokenXml.OuterXml)));
                var claimsPrincipal = new ClaimsPrincipal(sh.ValidateToken(token2).First());
            }           

        }
    }
}

ReadToken 失败,出现预期的名称元素错误,我想我可能使用了错误的处理程序/初始化错误?

我还认为我遗漏了其他几点

  1. 我绝不会向 AD FS 表明依赖方是谁。
  2. 我不确定将 ApplyTo 设置为什么,因为我没有使用服务,因此我将其设置为 AD FS 端点。

任何关于其中任何一个的建议都会很棒。

真的很难在基于 WCF/配置的方法之外找到这样的示例。我正在尝试将其全部写入代码,以便更好地了解它的工作原理。

【问题讨论】:

    标签: wif adfs


    【解决方案1】:

    1 和 2 相关 - ApplyTo 指定 ADFS 中的 RP。

    返回的是 SAML 令牌 - 因此您需要使用 SamlSecurityTokenHandler 来读取令牌(不是 WindowsUserName...)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-03
      • 1970-01-01
      • 1970-01-01
      • 2014-10-12
      • 2016-08-29
      • 2013-06-28
      • 2011-04-21
      • 2019-07-13
      相关资源
      最近更新 更多