25

我的背景:

  • .Net RESTful 网络服务
  • 客户端(混合平台、技术、库功能)已获得 SAML 令牌
  • 尝试接受令牌以在 REST 服务中进行身份验证/授权
    • 在 HTTP Authorization / X-Authorization 标头中
    • 作为查询参数
  • 稍后还将支持 SWT,但需要获取 SAML 令牌

细节:

我在字符串中有一个 SAML 令牌:

<saml:Assertion xmlns:saml="..." ...> ..etc... </>

在 HttpModule 中,我想将其转换为 ClaimsPrincipal 以便我的服务可以将通常的 Thread.CurrentPrincipal 用作 IClaimsPrincipal 的东西。

我发现了几个诱人的页面/博客/等...看起来很有帮助:

我一直试图将 SAML 令牌转换为 ClaimsPrincipal (通过 SecurityToken 中间步骤或直接......无论哪种方式都很高兴)。Cibrax 想法的示例代码将以下内容用于关键的验证和反序列化步骤:

SecurityTokenSerializer securityTokenSerializer 
    = new SecurityTokenSerializerAdapter(
        FederatedAuthentication.SecurityTokenHandlers, 
        MessageSecurityVersion.Default.SecurityVersion, 
        false, new SamlSerializer(), null, null);

SecurityToken theToken 
    = WSFederationAuthenticationModule.GetSecurityToken(
        theSamlTokenInStringForm, securityTokenSerializer);

我遇到的障碍是 WIF 的 RTM 版本没有公开 GetSecurityToken 的这种重载......它只公开:

WSFederationAuthenticationModule fam = new WSFederationAuthenticationModule();
SecurityToken theToken = fam.GetSecurityToken(HttpRequest theRequest);
SecurityToken theToken = fam.GetSecurityToken(SignInResponseMessage message);

感谢您帮助我摆脱困境!

泰勒

4

4 回答 4

3

刚刚发现这很有帮助。 http://www.tecsupra.com/blog/system-identitymodel-manually-parsing-the-saml-token/

基本思想:您需要“Audience”节点的 XML,然后您可以使用 SecurityTokenHandlerCollection 并使用“ValidateToken”

从帖子:

       string samlTokenXml = signInResponseXml
            .DocumentElement  // <trust:RequestSecurityTokenResponseCollection>
            .ChildNodes[0] // <trust:RequestSecurityTokenResponse>
            .ChildNodes[2] // <trust:RequestedSecurityToken>
            .InnerXml; // <Assertion>

        var xmlTextReader = new XmlTextReader(new StringReader(samlTokenXml));

        SecurityTokenHandlerCollection handlers = 
       FederatedAuthentication.FederationConfiguration.IdentityConfiguration.SecurityTokenHandlers;

        // read the token
        SecurityToken securityToken = handlers.ReadToken(xmlTextReader);
于 2013-09-16T14:16:15.983 回答
1

我想分享一些我发现在实现基本相同的场景时非常有用的资源。基本上,Dominick Baier是这个领域的神。他的博客充满了关于这个主题的重要信息:

http://leastprivilege.com/

在 RESTful 服务中将 SAML/SWT 令牌转换为 IClaimsIdentity:

http://www.develop.com/wcfrest/

http://identitymodel.codeplex.com/

于 2012-09-27T14:03:58.480 回答
0

好的,一些进展......如果我执行以下操作,我会走得更远:

Microsoft.IdentityModel.Configuration.ServiceConfiguration serviceConfig
    = new Microsoft.IdentityModel.Configuration.ServiceConfiguration();

// Now read the token and convert it to an IPrincipal
SecurityToken theToken = null;
ClaimsIdentityCollection claimsIdentity = null;
using (XmlReader reader = XmlReader.Create(new StringReader(authSamlString)))
{
    theToken = serviceConfig.SecurityTokenHandlers.ReadToken(reader);
    claimsIdentity = serviceConfig.SecurityTokenHandlers.ValidateToken(theToken);
}

IPrincipal principal = new ClaimsPrincipal(claimsIdentity);

我撞到的下一堵墙:

我现在在向导生成的 REST 服务主机分配中遇到异常:

<%@ ServiceHost Language="C#" Debug="true" Service="Sample.RestService.Service" Factory="Sample.RestService.AppServiceHostFactory"%>

using System;
using System.ServiceModel;
using System.ServiceModel.Activation;
using Microsoft.ServiceModel.Web.SpecializedServices;

namespace Sample.RestService 
{
  class AppServiceHostFactory : ServiceHostFactory
  {
    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        /// ***** The exception occurs on the next line *****
        return new SingletonServiceHost(serviceType, baseAddresses);
    }
  }
}

异常详情:

System.Configuration.ConfigurationErrorsException occurred
  Message="This element is not currently associated with any context"
  Source="System.Configuration"
  BareMessage="This element is not currently associated with any context"
  Line=0
  StackTrace:
       at System.Configuration.ConfigurationElement.get_EvaluationContext()
  InnerException: {{NONE}}
于 2010-04-02T18:22:27.557 回答
0

要解决最后一个异常,请检查标签及其内容并确保其正确。我不能说哪个元素有问题。我们有时会出现这个错误,每次的原因是身份模型部分格式错误。

于 2011-12-13T10:29:06.420 回答