System.Net.WebClient doesn't work with Windows Authentication

前端 未结 4 1883
广开言路
广开言路 2020-12-08 11:58

I am trying to use System.Net.WebClient in a WinForms application to upload a file to an IIS6 server which has Windows Authentication as it only \'Authentication\' method.

相关标签:
4条回答
  • 2020-12-08 12:26

    I have seen a similar issue, where the Integrated / NTLM security will only work if you are accessing the host by machine name or localhost. In fact, it is a [poorly] document feature in Windows that is designed to protect against "reflection attacks".

    Basically, you need to create a registry key on the machine that is trying to access the server, and whitelist the domain you are trying to hit. Each host name / FQDN needs to be on it's own line - there are no wildcards and the name must match exactly. From the KB Article:

    • Click Start, click Run, type regedit, and then click OK.
    • In Registry Editor, locate and then click the following registry key: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0
    • Right-click MSV1_0, point to New, and then click Multi-String Value.
    • Type BackConnectionHostNames, and then press ENTER.
    • Right-click BackConnectionHostNames, and then click Modify.
    • In the Value data box, type the host name or the host names for the sites that are on the local computer, and then click OK.
    • Exit Registry Editor, and then restart the computer.

    http://support.microsoft.com/kb/956158/en-us

    0 讨论(0)
  • 2020-12-08 12:28

    Try going into IE's options and explicitly add the site to the Intranet Zone. Then re-run the program. You should also not run the program from an administrator login. This may trigger the Enhanced Security Configuration for Internet Explorer.

    It could explain why you can hit the site with Firefox and Opera, but not with IE or WebClient.

    0 讨论(0)
  • 2020-12-08 12:32

    Without knowing your IIS deployment, and assuming that you have the correct authorization rules for upload set in IIS (e.g. the right allow* ACL's on the right dirs you are trying to upload content to, etc), first thing I would try is to set UseDefaultCredentials to true instead of explicitly set Credential. (Maybe you think you are accessing the server with the Credentials you are setting but that's not the case? That would be possible if this works.)

    This is a very common scenario, so I would focus on IIS authorization rules for the directory in which you are trying to upload the file, the actual ACL's on that directory. For ex. is your site impersonating or not? if it is, then you have to have actual ACL's on that dir, otherwise whatever account app pool is running on.

    0 讨论(0)
  • 2020-12-08 12:37

    Have you tried ...

    new NetworkCredential( "peter", "password", "boxname" );
    

    You might also try ...

    var credCache = new CredentialCache();
    credCache.Add( new Uri ("http://localhost/upload.aspx"),
                     "Negotiate",
                     new NetworkCredential("peter", "password", "boxname"));
    wc.Credentials = credCache;
    

    Also, according to this it may be that IIS is configured wrong. Try replacing "Negotiate" with "Basic" in the above and checking your IIS config for the website. There's also a bunch of possible causes here.

    0 讨论(0)
提交回复
热议问题