SimpleIdServer icon indicating copy to clipboard operation
SimpleIdServer copied to clipboard

how to custom saml attributes

Open AcTeemo opened this issue 1 year ago • 2 comments

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

AcTeemo avatar Nov 05 '24 09:11 AcTeemo

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

simpleidserver avatar Nov 05 '24 13:11 simpleidserver

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.

AcTeemo avatar Nov 06 '24 00:11 AcTeemo