simpleidserver / SimpleIdServer

OpenID, OAuth 2.0, SCIM2.0, UMA2.0, FAPI, CIBA & OPENBANKING Framework for ASP.NET Core
https://simpleidserver.com/
Apache License 2.0
737 stars 99 forks source link

how to custom saml attributes #800

Open AcTeemo opened 2 weeks ago

AcTeemo commented 2 weeks ago

Hello! I need to add the red attributes shown in the image to the SAML response. How can I do that? plz give me some help. THX!!!!!

image

simpleidserver commented 2 weeks ago

Hello,

I have made some modifications to the Release503 branch to support your requirement.

Could you please follow these steps?

  1. Create a new class CustomSaml2AuthResponseEnricher and copy the following content:
public class CustomSaml2AuthResponseEnricher : ISaml2AuthResponseEnricher
{
    public void Enrich(Saml2SecurityToken securityToken)
    {
        var statements = securityToken.Assertion.Statements;
        foreach (var statement in statements)
        {
            if (statement is Saml2AttributeStatement attributeStatement)
            {
                var userNameAttr = attributeStatement.Attributes.SingleOrDefault(a => a.FriendlyName == "username");
                if(userNameAttr != null)
                {
                    userNameAttr.Name = ClaimTypes.Upn;
                    userNameAttr.NameFormat = new Uri(Saml2MetadataConstants.AttributeNameFormat);
                }
            }
        }
    }
}
  1. Edit the Program.cs file and register the new dependency:
services.AddTransient<ISaml2AuthResponseEnricher, CustomSaml2AuthResponseEnricher>();
AcTeemo commented 2 weeks ago

Thank you for your reply. This should be helpful for me, but I have one question: I'm not sure if there is a FriendlyName attribute, because I can't see the ResponseXml.