Click here to Skip to main content
15,881,424 members
Articles / Hosted Services / Azure

Azure AD with ASP.NET Core 2.0

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
19 May 2018CPOL3 min read 7.7K   4   1
How to implement Azure AD authentication in ASP.NET Core 2.0.Continue reading

Problem

How to implement Azure AD authentication in ASP.NET Core 2.0.

Solution

In previous posts, we have seen how to secure our applications using:

In this and the next post, I’ll demonstrate how to use Azure AD to delegate identity and access management to Azure, simplifying our application.

Configure Azure AD

Create Azure subscription (start for free, gives you credit to play).

Create new resource and search for ‘azure active directory’:

Image 1

Create new instance:

Image 2

Enter details, including the unique ‘initial domain name’, this will become the Tenant Name that will be used in your application, e.g., below the tenant name will be fiverad.onmicrosoft.com:

Image 3

Once directory is created, we need to register our application:

Image 4

Enter details for your application, including the URL to your application home/sign-in page:

Image 5

Once the application is created, you’ll see ‘Application ID’ in the list. This will be used in our application later as ‘Client ID’:

Image 6

Click on application and ‘Reply URLs, enter URL where Azure AD will redirect user after authentication (https://localhost:44308/security/signin-callback):

Image 7

Next, we’ll create a user in Azure AD:

Image 8

Image 9

Note that username is based on our directory’s domain name, i.e., @fiverad.onmicrosoft.com also makes a note of the temporary password, you’ll be required to change it on first login:

Image 10 Image 11

So far, you have:

  1. Created Azure AD
  2. Registered your application
  3. Added a user

Next, we’ll setup our application and use Azure AD to authenticate users.

Configure Application

Create an empty project and update Startup to configure services and middleware for MVC and Authentication:

C#
public class Startup
    {
        private readonly string TenantName = ""; // aka 'Initial Domain Name' in Azure
        private readonly string ClientId = ""; // aka 'Application Id' in Azure

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = 
                   CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultSignInScheme = 
                   CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = 
                   OpenIdConnectDefaults.AuthenticationScheme;
            }).AddOpenIdConnect(options =>
            {
                options.Authority = "https://login.microsoftonline.com/" + 
                                        this.TenantName;
                options.ClientId = this.ClientId;
                options.ResponseType = OpenIdConnectResponseType.IdToken;
                options.CallbackPath = "/security/signin-callback";
                options.SignedOutRedirectUri = "https://localhost:44308/";
            }).AddCookie();
            
            services.AddMvc();
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseAuthentication();
            app.UseMvcWithDefaultRoute();
        }
    }

Here, we are setting up Open ID Connect authentication middleware services with:

  • Authority: path to the Azure AD tenant that we’ve setup as authentication server
  • ClientId: application identifier that Azure AD provides for our application
  • ResponseType: value that determines authorization flow used and parameters returned from server. We are interested only in authorization token for now.
  • CallbackPath: path where server will redirect after authentication. We don’t need to create this in our application, the middleware will handle this.
  • SignedOutRedirectUri: path where server will redirect after signing out. This is the path to our application home page, for instance.

Also, when configuring authentication middleware, we’ve specified Open ID Connect as challenge scheme. This is the middleware ASP.NET Core will use for users who have not been signed in.

Once signed in, we want to store their identity in a cookie, instead of redirecting to authentication server for every request. For this reason, we’ve added cookie authentication middleware and are using that as default sign-in scheme. If the cookie is missing, users will be ‘challenged’ for their identity.

Add a controller to implement login/logout actions:

C#
public class SecurityController : Controller
    {
        public IActionResult Login()
        {
            return Challenge(new AuthenticationProperties { RedirectUri = "/" });
        }

        [HttpPost]
        public async Task Logout()
        {
            await HttpContext.SignOutAsync(
              CookieAuthenticationDefaults.AuthenticationScheme);
            await HttpContext.SignOutAsync(
              OpenIdConnectDefaults.AuthenticationScheme);
        }
    }

When logging out, it is important to sign-out from authentication server (Azure AD) and also remove the cookie by signing out from cookie authentication scheme.

Secure controllers (or their actions) using [Authorize] attribute:

C#
[Authorize]
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
    }

Run the application and you’ll be redirected to Azure for authentication.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
QuestionHow do you get roles from claims Pin
hestol15-Aug-18 20:58
hestol15-Aug-18 20:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.