asp.net core web api - Angular application http calls gives 401 - Unauthorized

Viewed 185

Asp.Net Core - Web API

I have a asp.net core 5 Odata controller that is hosted on a IIS Express with the following configuration.

  "iisSettings": {
    "windowsAuthentication": true,
    "anonymousAuthentication": false,
    "iisExpress": {
      "applicationUrl": "http://localhost:5001",
      "sslPort": 0
    }
  }

My startup.cs has the following to enable CORS requests:

services.AddCors(c => { c.AddPolicy("AllowOrigin",
                options => options.WithOrigins("http://localhost:4200").AllowAnyHeader().AllowAnyMethod().AllowCredentials());  });
...
 app.UseCors("AllowOrigin");

Angular

When I am calling from my Angular service this above Odata service, I am seeing an error in my browser console that looks like: zone.js:2843 GET http://localhost:4200/api/Mills 401 (Unauthorized)

I have set the withCredentials: true on my interceptor so that user is forced to be authenticated as shown below

intercept(req: HttpRequest<any>, next: HttpHandler):
    Observable<HttpEvent<any>> {
      req = req.clone({ withCredentials: true });
      return next.handle(req);
  }

And my Angular proxy looks like below to bypass CORS issues:

"/api/*": {
        "target": "http://localhost:5001/odata",
        "secure": false,
        "logLevel": "debug",
        "pathRewrite": {
            "^/api": ""
          }
    }

I know that proxy config is working as the next error has <td>&nbsp;&nbsp;&nbsp;http://localhost:5001/odata/Mills</td></tr> \n <tr><th>Physical Path</th

What else do I need to enable the Angular App to get user's Windows identity and pass it to the web-api service?

0 Answers
Related