This Project uses Angular 2 Js Client and .Net Web API Server Authenticated with Azure AD
- Visual Studio 2015
- An Azure subscription(free is fine)
- Create Azure AD from your azure subscriptions.
- Node Js installed
- ng-cli installed in your system globally.
From your Visual studio or command line: git clone https://github.com/karthikeyanVK/ng2AzureAD.git
-
Sign in to the Azure portal.
-
On the top bar, click on your account and under the Directory list, choose the Active Directory tenant where you wish to register your application.
-
Click on More Services in the left hand nav, and choose Azure Active Directory.
-
Name the application, eg:. 'SampleWebAPI' and select 'Web Application and/or Web API' as the Application Type. For the sign-on URL, as
https://localhost:25476
. -
Click on Create to create the application.
-
Navigate to the SampleWebAPI and then change APP ID URI, enter https://<your_tenant_name>/TodoListService, replacing <your_tenant_name> with the name of your Azure AD tenant. We will use https://contoso.onmicrosoft.com/TodoListService
-
Save the Changes
-
Enter a name for the application, eg:. 'AngularWebAPP' and select 'Web Application and/or Web API' as the Application Type. For the sign-on URL, enter the base URL for the sample, which is by default
https://localhost:4200
. -
Click on Create to create the application.
-
Save the Changes
-
While still in the Azure portal, choose your application, click on Settings and choose Properties.
-
Find the Application ID value and copy it to the clipboard.
-
Configure Permissions for your application - in the Settings menu, choose the 'Required permissions' section, click on Add, then Select an API, and type "SampleWebAPI" in the text box. Then, click on Select Permissions and select 'Access SampleWebAPI'
-
Grant permissions across your tenant for your application. Go to Settings -> Properties -> Required Permissions, and click on the Grant Permissions button in the top bar. Click Yes to confirm.
IF GRANT PERMISSION NOT WORKING IN NEW PORTAL
Searching and selecting the other application in new portal (eg:-SampleWebAPI)is fuzzy and slow, Alternatively you can use old portal to give permission find the application in the directory -> Click on Add Application -> Search and then add delegated permissions as below.
- While still in the Azure portal, In your application page, click on Manifest to open the inline manifest editor.
- Change
oauth2AllowImplicitFlow
property totrue
as below, by default it will be false. - Save the changes
- Open the
ng2-ADWebAPI
application in Visual Studio 2015 - Rename the
Web.template.config
asWeb.config
- Change the app key of
Tenant
with AAD Tenant name in our casecontoso.onmicrosoft.com
- Change the app key of
Audience
with Tenant name in our case 'https://contoso.onmicrosoft.com/TodoListService'
- Navigate to
ng2-ADAuth
application in command prompt or git bash and runnpm install
- npm install will install the angular based ad js library known as ng2-adal and angular2-adal
- Rename
ng2-ADAuth/src/app/common/authentication/auth-config.service.ts.template
tong2-ADAuth/src/app/common/authentication/auth-config.service.ts
- Make sure you add endpoints, tenant, clientId as example provided in
auth-config.service.ts
- Switch to Visual Studio and go to the AngularWebAPP project. Right click on the project in the Solution Explorer and select Publish. Click Import, and import the AngularWebAPP publish profile you downloaded.
- On the Connection tab, update the Destination URL to https**(only https is supported for external url's)** , for example https://AngularWebAPP-contoso.azurewebsites.net. Click Next.
- On the Settings tab, make sure Enable Organizational Authentication is NOT selected. Click Publish.
- Visual Studio will publish the project and automatically open a browser to the URL of the project. If you see the default web page of the project, the publication was successful.
- Sign in to the Azure portal.
- On the top bar, click on your account and under the Directory list, choose your Active Directory tenant.
- Click on More Services in the left hand nav, and choose Azure Active Directory.
- Click on App registrations and select the To Do SPA application.
- Go to Settings --> Properties, and update the Sign-On URL to the address of your SPA, for example https://TodoListService-contoso.azurewebsites.net.
- Go to Settings --> Reply URLs and update the Reply URL field to the address of your SPA, for example https://TodoListService-contoso.azurewebsites.net.
- When accessing web API Use the AuthHttpModule instead of angular http module, which will take care of adding the bearer token into the request that goes from the web application( refer
ng2-ADAuth/src/app/home/home.component.ts
) authenticatorGuard.guard
is used for validating the AD routing in angular.- Import
import { AdalService } from "ng2-adal/services/adal.service";
,import { AuthConfigService } from './common/authentication/auth-config.service'
,import { AuthenticatorGuard } from './common/authentication/authenticatorGuard.guard';
intoapp.module.ts
- We use
ng2-Adal
for the AD connectity functionality in angular app. - Install below nugget packages to Web API Project
PM> Install-Package Microsoft.Owin.Security.ActiveDirectory
PM> Install-Package Microsoft.Owin.Host.SystemWeb
- Add Owin Startup Class as below to App_Start as in
ng2AzureAD/ng2-ADWebAPI/ng2-ADWebAPI/App_Start/Startup.cs
- Add [Authorize] attribute to ValuesController on class level.
- Enable Cors
config.EnableCors(cors)
as inng2AzureAD/ng2-ADWebAPI/ng2-ADWebAPI/App_Start/WebApiConfig.cs