Created
May 30, 2020 16:23
-
-
Save manoj-choudhari-git/4a215237a689f077c479f2cd27ed6e9a to your computer and use it in GitHub Desktop.
Azure AD B2C with WPF App and Web API
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 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"; | |
// Add Api endpoint and Api scope | |
public static string ApiEndpoint = "https://localhost:44379/weatherforecast"; | |
public static string[] ApiScopes = { "https://samplead.onmicrosoft.com/sample-api/api-scope" }; | |
// These are collective scopes sent with sign in request | |
public static string[] Scopes = { "openid", "profile", "https://samplead.onmicrosoft.com/sample-api/api-scope" }; | |
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