401 Error on POST using DefaultCredentials

Discussion in 'ASP .Net Web Services' started by Bfranknyc, Nov 21, 2008.

  1. Bfranknyc

    Bfranknyc Guest

    Seeing strange behavior when accessing a web service (Microsoft MVC) on IIS
    7. My client has a library with a generic HTTP submitter routine in it. I am
    using integrated authentication and passing:

    request.Credentials = CredentialCache.DefaultCredentials;

    When I send a GET, my web service is able to authenticate and all is good.
    When I send a POST, my web service returns a 401.

    Here is the GET code:
    Uri uri = new Uri(url + "?" + httpData);
    request = (HttpWebRequest)WebRequest.Create(uri);
    request.Method = "GET";
    request.PreAuthenticate = true;
    request.Credentials = CredentialCache.DefaultCredentials;
    request.KeepAlive = true;
    request.Timeout = 5000;
    WebResponse webResponse = request.GetResponse();
    Stream responseStream = webResponse.GetResponseStream();
    StreamReader responseReader = new StreamReader(responseStream);
    String responseString = responseReader.ReadToEnd();
    webResponse.Close();

    Here is the PUT code:
    Uri uri = new Uri(url);
    request = (HttpWebRequest)WebRequest.Create(uri);
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = httpData.Length;
    using (Stream writeStream = request.GetRequestStream())
    {
    UTF8Encoding encoding = new UTF8Encoding();
    byte[] bytes = encoding.GetBytes(httpData);
    writeStream.Write(bytes, 0, bytes.Length);
    }
    WebResponse webResponse = request.GetResponse();
    Stream responseStream = webResponse.GetResponseStream();
    StreamReader responseReader = new StreamReader(responseStream);
    String responseString = responseReader.ReadToEnd();
    webResponse.Close();


    Webserver log line for GET:
    2008-11-21 19:08:07 10.99.12.89 GET /users - 80 EMWP\bfranklin 10.100.30.69
    - 200 0 0 46

    Webserver log line for POST:
    2008-11-21 18:28:29 10.99.12.89 POST /documents/upload/2008/1/01409/EMWP-811
    - 80 - 10.100.30.67 - 401 2 5 0

    Looks like credentials aren't being passed on a POST, any ideas? BTW the
    posts work fine if I do it from a browser. Thanks in advance!
    Bfranknyc, Nov 21, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pieter Edelman
    Replies:
    0
    Views:
    790
    Pieter Edelman
    Mar 25, 2005
  2. Rodrigo Estrada

    401 Unauthorized when using DefaultCredentials

    Rodrigo Estrada, May 11, 2004, in forum: ASP .Net Security
    Replies:
    1
    Views:
    211
    Paul Glavich [MVP - ASP.NET]
    May 11, 2004
  3. Replies:
    5
    Views:
    148
  4. Replies:
    1
    Views:
    302
  5. Replies:
    5
    Views:
    222
Loading...

Share This Page