services | platforms | author | client | service | endpoint |
---|---|---|---|---|---|
active-directory |
service-fabric |
amigup |
angular5 |
ASP.NET Core 2.0 Web API |
AAD V2 |
This sample demonstrates the use of MSAL for JavaScript for securing an Angular based single page app, implemented with an ASP.NET Web API backend hosted on Service Fabric.
You expose a Web API and you want to protect it so that only authenticated user can access it. You want to enable authenticated users with both work and school accounts or Microsoft personal accounts (formerly live account) to use your Web API.
This sample presents a Web API running on ASP.NET Core 2.0 using Azure service fabric, protected by Azure AD JWT Bearer Authentication. The Web API is exercised by a angluar5 client application. The .Net application uses the Active Directory Authentication Library MSAL.NET to obtain a JWT access token through the OAuth 2.0 protocol. The access token is sent to the ASP.NET Core Web API, which authenticates the user using the ASP.NET JWT Bearer Authentication middleware.
Check this link to read about service fabric with .Net Core sample
Check this link to read about msal-angular
Getting started is simple! To run this sample you will need:
- Visual Studio 2017
- Service Fabric
- An Internet connection
- An Azure Active Directory (Azure AD) tenant. For more information on how to get an Azure AD tenant, see How to get an Azure AD tenant
- A user account in your Azure AD tenant, or a Microsoft personal account
From your shell or command line:
git clone https://github.com/amigup/servicefabric-dotnetcore-webapi-angular5-msal.git
There are two projects in this sample Web API and angular appliations. Either two different appliations can be registered at Azure AD for Web API and angular applications or one application to have both Web and Web API platforms. Following steps registers only one application. To register these projects, you can:
Sign in to application registration portal. From there, you can add converged applications.
- In the application registration portal, click Add an app
- In the Register your application page, provide a name for your application for instance like
servicefabric-dotnetcore-webapi-angular5-msal
- Press the Create button
- In the registration page for your application, copy the application ID to the clipboard you will need it to configure the code for your application
- Press the Save button at the bottom of the page.
- In the Platforms section, click on the Add Platform button and then on Web
- Click on the My applications link at the top of the page to get back to the list of applications in the app registration portal
- In the application registration portal, open the registered applcation (created earlier)
- In the Platforms section, click on the Add Platform button and then on Web API
- Copy the scope proposed by default to access your web api as a user. It's in the form
api://<Application ID>/access_as_user
- Press the Save button at the bottom of the page.
- Click on Edit Application Manifest and set the
oauth2AllowImplicitFlow
to true. - Press the Save button at the bottom of the page.
Step 2.1: Register the application with your Azure Active Directory tenant to enable Swagger on WebApi project
Sign in to application registration portal. From there, you can add converged applications.
- In the application registration portal, click Add an app
- In the Register your application page, provide a name for your application for instance like
swagger-servicefabric-dotnetcore-webapi-angular5-msal
- Press the Create button
- In the registration page for your application, copy the application ID to the clipboard you will need it to configure the code for your application
- Press the Save button at the bottom of the page.
- Open the registed application created in Step#2.
- In the Web API platform, in the Pre-authorized applications section click on Add application
- In the application ID field, paste the application ID of the
swagger-servicefabric-dotnetcore-webapi-angular5-msal
application as pasted from the registration - In the Scope field, click on the Select combo box and select the scope for this Web API
api://<Application ID>/access_as_user
- Press the Save button at the bottom of the page.
By default the sample is configured to enable users to sign in with any work and school accounts (AAD) or Microsoft Personal accounts (formerly live account).
common
is not a proper tenant. It's just a convention to express that the accepted tenants are any Work and School organizations, or Personal Microsoft account (consumer accounts).
Accepted tenants can have the following values:
Value | Meaning |
---|---|
common |
users can sign in with any Work and School account, or Microsoft Personal account |
organizations |
users can sign in with any Work and School account |
consumers |
users can sign in with a Microsoft Personal account |
a GUID or domain name | users can only sign in with an account for a specific organization described by its tenant ID (GUID) or domain name |
- Open the solution in Visual Studio.
- In the SampleUserService project, open the
appsettings.json
file. - Find the
ClientId
property and replace the value with the Application ID (Client ID) property of the regiseterd application, that you registered earlier. - [Optional] if you want to limit sign-in to users in your organization, also update the following
The
Domain
property, replacing the existing value with your AAD tenant domain, for example, contoso.onmicrosoft.com. TheTenantId
property replacing the existing value with the Tenant ID. - You can use a self-signed certificate on your local machine and test clusters but you want to make sure to purchase a CA-signed one for your production clusters. Find the
HttpsCertificateThumbprint
property and replace with obtatined certificate thumbprint .The Service Fabric SDK provides the CertSetup.ps1 script, which creates a self-signed certificate and imports it into the Cert:\LocalMachine\My certificate store. Open a command prompt as administrator and run the following command to create a cert with the subject "CN=localhost":
PS C:\program files\microsoft sdks\service fabric\clustersetup\secure> .\CertSetup.ps1 -Install -CertSubjectName CN=localhost
If you already have a certificate PFX file, run the following to import the certificate into the Cert:\LocalMachine\My
certificate store:
PS C:\mycertificates> Import-PfxCertificate -FilePath .\mysslcertificate.pfx -CertStoreLocation Cert:\LocalMachine\My -Password (ConvertTo-SecureString "!Passw0rd321" -AsPlainText -Force)
PSParentPath: Microsoft.PowerShell.Security\Certificate::LocalMachine\My
Thumbprint Subject
---------- -------
3B138D84C077C292579BA35E4410634E164075CD CN=zwin7fh14scd.westus.cloudapp.azure.com
- In the SampleUserService project, open the
appsettings.json
file. - Find the
Swagger:ClientId
property and replace the value with the Application ID (Client ID) property of the regiseterd swagger application, that you registered earlier. - Find the
Swagger:ClientSecret
property and replace with the value with the generated secret. - Find the
Swagger:Relam
property and replace withhttps://<<application server address>>/swagger/ui/o2c-html
. - Find the
Swagger:AppName
property and replace with the value name used while registering swagger application. - Find the
Swagger:Audience
property and replace with appliationapi://<<Client Id of Web API application>>
.
- In the MSALAngularDemoApp project, open
environment.ts
. - Find the app key
clientId
and replace the value with the ApplicationID (Client ID) for the registered app copied from the app registration page. - Find the app key
scope
and replace the value with the scope of the registered application copied from the app registration (of the formapi://<Application ID of service>/access_as_user
)
Clean the solution, rebuild the solution, and run it. .
- Once the Web API is started, Navigate to
https://localhost/swagger/index.html
. - Click on Authorize.
- Enter your credentials.
- Click on Try-out on API.
- Click on Execute
- Run
npm install
to intall all dependent packages - Run
ng serve
for a dev server. Navigate tohttp://localhost:4200/
. The app will automatically reload if you change any of the source files. - On launch, application will redirect to
http://localhost:4200/userProfile
after successful authentication and fetches the user name using graph API and user claims from the Web API. You can run the network monitoring tool likeBrowser developer tool
orFiddler
.
For more information, visit the following links:
-
To learn more about the code, visit Conceptual documentation for MSAL.NET and in particular:
-
Articles about the Azure AD V2 endpoint http://aka.ms/aaddevv2, with a focus on: