Quickstart: Sign in users in a single-page app (SPA) and call the Microsoft Graph API
Applies to: Workforce tenants
External tenants (learn more)
In this quickstart, you use a sample single-page app (SPA) to show you how to sign in users by using the authorization code flow with Proof Key for Code Exchange (PKCE) and call the Microsoft Graph API. The sample uses the Microsoft Authentication Library to handle authentication.
- An Azure account with an active subscription. If you don't already have one, Create an account for free.
- This Azure account must have permissions to manage applications. Any of the following Microsoft Entra roles include the required permissions:
- Application Administrator
- Application Developer
- Cloud Application Administrator
- A workforce tenant. You can use your Default Directory or set up a new tenant.
- Visual Studio Code or another code editor.
- Register a new app in the Microsoft Entra admin center with the following configuration and record its identifiers from the app Overview page and record its identifiers from the app Overview page. For more information, see Register an application.
- Name: identity-client-spa
- Supported account types: Accounts in this organizational directory only (Single tenant)
- Platform configuration: Single-page application (SPA)
- Redirect URI:
http://localhost:5000/authentication/login-callback.
- .NET SDK
To obtain the sample application, you can either clone it from GitHub or download it as a .zip file.
To clone the sample, open a command prompt and navigate to where you wish to create the project, and enter the following command:
git clone https://github.com/Azure-Samples/ms-identity-docs-code-dotnet.git
Download the .zip file. Extract it to a file path where the length of the name is fewer than 260 characters.
In your IDE, open the project folder, ms-identity-docs-code-dotnet/spa-blazor-wasm, containing the sample.
Open spa-blazor-wasm/wwwroot/appsettings.json and update the following values with the information recorded earlier in the admin center.
{ "AzureAd": { "Authority": "https://login.microsoftonline.com/<Enter the tenant ID obtained from the Microsoft Entra admin center>", "ClientId": "Enter the client ID obtained from the Microsoft Entra admin center", "ValidateAuthority": true } }
Authority
- The authority is a URL that indicates a directory that MSAL can request tokens from. Replace Enter_the_Tenant_Info_Here with the Directory (tenant) ID value that was recorded earlier.ClientId
- The identifier of the application, also referred to as the client. Replace the text in quotes with the Application (client) ID value that was recorded earlier.
Run the project with a web server by using dotnet:
To start the server, run the following commands from within the project directory:
cd spa-blazor-wasm dotnet workload install wasm-tools dotnet run
Copy the
http
URL that appears in the terminal, for example,http://localhost:5000
, and paste it into a browser. We recommend using a private or incognito browser session.Follow the steps and enter the necessary details to sign in with your Microsoft account. You'll be requested an email address so a one time passcode can be sent to you. Enter the code when prompted.
The application will request permission to maintain access to data you have given it access to, and to sign you in and read your profile. Select Accept. The following screenshot appears, indicating that you have signed in to the application and have accessed your profile details from the Microsoft Graph API.
- An Azure account with an active subscription. If you don't already have one, Create an account for free.
- This Azure account must have permissions to manage applications. Any of the following Microsoft Entra roles include the required permissions:
- Application Administrator
- Application Developer
- Cloud Application Administrator
- An external tenant. To create one, choose from the following methods:
- Use the Microsoft Entra External ID extension to set up an external tenant directly in Visual Studio Code. (Recommended)
- Create a new external tenant in the Microsoft Entra admin center.
- A user flow. For more information, refer to create self-service sign-up user flows for apps in external tenants. This user flow can be used for multiple applications.
- Visual Studio Code or another code editor.
- Register a new app in the Microsoft Entra admin center with the following configuration and record its identifiers from the app Overview page. For more information, see Register an application.
- Name: identity-client-spa
- Supported account types: Accounts in this organizational directory only (Single tenant)
- Platform configuration: Single-page application (SPA)
- Redirect URI:
http://localhost:3000/
- Add your application to the user flow
- Node.js
Once you register your application, it gets assigned the User.Read permission. However, since the tenant is an external tenant, the customer users themselves can't consent to this permission. You as the tenant administrator must consent to this permission on behalf of all the users in the tenant:
From the App registrations page, select the application that you created (such as ciam-client-app) to open its Overview page.
Under Manage, select API permissions.
- Select Grant admin consent for <your tenant name>, then select Yes.
- Select Refresh, then verify that Granted for <your tenant name> appears under Status for the permission.
To obtain the sample application, you can either clone it from GitHub or download it as a .zip file.
To clone the sample, open a command prompt and navigate to where you wish to create the project, and enter the following command:
git clone https://github.com/Azure-Samples/ms-identity-ciam-javascript-tutorial.git
Download the sample. Extract it to a file path where the length of the name is fewer than 260 characters.
Open
App/public/authConfig.js
and replace the following with the values obtained from the Microsoft Entra admin center:Enter_the_Application_Id_Here
and replace it with the Application (client) ID of the app you registered earlier.Enter_the_Tenant_Subdomain_Here
and replace it with the Directory (tenant) subdomain. For example, if your tenant primary domain iscontoso.onmicrosoft.com
, usecontoso
. If you don't have your tenant name, learn how to read your tenant details.
Save the file.
To start the server, run the following commands from within the project directory:
cd 1-Authentication\0-sign-in-vanillajs\App npm install npm start
Copy the
https
URL that appears in the terminal, for example,https://localhost:3000
, and paste it into a browser. We recommend using a private or incognito browser session.Sign-in with an account registered to the tenant.
The following screenshot appears, indicating that you have signed in to the application and have accessed your profile details from the Microsoft Graph API.
- Find the Sign out button on the page, and select it.
- You'll be prompted to pick an account to sign out from. Select the account you used to sign in.
A message appears indicating that you have signed out. You can now close the browser window.