如何将SAML XML标记字符串转换为SecurityToken或ClaimsPrincipal实例?

我的背景:

  • .Net RESTful Web服务
  • 客户端(混合平台,技术,libfunction)已获得SAML令牌
  • 尝试在REST服务中接受用于身份validation/授权的令牌
    • 在HTTP授权/ X-Authorization标头中
    • 作为查询参数
  • 稍后还会支持SWT,但需要获得SAML令牌

细节:

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

 ..etc...  

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

我找到了一些诱人的网页/博客/等……看起来很有帮助:

  • Cibrax的想法是在HTTP Authorization标头中传递令牌
  • Dominick Baier在SWT上有类似的东西,提到SAML很容易做同样的事情

我实际上试图将SAML令牌转换为ClaimsPrincipal(通过SecurityToken中间步骤或直接……快乐)。 来自Cibrax的想法的示例代码使用以下内容进行关键validation和反序列化步骤:

 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); 

谢谢你帮我搞砸了!

泰勒

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

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

从post:

  string samlTokenXml = signInResponseXml .DocumentElement //  .ChildNodes[0] //  .ChildNodes[2] //  .InnerXml; //  var xmlTextReader = new XmlTextReader(new StringReader(samlTokenXml)); SecurityTokenHandlerCollection handlers = FederatedAuthentication.FederationConfiguration.IdentityConfiguration.SecurityTokenHandlers; // read the token SecurityToken securityToken = handlers.ReadToken(xmlTextReader); 

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

http://leastprivilege.com/

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

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

http://identitymodel.codeplex.com/

要解决最后一个exception,请检查标签及其内容并确保其正确无误。 我不能说哪个元素有问题。 我们有时会出现此错误,并且每次出现格式错误的identitymodel部分。

好的,一些进展…如果我做以下,我会进一步:

 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服务主机分配中遇到exception:

 <%@ 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}}