我在字符串中有一个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);
感谢您帮助我摆脱困境!
泰勒
答案 0 :(得分:2)
刚发现这很有帮助。 http://www.tecsupra.com/blog/system-identitymodel-manually-parsing-the-saml-token/
基本思路:你需要“Audience”-node的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);
答案 1 :(得分:1)
我想分享一些我认为在实现基本相同的场景时非常有用的资源。基本上,Dominick Baier是这个领域的神。他的博客上有很多关于这个主题的信息:
在RESTful服务中将SAML / SWT令牌转换为IClaimsIdentity:
答案 2 :(得分: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}}
答案 3 :(得分:0)
要解决最后一个异常,请检查标签及其内容并确保其正确无误。我不能说哪个元素有问题。我们有时会出现此错误,并且每次出现错误的身份模式部分时都会出错。