×

iFour Logo

Integration of Azure AD B2C with .NET desktop applications

iFour Team January 26, 2017

Listening is fun too.

Straighten your back and cherish with coffee - PLAY !

  • play
  • pause
  • pause
Integration of Azure AD B2C with .NET desktop applications

1. Introduction


Using Azure Active Directory (Azure AD) B2C, powerful self-service identity management features can be added to desktop app in a few short steps by asp.net software companies USA.

This article shows how to create a .NET Windows Presentation Foundation (WPF) app that includes user sign-up, sign-in, and profile management. This app includes support for sign-up and sign-in by using a user name or email. It supports sign-up and sign-in by social accounts such as Facebook and Google.

2. Get an Azure AD B2C Directory


Before using Azure AD B2C, we must create a directory, or tenant. A directory is a container for all of your users, applications, groups, and more. If you don't have directory already then create a B2C directory.

3. Create an Application


  1. Next we need to create application in B2C Directory. It gives Azure AD information that need to securely communicate with application. For creating an application in B2C directory follow these steps.

  2. Make sure to:

    Create Application in B2C Directory
    1. Include a native client in the application.

    2. Copy the Redirect URI urn:ietf:wg:oauth:2.0:oob. It's the default URL for application.

    3. Copy the Application ID that is assigned to your app. we will need it later.

Looking for a trusted .NET development company ? Contact us now

4. Create a policies


Azure ad B2C with Net Desktop Applications Integration
  1. In Azure AD B2C, every user experience is denoted by a policy. We need to create a policy for each type(Sign-in, Sign-up and Edit-profile).

  2. When you create the policies, be sure to:

    Integration of Azure ad B2C with Net Desktop Applications
    Integration with Net Desktop Applications
    Azure ad B2C with .Net Desktop Applications Integration
    1. Choose either User ID sign-up or Email sign-up in the identity providers blade.

    2. Choose Display name and other sign-up attributes in your sign-up policy.

    3. Choose Display name and Object ID claims as application claims for every policy.

    4. Copy the Name of each policy after creating. It should have the prefix b2c_1_.

Kickstart your journey with Desktop Application Development Company? Contact us now

5. Build a Windows desktop app


Step 1: Creating WPF Project
Integration with Dot Net Desktop Applications
Integration of Azure Ad B2C with Dot Net Desktop Applications
Integration Method of Azure Ad B2C with .Net Desktop Application

The primary class of Microsoft Authentication Library (MSAL) is PublicClientApplication. This class represents your application in the Azure AD B2C system. When the app initializes, create an instance of PublicClientApplication in MainWindow.xaml.cs. This can be used throughout the window.

Integration of Azure ad B2C with Dot Net Application
Integration of Azure Ad B2C with Dotnet Desktop Application

When a user opts to signs up, you want to initiate a sign-up flow that uses the sign-up policy you created. By using MSAL, you just call pca.AcquireTokenAsync(). The parameters you pass to AcquireTokenAsync() determine which token you receive, the policy used in the authentication request, and more.

Integration of Azure ad B2C with .net Desktop Applications

You can initiate a sign-in flow in the same way that you initiate a sign-up flow. When a user signs in, make the same call to MSAL, this time by using your sign-in policy:

Integration of Azure ad B2C with Net Desktop Application

Again, you can execute an edit-profile policy in the same behavior:

In all of these cases, MSAL either returns a token in AuthenticationResult or throws an exception. Each time you get a token from MSAL, you can use the AuthenticationResult.User object to update the user data in the app, such as the UI.

Azure ad B2C with .Net Desktop Application Integration

Finally, we can end a user's session with the app when the user selects Sign out. When using MSAL, this is accomplished by clearing all of the tokens from the token cache:

Integration of Azure ad B2C with .Net Desktop Application
  • Add a new WPF application named "AzureADB2CDesktopApp"

  • Install the needed NuGet Packages to Configure the MVC App

    Install-Package Microsoft.Identity.Client -IncludePrerelease
  • Create a class file Globals.cs, open the file and add property values. his class is used throughout AzureADB2CDesktopApp to reference commonly used values.

  • Create a class file FileCache.cs for simple persist cache implementation of desktop app.

  • Create the PublicClientApplication

  • Check for tokens on App Start

  • Sign-up flow

  • Sign-in flow

  • Edit-profile flow

  • Sign-out flow

Step 2: Run WPF Project


Integration .Net Desktop Application

Planning to Hire ASP.Net Developer ? Your Search ends here

 

Integration of Azure Ad B2C with Dot Net Desktop Application
Azure B2C Example
  • Click on Sign In button, it will open Azure AD B2C Tenant Sign In browser window and enter the credential.

  • If the credentials provided are valid then a successful authentication will take place and a token will be obtained and stored in the claims identity for the authenticated user and navigate to main window.

 

Conclusion


By using Azure Active Directory (Azure AD) B2C, ASP.NET software development companies can add powerful self-service identity management features to desktop applications.

Integration of Azure AD B2C with .NET desktop applications 1. Introduction Using Azure Active Directory (Azure AD) B2C, powerful self-service identity management features can be added to desktop app in a few short steps by asp.net software companies USA. This article shows how to create a .NET Windows Presentation Foundation (WPF) app that includes user sign-up, sign-in, and profile management. This app includes support for sign-up and sign-in by using a user name or email. It supports sign-up and sign-in by social accounts such as Facebook and Google. 2. Get an Azure AD B2C Directory Before using Azure AD B2C, we must create a directory, or tenant. A directory is a container for all of your users, applications, groups, and more. If you don't have directory already then create a B2C directory. 3. Create an Application Next we need to create application in B2C Directory. It gives Azure AD information that need to securely communicate with application. For creating an application in B2C directory follow these steps. Make sure to: Include a native client in the application. Copy the Redirect URI urn:ietf:wg:oauth:2.0:oob. It's the default URL for application. Copy the Application ID that is assigned to your app. we will need it later. Looking for a trusted .NET development company ? Contact us now See here 4. Create a policies In Azure AD B2C, every user experience is denoted by a policy. We need to create a policy for each type(Sign-in, Sign-up and Edit-profile). When you create the policies, be sure to: Choose either User ID sign-up or Email sign-up in the identity providers blade. Choose Display name and other sign-up attributes in your sign-up policy. Choose Display name and Object ID claims as application claims for every policy. Copy the Name of each policy after creating. It should have the prefix b2c_1_. Kickstart your journey with Desktop Application Development Company? Contact us now See here 5. Build a Windows desktop app Step 1: Creating WPF Project The primary class of Microsoft Authentication Library (MSAL) is PublicClientApplication. This class represents your application in the Azure AD B2C system. When the app initializes, create an instance of PublicClientApplication in MainWindow.xaml.cs. This can be used throughout the window. When a user opts to signs up, you want to initiate a sign-up flow that uses the sign-up policy you created. By using MSAL, you just call pca.AcquireTokenAsync(). The parameters you pass to AcquireTokenAsync() determine which token you receive, the policy used in the authentication request, and more. You can initiate a sign-in flow in the same way that you initiate a sign-up flow. When a user signs in, make the same call to MSAL, this time by using your sign-in policy: Again, you can execute an edit-profile policy in the same behavior: In all of these cases, MSAL either returns a token in AuthenticationResult or throws an exception. Each time you get a token from MSAL, you can use the AuthenticationResult.User object to update the user data in the app, such as the UI. Finally, we can end a user's session with the app when the user selects Sign out. When using MSAL, this is accomplished by clearing all of the tokens from the token cache: Add a new WPF application named "AzureADB2CDesktopApp" Install the needed NuGet Packages to Configure the MVC App Install-Package Microsoft.Identity.Client -IncludePrerelease Create a class file Globals.cs, open the file and add property values. his class is used throughout AzureADB2CDesktopApp to reference commonly used values. Create a class file FileCache.cs for simple persist cache implementation of desktop app. Create the PublicClientApplication Check for tokens on App Start Sign-up flow Sign-in flow Edit-profile flow Sign-out flow Step 2: Run WPF Project Planning to Hire ASP.Net Developer ? Your Search ends here See here   Step 2: Run WPF Project --> Click on Sign In button, it will open Azure AD B2C Tenant Sign In browser window and enter the credential. If the credentials provided are valid then a successful authentication will take place and a token will be obtained and stored in the claims identity for the authenticated user and navigate to main window.   Conclusion By using Azure Active Directory (Azure AD) B2C, ASP.NET software development companies can add powerful self-service identity management features to desktop applications.

Build Your Agile Team

Categories

Ensure your sustainable growth with our team

Talk to our experts
Sustainable
Sustainable
 
Blog Our insights
What’s New in .NET 9?

27 September 2024

Kapil Panchal

What’s New in .NET 9?

Today, we’re thrilled to present you with the first glimpse of .NET 9 release and let you know what features and updates you can anticipate in this new version. Various professionals believe that it’s the right time to explore and adopt the latest version of .NET for your upcoming projects. It is even recommended for projects built using .NET 6 or .NET 8, due to the framework updates made in this version.

.NET MAUI vs React Native for Cross-platform Applications

30 August 2024

Kapil Panchal

.NET MAUI vs React Native for Cross-platform Applications

The dominance of Android, which holds a 71% market share, coupled with iOS supremacy in the US market, shows just how important it is to create apps that work on different platforms....

What's New in .NET 8?

06 June 2023

Kapil Panchal

What's New in .NET 8?

DOT NET is a prominent platform for custom software development chosen mostly by large-scale IT giants. According to statistics, there are more than 307,590 developers working in the...