设置“UseDefaultCredential = True”时的DownloadString给出401错误

时间:2017-10-25 04:14:28

标签: c# .net model-view-controller webclient downloadstring

我已经将一个网站部署到IIS(在本地计算机上一直正常工作),并且发现当我运行其中一个例程时,它失败并显示401未授权错误:

代码如下:

          string html = String.Empty;
            string CurrentURL = Request.Url.Scheme + Uri.SchemeDelimiter + Request.Url.Host + Request.Url.PathAndQuery;
            Byte[] sAttachment;

            using (WebClient client = new WebClient())
            {                    
                client.UseDefaultCredentials = true;

                _logger.Debug("Getting page content ready to fax...");
--> fails here: html = client.DownloadString(new Uri(CurrentURL));

                html = html.Replace("Add Recipient", "");
                html = html.Replace("Send Fax", "");
                html = html.Replace("Close", "");

                //make sure the destination fax number and recipient is in the html
                string toFaxDetailsHTML = "insertFaxDetails\">";
                toFaxDetailsHTML += "<label class=\"form-label col-md-2 text-right\">Fax: </label>" + FVM.ToFaxNumber + "<br/>";
                toFaxDetailsHTML += "<label class=\"form-label col-md-2 text-right\">To: </label>" + FVM.ToName + "<br/><br/>";
                html = html.Replace("insertFaxDetails\">", toFaxDetailsHTML);

                sAttachment = FVM.PdfSharpConvert(html);
                FVM.Attachment = sAttachment;
            }

任何帮助表示赞赏。

环境是Windows 2008 R2标准上的IIS 7。 使用MVC 5 c#.NET 4.5.2

1 个答案:

答案 0 :(得分:0)

IIS创建一个名为&#34; IUSR&#34;的用户,通过IIS作为defaut用户访问文件。因此,重要的是&#34; IUSR&#34;拥有对文件/文件夹的适当访问权限。

因此,请按照以下步骤查看IUSR是否具有适当的访问权限:

Deploy Folder→Right Click→Properties→Security Tab

请确保IUSR在群组或用户名列表中,如果否,请执行以下操作:

Click Edit→Add→Advanced→Find→Add IUSR.

※我使用的是日文版的Widows,因此按钮名称可能略有偏差