Last active
February 10, 2022 21:42
-
-
Save MadhavJ/c5adeaf20f45817417ce3d1e240aef97 to your computer and use it in GitHub Desktop.
Code for configuring custom user builder for Azure AD integration with sitecore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CustomExternalUserBuilder : DefaultExternalUserBuilder | |
{ | |
public CustomExternalUserBuilder(string isPersistentUser) : base(bool.Parse(isPersistentUser)) { } | |
protected override string CreateUniqueUserName(UserManager userManager, ExternalLoginInfo externalLoginInfo) | |
{ | |
Assert.ArgumentNotNull((object)userManager, nameof(userManager)); | |
Assert.ArgumentNotNull((object)externalLoginInfo, nameof(externalLoginInfo)); | |
identityProvider = this.FederatedAuthenticationConfiguration.GetIdentityProvider(externalLoginInfo.ExternalIdentity); | |
string defaultDomain = "sitecore\\"; | |
if (string.IsNullOrWhiteSpace(externalLoginInfo.Email)) | |
{ | |
if (identityProvider != null) | |
{ | |
defaultDomain = identityProvider.Domain; | |
var emailClaim = externalLoginInfo.ExternalIdentity.Claims.FirstOrDefault(x => x.Type == "email"); | |
if (emailClaim != null) return defaultDomain + emailClaim.Value; | |
} | |
var validUserName = externalLoginInfo.DefaultUserName.Replace(",", "").Replace(".", "").Replace("'", ""); | |
return defaultDomain + validUserName.Replace(" ", ""); | |
} | |
return defaultDomain + externalLoginInfo.Email; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<sitecore> | |
<federatedAuthentication> | |
<identityProvidersPerSites> | |
<mapEntry name="all sites" type="Sitecore.Owin.Authentication.Collections.IdentityProvidersPerSitesMapEntry, Sitecore.Owin.Authentication" resolve="true"> | |
<sites hint="list"> | |
<site>regexp:.*</site> | |
</sites> | |
<externalUserBuilder type="BasicCompany.Foundation.Extensions.UserBuilders.CustomExternalUserBuilder, BasicCompany.Foundation.Extensions" resolve="true"> | |
<IsPersistentUser>true</IsPersistentUser> | |
</externalUserBuilder> | |
</mapEntry> | |
</identityProvidersPerSites> | |
</federatedAuthentication> | |
</sitecore> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment