1

I am working on localhost where angular and web api using 2 different ports that's causing issue now.

I am trying to call web api c# from angular like this

this.http.post<AccessToken>(`${this.url}/api/Home/Eid/?code=12345sfffs`,{headers:this.httpHeaders})
       .pipe(Response => {
         console.log("hi");
         console.log(Response);
        return Response;
        }); ```

But I am getting below error

Failed to load resource: the server responded with a status of 401 (Unauthorized)

When I tried in postman also I got same 401 error and later I fixed by adding NTLM credentials in postman to get 200 response.

But how to pass NTLM credentials from angular post call? And why web api expecting for NTLM credentials?

Thanks in advance!

Manoj R
  • 21
  • 5
  • TL;DR: add `withCredentials: true` to your `options` object. Be aware that the user's browser needs an extant NTLM/Kerberos session with the remote host, and this won't work over the Internet. – Dai May 10 '22 at 05:04
  • Pass your token or values of authentication through Header. – Kiran Joshi May 10 '22 at 05:04
  • @KiranJoshi [NTLM/Kerberos is managed by the browser _and_ client OS, and the browser does not expose that information to scripts](https://stackoverflow.com/questions/18527749/javascript-ajax-ntlm-authentication). The token/headers are only accessible to the browser (like a `httponly` cookie). – Dai May 10 '22 at 05:05
  • @Dai Thank you so much, it's worked! Thanks for everyone for your responses – Manoj R May 10 '22 at 06:24

1 Answers1

1

I have modified my angular post call to this

  return
 this.http.post<AccessToken>(`${this.url}/api/Home/Eid/?code=12345sfffs`,{headers:this.httpHeaders},{
withCredentials: true })
        .pipe(Response => {
         console.log("hi");
         console.log(Response);
         return Response;
         });

In Web API, I have modified web.config to this

<httpProtocol>
      <customHeaders>       
        <add name="Access-Control-Allow-Origin" value="http://localhost:4200"/>               
        <add name="Access-Control-Allow-Credentials" value="true" />              
      </customHeaders>
    </httpProtocol>
Kiran Joshi
  • 1,758
  • 2
  • 11
  • 34
Manoj R
  • 21
  • 5