In this article
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
A Single-Page Application (SPA) signing-in with B2C.
-
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
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.
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.
- 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.
- In Program.cs, delete all the code but leave the method signature
static int Main(string[] args)
- Change main to
static async Task Main(string[] args)
- 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).
- 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}"); }
- Go to the App.config. Comment out all the lines under
<appSettings>
- 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.
- 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".
- Right click again on the project. Set "Set as start-up project". Hit "Start" and run the app. Should work.
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.