33

I am trying to access a service that is behind Windows Authentication. I thought I could pass the credentials the way HTTP Basic Authentication credentials are passed, but it's not working. When I do the following, I get a 401 error, and I am absolutely sure the password is correct.

curl --user username:password http://example.com

Why doesn't this work?

woz
  • 10,888
  • 3
  • 34
  • 64

4 Answers4

48

If you are using Windows Authentication, you need to use NTLM:

curl --ntlm --user username:password http://example.com
Sebas
  • 21,192
  • 9
  • 55
  • 109
woz
  • 10,888
  • 3
  • 34
  • 64
  • 5
    And if you don't put the password the command line will prompt you for it. – Naftali Oct 18 '12 at 17:42
  • 5
    See my answer with how to use the current logged in user for your requests – Sean Lynch Feb 23 '15 at 18:24
  • I see a strange behaviour, when I keep my password in command line option `-u`, it doesn't work, but the same password works when I keep just the username there, and enter password when prompted. Any idea why? – 0xc0de May 14 '19 at 07:03
39

You can also leave the username and password fields empty (-u :) and curl will use your current credentials from your environment:

curl --ntlm -u : http://example.com

Per the docs (under -u, --user)

If you use a Windows SSPI-enabled curl binary and perform Kerberos V5, Negotiate, NTLM or Digest authentication then you can tell curl to select the user name and password from your environment by specifying a single colon with this option: "-u :".

Kerberos-Enabled Binaries

ggrandes
  • 2,067
  • 22
  • 16
Sean Lynch
  • 6,267
  • 2
  • 43
  • 45
4

I just want to add that authorization might include several redirects and the NTLM authentication might be required for the second or subsequent requests, but not the first one. In this case the answers here won't work.

The thing is, CURL sends credentials with the first request only, while you might need them for the second one for example. To pass credentials with all the requests, use the --location-trusted option.

From CURL documentation (-L option):

When authentication is used, curl only sends its credentials to the initial host. If a redirect takes curl to a different host, it won't be able to intercept the user+password. See also --location-trusted on how to change this.

This important note helped me authorize on my company website. Hope that helps somebody else.

makeiteasy
  • 766
  • 7
  • 11
0

I had the same issue with you, but I through adjust iis server configuration to authenticate, to enable HTTP basic or digest authentication, after done, you could use simplest authenticate, sorry for my bad English

Chauncery
  • 111
  • 2
  • 6