C# Net核心-将SAML断言转换为ClaimsPrincipal
有一个问题,但我需要额外的帮助 这里唯一的答案提到了Kentor.AuthServices,但我不知道如何使用它。我在这个或其他SAML库、文档、博客帖子和示例应用程序中找到的所有内容都是关于联系外部身份验证服务以及处理登录和注销的 但我不需要这些。我正在使用的安装程序在面向边缘的防火墙应用程序中执行此操作,并且登录/注销请求永远不会到达我的应用程序。我得到的只是一个cookie中的SAML令牌,我需要验证它并将其转换为ClaimsPrincipal。我不能(部署网络设置非常偏执)也不想联系任何身份提供商 目前,我已经编写了一个中间件,它接受cookie,解析cookie,并解析出声明主体所需的部分。但我不做任何验证,不管是XML签名还是SAML有效性(有效时间属性等)。使用.NETCore2.0Preview 2,我可以进行XML签名验证,但我仍然坚持进行SAML验证。是否有一个库只验证SAML约束而不做其他事情(或者,至少,我可以忽略所有其他事情)?我相信Kentor、ITfoxtec或elerch的SAML2.Core必须包含这样的功能,但我不知道它在哪里。C# Net核心-将SAML断言转换为ClaimsPrincipal,c#,asp.net-core,saml,saml-2.0,kentor-authservices,C#,Asp.net Core,Saml,Saml 2.0,Kentor Authservices,有一个问题,但我需要额外的帮助 这里唯一的答案提到了Kentor.AuthServices,但我不知道如何使用它。我在这个或其他SAML库、文档、博客帖子和示例应用程序中找到的所有内容都是关于联系外部身份验证服务以及处理登录和注销的 但我不需要这些。我正在使用的安装程序在面向边缘的防火墙应用程序中执行此操作,并且登录/注销请求永远不会到达我的应用程序。我得到的只是一个cookie中的SAML令牌,我需要验证它并将其转换为ClaimsPrincipal。我不能(部署网络设置非常偏执)也不想联系任何
斯科特的这个博客用简单的方式解释了这一点 我已经使用System.IdentityModel.Tokens中的SecurityTokenHandlerCollection类完成了此操作 我希望这段代码能帮助你
public Saml2SecurityToken DeserializeSAMLResponse(string samlResponse)
{
//Deserializing saml response
Saml2SecurityToken token;
using (var reader = XmlReader.Create(new StringReader(samlResponse)))
{
reader.ReadToFollowing("Assertion", Infrastructure.Enumerations.StringEnum.GetStringValue(SAMLProtocoles.SAML_20_ASSERTION));
// Deserialize the token so that data can be taken from it and plugged into the RSTR
SecurityTokenHandlerCollection tokenHandlerCollection = SecurityTokenHandlerCollection.CreateDefaultSecurityTokenHandlerCollection();
token = (Saml2SecurityToken)tokenHandlerCollection.ReadToken(reader.ReadSubtree());
}
//Deserializing successful
return token;
}
它将在内部验证SAML并在Saml2SecurityToken中解析它
获得令牌后,您可以像这样访问用户凭据
public User ReadSamlResponse(string samlResponse, string profileName, bool isSAMLProfile = true)
{
User User = new User();
var DecodedSamlResponse = Convert.FromBase64String(samlResponse);
string ResponseDecoded = coding.UTF8.GetString(DecodedSamlResponse);
Saml2SecurityToken Token = _samlAuthenticationService.DeserializeSAMLResponse(ResponseDecoded);
if ()// apply condition here if you need to validate signature
{
if (!_samlAuthenticationService.ValidateSamlToken(ResponseDecoded, AuthenticationConnector, isSAMLProfile))
throw new Exception("Signature is invalid");
}
User = GetUserFromToken(Token);
return User;
}
要获取用户的安全令牌,您可以这样做
public User GetUserFromToken(Saml2SecurityToken Token)
{
//Get user information from the token started
User User = new User();
if (Token != null)
{
if (Token.Assertion.Subject.NameId != null && (Token.Assertion.Subject.NameId.Format == null || Token.Assertion.Subject.NameId.Format.OriginalString == "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress"))
User.EmailAddress = Token.Assertion.Subject.NameId.Value;
foreach (var Statement in Token.Assertion.Statements)
{
var AttributeStatement = Statement as Saml2AttributeStatement;
var AuthenticationStatement = Statement as Saml2AuthenticationStatement;
if (AttributeStatement != null)
foreach (var Saml2Attribute in AttributeStatement.Attributes)
{
if (Saml2Attribute.Name.Equals("mail") || Saml2Attribute.Name.Equals("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"))
User.EmailAddress = Saml2Attribute.Values[0];
if (Saml2Attribute.Name.Equals("uid") || Saml2Attribute.Name.Equals("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name"))
User.Name = Saml2Attribute.Values[0];
if (Saml2Attribute.Name.Equals("phone"))
User.MobileNumber = Saml2Attribute.Values[0];
if (Saml2Attribute.Name.Equals("title"))
User.JobTitle = Saml2Attribute.Values[0];
if (Saml2Attribute.Name.Equals("company"))
User.CompanyName = Saml2Attribute.Values[0];
}
if (AuthenticationStatement != null)
{
User.SAMLSessionIndex = AuthenticationStatement.SessionIndex;
}
}
}
//Successfully parsed user credentials
return User;
}
net core yetI不支持System.IdentityModel。我认为您应该手动将SAML断言解析为calims原则