Skip to content

Instantly share code, notes, and snippets.

@manoj-choudhari-git
Created May 30, 2020 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manoj-choudhari-git/9df2d83f9f0f57a6f56213cd10f69be7 to your computer and use it in GitHub Desktop.
Save manoj-choudhari-git/9df2d83f9f0f57a6f56213cd10f69be7 to your computer and use it in GitHub Desktop.
Azure AD B2C Authentication in .NET Core WPF application
public partial class App : Application
{
private static readonly string Tenant = "samplead.onmicrosoft.com";
private static readonly string AzureAdB2CHostname = "samplead.b2clogin.com";
private static readonly string ClientId = "888fff1d-16c3-4de6-92af-3d4ab54a860a";
private static readonly string RedirectUri = "http://localhost";
public static string PolicySignUpSignIn = "B2C_1_SignUpSignIn";
public static string PolicyEditProfile = "B2C_1_Edit_Profile";
public static string PolicyResetPassword = "B2C_1_Pwd_Reset";
public static string[] Scopes = { "openid", "profile" };
private static string AuthorityBase = $"https://{AzureAdB2CHostname}/tfp/{Tenant}/";
public static string AuthoritySignUpSignIn = $"{AuthorityBase}{PolicySignUpSignIn}";
public static string AuthorityEditProfile = $"{AuthorityBase}{PolicyEditProfile}";
public static string AuthorityResetPassword = $"{AuthorityBase}{PolicyResetPassword}";
public static IPublicClientApplication PublicClientApp { get; private set; }
static App()
{
PublicClientApp = PublicClientApplicationBuilder.Create(ClientId)
.WithB2CAuthority(AuthoritySignUpSignIn)
.WithRedirectUri(RedirectUri)
.WithLogging(Log, LogLevel.Verbose, false) //PiiEnabled set to false
.Build();
TokenCacheHelper.Bind(PublicClientApp.UserTokenCache);
}
private static void Log(LogLevel level, string message, bool containsPii)
{
string logs = ($"{level} {message}");
StringBuilder sb = new StringBuilder();
sb.Append(logs);
File.AppendAllText(System.Reflection.Assembly.GetExecutingAssembly().Location + ".msalLogs.txt", sb.ToString());
sb.Clear();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment