8

Web API has been created in .Net with Windows Authentication. When am invoking the API in React APP getting 401 Unauthorized. Web API Works fine in the browser with windows authentication.

axios.get('http://.../.../user/Data')
    .then(function (response) {
      console.log(response);
    })
    .catch(function (error) {
      console.log(error);
    });```
TrueWill
  • 25,132
  • 10
  • 101
  • 150
Mathan
  • 143
  • 2
  • 6
  • Should be similar to https://stackoverflow.com/questions/1002179/how-can-i-pass-windows-authentication-to-webservice-using-jquery – TrueWill Apr 19 '19 at 16:11
  • 1
    That post is related to Jquery -> if the front-end application is windows authenticated, it will work directly. Can u help me to make the React app windows authenticated. – Mathan Apr 19 '19 at 16:16
  • When you say it works fine in the browser you mean it prompts you for your credentials? – go_diego Apr 19 '19 at 16:18
  • No it wont, Web API project has been developed in .Net and hosted with 'Windows Authentication', so API will be directly able to access my Windows Login UserName. – Mathan Apr 19 '19 at 16:26
  • @Mathan jQuery's `$.ajax` is essentially the same as axios - both make AJAX calls. React runs in the browser; the issue is when you make an AJAX call to the server. You could replicate this with just axios (no React). – TrueWill Apr 19 '19 at 16:27
  • @TrueWill Can you provide some sample code which will help me out to try. – Mathan Apr 19 '19 at 16:37

1 Answers1

4

With fetch I set the credentials: include option. Also, I think the Access-Control-Allow-Credentials header needed to be set on the back-end.

I'm a front-end developer so I'm not sure what all went into configuring the back-end for Windows Authentication.

go_diego
  • 393
  • 2
  • 7
  • In the Back-end Project, authentication is set to Windows. Kindly help me to get it work in Front-end. – Mathan Apr 19 '19 at 16:36
  • 1
    What I'm saying is try using `withCredentials: true,` with Axios since `credentials: include` worked for me using fetch. But, if I remember correctly, it only worked for me if the `Access-Control-Allow-Credential` response header was set. – go_diego Apr 19 '19 at 16:39
  • Did you need to set the header? Also, please accept the answer so others can find it more easily. – go_diego Apr 19 '19 at 17:31