Enable Windows Authentication between Angular and ASPNet WebAPI

--- ---

How to enable Windows credentials between Angular and ASPNET WebAPI

  1. Create the WebAPI with windows authentication enabled
  2. You might need to enable CORS
  3. In your http calls of your Angular app, send a request options object, in this object, set withCredentials = true
    import { HttpClient } from '@angular/common/http';
    import { Injectable } from '@angular/core';
    
     @Injectable({
     providedIn: 'root'
     })
     export class ItemsService {
    
     constructor(private httpClient: HttpClient) { }
     getItems = () => {
         return this.httpClient.get('http://localhost:49288/api/items', { withCredentials: true });
     }
     }
    
    
  4. In your WebAPI service, one way to get the current user is:
     public class ItemsController : ApiController
     {
         public string Get()
         {
             return User.Identity.Name;
         }
     }
    

Resources

Comments

Popular posts from this blog

WPF - Combobox with a null item

Add Cordova and Electron to an Angular App

How to enable CORS between an Angular app and an ASP.Net Web API Service