401 Unauthorized when using DefaultCredentials

Ask a Question related to ASP.NET Security, Design and Development.

  1. #1

    Default 401 Unauthorized when using DefaultCredentials

    Hi all

    I hope this is the right newsgroup. I'm having a hard time making a web request from an authenticated process. Here's the scenario
    Managed WinForms component hosted in IE, the virtual Directory has Anonymous access disabled and only has Integrated Windows Authentication enabled. When I hit the URL I get challenged for credentials, once I input them I can see the page with my control. The control uses the class WebRequest to make an Http get request for information on the same virtual directory with Integrated Windows Authentication. If I use the following code, everything works as expected

    WebRequest req
    HttpWebResponse resp
    NetworkCredential credential

    req = WebRequest.Create("http://localhost/security/GetImage.aspx")
    req.PreAuthenticate = true
    credential = new NetworkCredential("user", "password", "domain")
    req.Credentials = credential
    resp = (HttpWebResponse)req.GetResponse()

    In my scenario, I don't have access to domain, username and password information, so I have to use the credentials already used by my already authenticated process. In my search I've found that the following code would do the trick

    WebRequest req
    HttpWebResponse resp

    req = WebRequest.Create("http://localhost/security/GetImage.aspx")
    req.PreAuthenticate = true
    req.Credentials = System.Net.CredentialCache.DefaultCredentials
    resp = (HttpWebResponse)req.GetResponse()

    But in this case I get an 401 Unauthorized error. Can anybody tell me if I'm doing something wrong and how to solve it

    Many TIA

    - Rodrigo
    Rodrigo Estrada Guest

  2. Similar Questions and Discussions

    1. DefaultCredentials problem?
      Hi All, this is the scenario: - "Hello World" web service on Machine A - winform client on Machine B - asp.net webform client on Machine B ...
    2. CredentialCache.DefaultCredentials not working!!!!!
      Hi, I'm trying to use the CredentialCache.DefaultCredentials to pick up the credentials of the current user but its not working correctly. I've...
    3. CredentialCache.DefaultCredentials Not working
      I am using the following method to call a web service from a windows application Dim a As New MachineReference.Service1 a.Credentials = s...
    4. Get DefaultCredentials of one web app into another?
      We have a web app which links to another ASP.NET web application (namely the Microsoft CRM). Our app is launched in a new browser window from a link...
    5. DefaultCredentials and WebClient
      I'm trying to make a call inside an ASP.NET web application to an external quasi-web service (aka FrontPage Server Extensions): Dim rpcClient As...
  3. #2

    Default Re: 401 Unauthorized when using DefaultCredentials

    On your server, check the 'Security' event log to see what 'Failure Audits'
    are occurring and examine them to see if a particular user is attempting to
    logon (and get denied) when you try and access the service. You will either
    see a 'failure audit' detailing what user tried to authenticate (and you can
    then tell if you are propagating the user correctly) or you wont see
    anything at all when it fails, which means it's not using anything to
    authenticate as it just gets denied immediately (as no user was supplied).

    Not a solution I know, but it does aid in giving you a better idea of whats
    going on.

    --
    - Paul Glavich
    Microsoft MVP - ASP.NET


    "Rodrigo Estrada" <anonymous@discussions.microsoft.com> wrote in message
    news:A29E8F36-F324-40DC-8A05-79ECF578DB34@microsoft.com...
    > Hi all:
    >
    > I hope this is the right newsgroup. I'm having a hard time making a web
    request from an authenticated process. Here's the scenario:
    > Managed WinForms component hosted in IE, the virtual Directory has
    Anonymous access disabled and only has Integrated Windows Authentication
    enabled. When I hit the URL I get challenged for credentials, once I input
    them I can see the page with my control. The control uses the class
    WebRequest to make an Http get request for information on the same virtual
    directory with Integrated Windows Authentication. If I use the following
    code, everything works as expected:
    >
    > WebRequest req ;
    > HttpWebResponse resp ;
    > NetworkCredential credential ;
    >
    > req = WebRequest.Create("http://localhost/security/GetImage.aspx") ;
    > req.PreAuthenticate = true ;
    > credential = new NetworkCredential("user", "password", "domain") ;
    > req.Credentials = credential ;
    > resp = (HttpWebResponse)req.GetResponse() ;
    >
    > In my scenario, I don't have access to domain, username and password
    information, so I have to use the credentials already used by my already
    authenticated process. In my search I've found that the following code would
    do the trick:
    >
    > WebRequest req ;
    > HttpWebResponse resp ;
    >
    > req = WebRequest.Create("http://localhost/security/GetImage.aspx") ;
    > req.PreAuthenticate = true ;
    > req.Credentials = System.Net.CredentialCache.DefaultCredentials ;
    > resp = (HttpWebResponse)req.GetResponse() ;
    >
    > But in this case I get an 401 Unauthorized error. Can anybody tell me if
    I'm doing something wrong and how to solve it?
    >
    > Many TIA.
    >
    > - Rodrigo

    Paul Glavich [MVP - ASP.NET] Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139