In this post, I am going to explain to you about how we can integrate ASP.NET Core and Azure Active Directory. In the first scenario if you are new to Azure Active directory than please read Basics of Authentication in Azure AD from here .This article explains how to manually configure Azure Active Directory with advanced settings so let's start.
Configure Azure Active Directory: First step is to configure Azure ACTIVE directory, so first we need to configure applications in Azure Active Directory, so the simple steps are to login in your azure portal http://manage.windowsazure.com/ , all I am using is the new Azure portal so all you need to do is click on Browse and click on Active Directory,

Now click on Application Tab and then register a new application , then select the options “Add an application my organization is developing” once you will do that following screen will appear.

Select the options “Web Applications” and click on next step, it will ask us two new things as mentioned in the following image.

let’s talk about those in more details:
- SIGN-ON-URL:
This is called login URL of your applications , When a user “signs in” to an application, they go through an authentication process where they are required to prove that who they are. Here you can use localhost and once moved to production you can change it. If your applications is running at localhost: 5385 than you can use localhost: 5385 here but such value gives you trouble in advanced stages so the best suggestion is to use the URL of your applications. - APP ID URI:
This must be a unique URL, here mostly what we do is use a URL with our tenant Azure (*.onmicrososoft.com) along with the name of applications. (eg. http://dutechnosys.onmicrosoft.com/DogDemo).

“REPLY URL” is where Azure Active directory is redirected after the loginprocess, you can use local host here and can change it any time.The Reply URL is the location to which Azure AD will send the authentication response, including a token if authentication was successful. In the case of a native application, the Redirect URI is a unique identifier to which Azure AD will redirect the user-agent in an OAuth 2.0 request. All I am using here is http://localhost:59917/Home/Contact Now copy the "CLIENT ID" displayed on the same screen configuration (it is a guid) since you'll need to configure the application:

After all the above steps we are done with ADD and now the next step is to use the ASP.NET core set applications to use Azure active directory as authenticationprovider.
Step Two Configure ASP.NET applications core: You just need to create a new application using asp.net as mentioned in the below image.

Visual Studio 2015: In Solution Explorer, right click on your existing project and select the Configure Azure AD Authentication option. Once you do that it will open your Existing azure active directory you can check from copied client ID. By the way, from here you can also add your applications into Azure Active Directory.

Once you complete the steps it will verify your account and after successful verification it will replace your existing AccountController.cs ,Startup.Auth.Cs, _LoginPartial.cshtml,
If you want to see important changes than those are available in Startup.Auth.Cs file,
- privatestaticstringclientId = ConfigurationManager.AppSettings["ida:ClientId"];
- privatestaticstringaadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];
- privatestaticstringtenantId = ConfigurationManager.AppSettings["ida:TenantId"];
- privatestaticstringpostLogoutRedirectUri = ConfigurationManager.AppSettings["ida:PostLogoutRedirectUri"];
- privatestaticstring authority = aadInstance + tenantId;
- publicvoidConfigureAuth(IAppBuilder app)
- {
- app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
- app.UseCookieAuthentication(newCookieAuthenticationOptions());
- app.UseOpenIdConnectAuthentication(
- newOpenIdConnectAuthenticationOptions
- {
- ClientId = clientId,
- Authority = authority,
- PostLogoutRedirectUri = postLogoutRedirectUri
- });
- }

Now it’s time to run applications, once your build is successful it will ask you to add SSL error, add that in browser and it will show yo the following image

Click on Accept and you are ready to move on.
Conclusion:
In this post I talked about the developer experience of building Web Applications and Web API applications that are protected by Azure AD. A great article by Mr. Tom Archer will explain details about Getting Started with Azure Active Directory and Visual Studio connected services (MVC Projects).

sanjayanthanPosted Jun 1, 2016, 6:10 AM
hi can you show the sample code
Vivek KumarPosted Apr 28, 2016, 3:12 PM
Good one
Bhavik PatelPosted Apr 27, 2016, 9:23 PM
good article
Humayun Kabir MamunPosted Apr 11, 2016, 3:49 AM
Nice...
Sourabh SomaniPosted Apr 10, 2016, 1:43 AM
Good one sir :)
Debasis SahaPosted Apr 9, 2016, 2:03 AM
Good share..
Rahul Kumar SaxenaPosted Apr 8, 2016, 10:31 PM
Good show
Vignesh ManiPosted Apr 8, 2016, 5:16 PM
good
Mohammed IbrahimPosted Apr 8, 2016, 2:20 PM
nice