Skip to content

moroboro/AppModelv2-WebApp-OpenIDConnect-DotNet

 
 

Repository files navigation

page_type languages products name urlFragment description
sample
csharp
aspnet
azure-active-directory
Sign in a user in an ASP.NET Web App with OpenID Connect and the Microsoft identity platform
AppModelv2-WebApp-OpenIDConnect-DotNet
This sample demonstrates a ASP.NET Web App application that authenticates users against Azure AD

Sign in a user in an ASP.NET Web App with OpenID Connect and the Microsoft identity platform

  1. Overview
  2. Scenario
  3. Contents
  4. Prerequisites
  5. Setup
  6. Registration
  7. Running the sample
  8. Explore the sample
  9. About the code
  10. More information
  11. Community Help and Support
  12. Code of Conduct

Overview

This sample demonstrates a ASP.NET web app application that authenticates users against Azure AD.

Scenario

  1. The client ASP.NET web app application uses the Microsoft Authentication Library (MSAL) to obtain an ID Token from Azure AD:
  2. The ID Token proves that the user has successfully authenticated against Azure AD.

Prerequisites

Setup

Step 1: Clone or download this repository

From your shell or command line:

git clone https://github.com/AzureADQuickStarts/AppModelv2-WebApp-OpenIDConnect-DotNet.git
cd AppModelv2-WebApp-OpenIDConnect-DotNet

or download and extract the repository .zip file.

⚠️ To avoid path length limitations on Windows, we recommend cloning into a directory near the root of your drive.

Register the sample application(s) with your Azure Active Directory tenant

There is one project in this sample. To register it, you can:

  • follow the steps below to manually register your apps
  • or use PowerShell scripts that:
    • automatically create the Azure AD applications and related objects (passwords, permissions, dependencies) for you.
    • modify the projects' configuration files.
Expand this section if you want to use this automation:

⚠️ If you have never used Azure AD Powershell before, we recommend you go through the App Creation Scripts once to ensure that your environment is prepared correctly for this step.

  1. On Windows, run PowerShell as Administrator and navigate to the root of the cloned directory

  2. If you have never used Azure AD Powershell before, we recommend you go through the App Creation Scripts once to ensure that your environment is prepared correctly for this step.

  3. In PowerShell run:

    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
  4. Run the script to create your Azure AD application and configure the code of the sample application accordingly.

  5. In PowerShell run:

    cd .\AppCreationScripts\
    .\Configure.ps1

    Other ways of running the scripts are described in App Creation Scripts The scripts also provide a guide to automated application registration, configuration and removal which can help in your CI/CD scenarios.

Choose the Azure AD tenant where you want to create your applications

As a first step you'll need to:

  1. Sign in to the Azure portal.
  2. If your account is present in more than one Azure AD tenant, select your profile at the top right corner in the menu on top of the page, and then switch directory to change your portal session to the desired Azure AD tenant.

Register the Web app (Quickstart-AspNetWebApp)

  1. Navigate to the Azure portal and select the Azure AD service.
  2. Select the App Registrations blade on the left, then select New registration.
  3. In the Register an application page that appears, enter your application's registration information:
    • In the Name section, enter a meaningful application name that will be displayed to users of the app, for example Quickstart-AspNetWebApp.
    • Under Supported account types, select Accounts in this organizational directory only.
    • In the Redirect URI (optional) section, select Web in the combo-box and enter the following redirect URI: https://localhost:44368/.

      Note that there are more than one redirect URIs used in this sample. You'll need to add them from the Authentication tab later after the app has been created successfully.

  4. Select Register to create the application.
  5. In the app's registration screen, find and note the Application (client) ID. You use this value in your app's configuration file(s) later in your code.
  6. In the app's registration screen, select Authentication in the menu.
    • If you don't have a platform added, select Add a platform and select the Web option.
    • In the Redirect URIs section, enter the following redirect URIs.
      • https://localhost:44368/signin-oidc
    • In Implicit grant section, select the check box for ID tokens.
    • In the Logout URL section, set it to https://localhost:44368/signout-oidc.
  7. Select Save to save your changes.

Configure the Web app (Quickstart-AspNetWebApp) to use your app registration

Open the project in your IDE (like Visual Studio or Visual Studio Code) to configure the code.

In the steps below, "ClientID" is the same as "Application ID" or "AppId".

  1. Open the AppModelv2-WebApp-OpenIDConnect-DotNet\Web.config file.
  2. Find the key ClientId and replace the existing value with the application ID (clientId) of the Quickstart-AspNetWebApp application copied from the Azure portal.
  3. Find the key Tenant and replace the existing value with your Azure AD tenant ID.

Running the sample

For Visual Studio Users

Clean the solution, rebuild the solution, and run it.

Explore the sample

  1. Open your browser and navigate to https://localhost:44368.

  2. Select the Sign in button on the top right corner. When the user signs-in for the first time , a consent screen is presented with required permissions, select Accept.

    Click on See Your Claims link, you will see claims from the signed-in user's token.

UserClaims

ℹ️ Did the sample not work for you as expected? Then please reach out to us using the GitHub Issues page.

We'd love your feedback!

Were we successful in addressing your learning objective? Do consider taking a moment to share your experience with us.

About the code

  1. In Startup.cs, Configuration method configures OWIN to use OpenIdConnect as below:

     public void Configuration(IAppBuilder app)
      {
            ...
            app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions
            {
                ClientId = clientId,
                Authority = authority,
                RedirectUri = redirectUri,
                PostLogoutRedirectUri = redirectUri,
                Scope = OpenIdConnectScope.OpenIdProfile,
                ResponseType = OpenIdConnectResponseType.CodeIdToken,
                Notifications = new OpenIdConnectAuthenticationNotifications
                {
                    AuthenticationFailed = OnAuthenticationFailed
                }
            }
        );
      }
  2. HomeController.cs contains SignIn and SignOut methods as following:

    public class HomeController : Controller
    {
        ...
        public void SignIn()
        {
            if (!Request.IsAuthenticated)
            {
                HttpContext.GetOwinContext().Authentication.Challenge(
                    new AuthenticationProperties { RedirectUri = "/" },
                    OpenIdConnectAuthenticationDefaults.AuthenticationType);
            }
        }
        public void SignOut()
        {
            HttpContext.GetOwinContext().Authentication.SignOut(
                    OpenIdConnectAuthenticationDefaults.AuthenticationType,
                    CookieAuthenticationDefaults.AuthenticationType);
        }
    }

More information

For more information about how OAuth 2.0 protocols work in this scenario and other scenarios, see Authentication Scenarios for Azure AD.

Community Help and Support

Use Stack Overflow to get support from the community. Ask your questions on Stack Overflow first and browse existing issues to see if someone has asked your question before. Make sure that your questions or comments are tagged with [azure-active-directory azure-ad-b2c ms-identity adal msal].

If you find a bug in the sample, raise the issue on GitHub Issues.

To provide feedback on or suggest features for Azure Active Directory, visit User Voice page.

Code of Conduct

This project has adopted the Microsoft Open Source Code of Conduct. For more information, see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

About

ASP.NET Web Server - OpenID Connect v2 Endpoint Sample

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 95.9%
  • C# 1.7%
  • PowerShell 1.6%
  • Other 0.8%