How do I use Azure AD Authentication for a standalone ASP.NET MVC web application with ASP.NET web interface and Api websites?

We have the following:

  • An ASP.NET MVC 5 web application hosted on our non-Azure company server, for example, ourcompany.com. This is not an AngularJS application or a single page application.
  • ASP.NET MVC WebApi 2 is hosted on our company server (not Azure), for example, api.ourcompany.com. This is a separate project from a website, so you can access it with several applications / sites.
  • Azure Active Directory for our company

What we want to do:

  • A user visits a website and logs in through Azure AD authentication.
  • The web application displays pages with JavaScript.
  • JS page calls web API

I see documents / samples in Azure documentation for:

  • Web Application for Azure AD
  • Web Application for WebApi
  • SPA for WebApi

If the API components were included in the web application project, a JS call from the page will have access to the user credentials. However, regardless of the API, the questions are: how to use authentication in # 1 to allow # 3 to call WebApi using JS and have access to all authentication information?

+5
source share
1 answer

I managed to get a similar script to work, starting with a sample of WebApp and WebApi using Azure AD and OpenID Connect.

Calling a web API in a web application using Azure AD and OpenID Connect

The sample provides a single sign for users in the web application and delegates the user ID from the web application to the api web application. He did this by extracting the carrier token into the web application controller and using it to call the web api. We can easily use the same bearer token to make an ajax javascript call by putting the bearer token in the authorization header:

//jQuery example settings.headers = { 'Authorization': 'Bearer ' + bearerToken, }; 

I was able to get this to work without adal.js liberary. Kors is still needed to make an ajax call in the cross domain.

0
source

All Articles