Skip to content

Latest commit

 

History

History
116 lines (79 loc) · 5.14 KB

CxP-2023.md

File metadata and controls

116 lines (79 loc) · 5.14 KB

CxP training | Jan 2023

Understanding the basics Weeks 1 & 2

In this article

Prerequisites

Install Node.

Have your favorite text editor open and ready. We'll be using Visual Studio 2022.

Ideally, install Visual Studio 2022, checking ASP.NET and web development, and .NET Core cross-platform development, or otherwise, install ASP.NET Core 7.0 SDK

We'll be walking through the following pre-configured MSAL samples, if you want to git clone and have them ready:

MSAL JS Single-Page Angular Application

Optional The samples are pre-configured and will work "out of the box", but if you have time and interest, you can configure your own B2C tenant and use that configuration instead: B2C Test Tenant

Lab 01: MSAL JS Angular Single-Page Application

A Single-Page Application (SPA) signing-in with B2C.

Run the SPA

  • git clone https://github.com/Azure-Samples/ms-identity-javascript-angular-tutorial.git

  • cd 1-Authentication/2-sign-in-b2c/SPA

  • Install Node.Js if you haven't already

  • Install and update the Node dependencies

    npm install && npm update

    npm start

Lab 02: MSAL.NET Desktop

Sample here: https://github.com/Azure-Samples/active-directory-dotnet-desktop-msgraph-v2

git clone https://github.com/Azure-Samples/active-directory-dotnet-desktop-msgraph-v2.git desktop

cd desktop

Open the sample in VS 2022 and run.

ADAL to MSAL via Id Web overview Week 3

git clone https://github.com/Azure-Samples/active-directory-dotnet-daemon-certificate-credential.git adalDaemonSample

cd adalDaemonSample

git checkout master

git pull

*.sln tab

Let VS update to .NET Framework 4.8 when the pop-up window appears. There are two samples, the Daemon sample and the web API (TodoListService). We are not going to update the TodoListService but will instead call MS Graph with the Daemon sample.

Steps (do in order, or it will take 1-2 hrs or never work)

  1. Open in VS. Right-click on packages.config. Select "Migrate packages.config to PackageReference.." (reference). Not doing this will make it near impossible to get VS to correctly add the packages. Partners who cannot update have infinite difficulties in, not only updating packages, but adopting new packages.
  2. In Program.cs, delete all the code but leave the method signature static int Main(string[] args)
  3. Change main to static async Task Main(string[] args)
  4. Right click the project and select "Manage NuGet Packages". Search "Microsoft.Identity.Web" in the search bar. Select "Microsoft.Identity.Web.MicrosoftGraph". Use the latest version. Should be 2.x (not preview).
  5. Back in Program.cs, copy/paste the below code in Main:
TokenAcquirerFactory tokenAcquirerFactory = TokenAcquirerFactory.GetDefaultInstance();
            tokenAcquirerFactory.Services.AddMicrosoftGraph();

            var serviceProvider = tokenAcquirerFactory.Build();
            try
            {
                GraphServiceClient graphServiceClient = serviceProvider.GetRequiredService<GraphServiceClient>();
                var users = await graphServiceClient.Users
                    .Request()
                    .WithAppOnly()
                    .GetAsync();
                Console.WriteLine($"{users.Count} users");
                Console.ReadLine();
            }
            catch (Exception ex) { Console.WriteLine("We could not retrieve the user's list: " + $"{ex}"); }
  1. Go to the App.config. Comment out all the lines under <appSettings>
  2. Right-click the project again, add a new class. Name it appsettings.json. Copy/paste this code in the new file:
{

  "Instance": "https://login.microsoftonline.com/",
  "TenantId": "msidentitysamplestesting.onmicrosoft.com",
  "ClientId": "6af093f3-b445-4b7a-beae-046864468ad6",
  "ClientCredentials": [
    {
      "SourceType": "KeyVault",
      "KeyVaultUrl": "https://webappsapistests.vault.azure.net",
      "KeyVaultCertificateName": "Self-Signed-5-5-22"
    }
  ]

}

This is the configuration. If you don't have access to the test account, let us know.

  1. Click on appsettings.json. Go to "Properties" which is on the bottom right pannel in VS. Under "Advanced" make sure "Copy to Output Direct" is on "Copy always", it's probably on "Do not copy".
  2. Right click again on the project. Set "Set as start-up project". Hit "Start" and run the app. Should work.

MSAL to Id Web for daemon scenarios

Here you can see a PR which shows the diff between MSAL and the higher-level APIs with Id Web. The MSAL code has almost the same amount of code as the ADAL counterpart.