In this article
Install Node.
Have your favorite text editor open and ready. We'll be using Visual Studio 2019.
Ideally, install Visual Studio 2019, checking ASP.NET and web development, and .NET Core cross-platform development, or otherwise, install ASP.NET Core 3.1 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) calling a Web API. The Web API is the MSAL Node JS Web API. We will run both at the same time, in order to see the middleware library (Passport.js) validating the token. This will show the content learned early in the OAuth2.0 & OIDC sections.
-
git clone https://github.com/Azure-Samples/active-directory-b2c-javascript-nodejs-webapi.git
-
cd active-directory-b2c-javascript-nodejs-webapi
-
Install Node.Js if you haven't already
-
Install and update the Node dependencies
npm install && npm update
-
Run the Web API. By default, it runs on
http:localhost:5000
npm start
-
git clone https://github.com/Azure-Samples/active-directory-b2c-javascript-angular-spa.git
-
cd active-directory-b2c-javascript-angular-spa
-
Install and update the Node dependencies
npm install && npm update
-
Open
src/app/app-config.ts
-
Edit Line 49:
replace:
webApi: "https://fabrikamb2chello.azurewebsites.net/hello"
with:
webApi: "http://localhost:5000/hello"
-
Hit Save
-
Run the Web app.
npm start
Listening on port 6420...
Visit http://localhost:6420
and perform the following actions:
- Click the Login button to start the Azure AD B2C sign in or sign up workflow.
- Once signed in, you can click the Call Web API button to have your display name returned from the Web API call as a JSON object.
- Click "Logout" to logout from the application.
In this lab, we'll need to use the Microsoft.Identity.Web project templates (which are also available in .NET 5.0 from Preview 7). To install them in your system:
- Download the Microsoft.Identity.Web.ProjectTemplates-1.1.0 NuGet package from NuGet.org, and save it locally
- In a developer command prompt, type
dotnet new -i Microsoft.Identity.Web.ProjectTemplates.1.1.0.nupkg
This installs the templates, and displays which project templates are available. (You can also type dotnet new --help
to see the list)
In a developer command prompt:
-
Create a folder, and cd into it.
mkdir webapp cd webapp
-
Create the B2C web app
dotnet new webapp2 --auth IndividualB2C ^ --aad-b2c-instance "https://fabrikamb2c.b2clogin.com" ^ --domain "fabrikamb2c.onmicrosoft.com" ^ --client-id "fdb91ff5-5ce6-41f3-bdbd-8267c817015d"
-
Edit the
Properties\launchsettings.json
file so that in the IISExpress configuration, you use "sslPort": 44365, as this is one of the ports for which the redirect URIs are registered in the B2C application (5001 one is another one, and it's used by the other ASP.NET Core profile described in launchsettings.json file){ "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:48464", "sslPort": 44365 } }, // More here
-
Run your application (either from Visual Studio or from the command line with
dotnet run
) -
Sign-in with one of your social identities or sign-up for a new local account.
That's what it took to create a B2C application that signs-in users. Note that you don't need any client secret until your web app calls a web API.
- In ASP.NET Core, web apps have several flavors, MVC, Razor, and Blazor Web server. You have created above a Razor assembly by using the webapp2 template. You can also try the mvc2 one.