Angular2 Windows Authentication
Windows Authentication is required for intranet web sites, I have been working for a manufacturing company for 4 years, we have domain account/ Active Directory for security checkup in each sites. So if we need to develop a Angular2 application which can be used all over the domain, then we need to think about the security very seriously, this is just a small demo with very limit functions.
ASP.NET Web Api
Create a ASP.NET WebApi project with Windows Authentication checked, this will normally include the basic settings which are required for Windows Authentication. Just double check following two files
launchSetting.json
web.config
Enabling Cross-Origin Request(CORS)
Browser security provents a web page from making AJAX requests to another momain. This restriction is called the same-origin policy, and provents a malicious site from reading sensitive data from another site.
Cross Origin Resource Sharing(CORS) is a W3C standard that allows a server to relax the same-origin policy. Using CORS, a serve can explicitly allow some cross-origin requests while rejecting others.
Add CORS package to your project
you can use NuGet Manger to install it or run npm install
reference it in your startup.cs file
using Microsoft.AspNetCore.Cors;
Add the orgins which will access your WEB API, for example: http://localhost
- option 1: you can enable it globally by adding it in following method
- Option 2: Or you can enable it in the controller class or action method
Deploy WebApi to IIS 7
The deployment procedure should be very simple, I will not list the procedures here. host-aspnetcore-webapi-in-iis
Notice: Make sure “Windows Authentication” is enabled and “Anonymous Authentication” is disabled. This is to ensure all requests are using windows authentication.
Angular2 Application
By default, the browser will not send user authentication details to the server. We must configure out Angular2 application requests to send this information.
Summary
Above processes should be implemented without any failure, you can add more functionalites into it. I hope this artical will save couple of your valuable hours.
Errors you may encounter
- No ‘Access-Control-Allow-Origin’ header is present on the requested resource
- HTTP Error 401.2 - Unauthorized
- The response had HTTP status code 401
- XMLHttpRequest cannot load