Reputation: 137
I am getting 401 unauthorized error . My web service is written in mvc . in IIS configured to use windows authentication. Below is screen shot of fiddler
When I hit URL from browser it gives me popup window to enter user name and password. How can I avoid popup window?
I am calling this web api from another window service.
Upvotes: 7
Views: 28757
Reputation: 41
I suspect that the two web services may be hosted on the same server. In this case, the problem may be caused by the loopback check. In order to test, try referencing the service without using the fully qualified domain name and see if it works. If it does, use the following steps to specify which host names are on the local computer.
Method 1: Specify host names (Preferred method if NTLM authentication is desired) (https://support.microsoft.com/en-us/help/926642/)
To specify the host names that are mapped to the loopback address and can connect to Web sites on your computer, follow these steps:
**Edited to be in the form of an answer and include detailed steps from referenced links
Upvotes: 4
Reputation: 116438
You can specify the username and password as part of the URL:
http://username:[email protected]/foo/bar/baz
Note: Just because you can doesn't mean you should. While this can be a temporary solution to test things, I would not suggest doing this in production. And in the old days, this is how we did it. But as @DiskJunky points out, "URLs are easily visible to pretty much anything/anyone" which includes your browser history, server logs, and perhaps worse.
Upvotes: 1
Reputation: 6733
My 2 cents: I faced a scenario where we were baffled by an HTTP 401 when requesting an image when the web application was deployed. We use WiX as our packaging and install solution. In this specific case, the image wasn't being packaged by the installer and hence the path did nor exist on the deployed instance.
One may wonder why this threw a 401 when a 404 (not found) would have been expected - my understanding is that since our path was not directly under the root but something like root/content/images/image.png, and I made an anonymous request, I got a 401 (unauthorized) as I did not have the access to browse the directory. I confirmed this by adding an Authorization header to my request and then as expected I got a 404.
Upvotes: 1
Reputation: 137
I added below lines in web config to fix the issue and it worked.
<security>
<authorization>
<add accessType="Allow" users="*" />
</authorization>
</security>
Upvotes: -2
Reputation: 31
If you are using WebClient you need to set the Credientials. How are you calling the web api from the windows service?
Upvotes: 3