This sample application is built on ASP.NET Core 2.1 to test authentication via Azure AD.
You will need a development environment capable of running an ASP.NET Core 2.1 application.
Windows users can install Visual Studio 2017 with the ASP.NET and web development workload.
Users on Windows, Mac, or Linux can download the .NET Core SDK and use any editor that works best. Visual Studio Code is pretty good.
To run the app locally, you'll need to register an application in Azure AD.
How to register the app:
- Go to https://portal.azure.com
- Find Azure Active Directory on the left or from under All services
- Go to App registrations
- Click on New application registration
- Give the app a name, e.g. ASP.NET Core 2 Azure AD Test
- Make sure the application type is Web app/API
- Set sign-on URL to http://localhost:5000/Account/SignIn
- Click Create
Getting client id, setting reply URL, and generating client secret:
- After creation, open the app
- Copy the Application ID, and put it somewhere, this is also called the Client ID
- Click Settings and then Reply URLs
- Add https://localhost:5000/signin-oidc to the list and save it
- Go to Keys
- In the Passwords section, put some description for the key, select the expiry, and hit Save
- Copy the key value somewhere, this is your client secret (keep it secret)
Adding permissions for Microsoft Graph API:
- Find your app in the Azure AD blade's App Registrations tab in Azure Portal
- Go to Required permissions
- Click Add
- Choose Microsoft Graph as the API
- Select Sign in and read user profile, View users' basic profile, and View users' email address under Delegated permissions
- Click Select and Done
Getting the authority URL:
- Go back to the App registrations list
- Click Endpoints
- Copy the OAuth 2.0 Authorization Endpoint value
- Remove the /oauth2/authorize part from the URL, the result is your Authority
Fill the values in settings:
- Open the solution in Visual Studio
- Set client id and authority in appsettings.json
- Right-click on the project and click Manage user secrets
- Add the client secret here. Example below:
{
"Authentication":{
"ClientSecret": "your-client-secret....."
}
}
The main reason to put the client secret there is to make sure it is not accidentally put into version control. This is not absolute advice and you must make the decision how to store configurations for your app.