Skip to content

Instantly share code, notes, and snippets.

@sergiutesu
Created September 21, 2022 07:30
Show Gist options
  • Save sergiutesu/29c16ba76a8a0047d5f7cd2a1c20a84b to your computer and use it in GitHub Desktop.
Save sergiutesu/29c16ba76a8a0047d5f7cd2a1c20a84b to your computer and use it in GitHub Desktop.
using System.Security.Claims;
namespace AzureAD
{
public static class MemberAuthenticationExtensions
{
public static IUmbracoBuilder ConfigureAuthenticationMembers(this IUmbracoBuilder builder)
{
builder.Services.ConfigureOptions<AzureB2CMembersExternalLoginProviderOptions>();
builder.AddMemberExternalLogins(logins =>
{
//const string schema = MicrosoftAccountDefaults.AuthenticationScheme;
logins.AddMemberLogin(
membersAuthenticationBuilder =>
{
membersAuthenticationBuilder.AddOpenIdConnect(
membersAuthenticationBuilder.SchemeForMembers(AzureB2CMembersExternalLoginProviderOptions.SchemeName),
options =>
{
options.SaveTokens = true;
//Obtained from the AZURE AD B2C WEB APP
options.ClientId = "";
//Obtained from the AZURE AD B2C WEB APP
options.ClientSecret = "";
//Callbackpath - Important! The CallbackPath represents the URL to which the browser should be redirected to and the default value is /signin-oidc This should be unique!.
options.CallbackPath = "/umbraco-b2c-members-signin";
//Obtained from user flows in your Azure B2C tenant
options.MetadataAddress = "";
options.Events.OnTokenValidated = async context =>
{
ClaimsPrincipal? principal = context.Principal;
if (principal is null)
{
throw new InvalidOperationException("No claims found.. :(");
return;
}
var claims = principal.Claims.ToList();
Claim? email = claims.SingleOrDefault(x => x.Type == "emails");
if (email is not null)
{
claims.Add(new Claim(ClaimTypes.Email, email.Value));
}
Claim? name = claims.SingleOrDefault(x => x.Type == "name");
if (name is not null)
{
claims.Add(new Claim(ClaimTypes.Name, name.Value));
}
var authenticationType = principal.Identity?.AuthenticationType;
context.Principal = new ClaimsPrincipal(new ClaimsIdentity(claims, authenticationType));
};
});
});
});
return builder;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment