首页 > 解决方案 > 通过 iis 在共享文件夹中搜索

问题描述

我编写了一个网络应用程序,用户可以在其中在共享文件夹中搜索文件,然后他们选择的文件将显示在浏览器中。

当我通过 Visual Studio 运行应用程序时,一切正常。

但是,如果我尝试通过 IIS 运行它,当共享文件夹中的 JSON 执行时,我会收到错误 500(内部服务器错误)。

有权限限制吗?我会通过它使用我的凭据的 VS 运行它,而不是在我通过 IIS 运行它时运行它吗?也许共享文件夹必须具有 IIS_IUSR 组的读取权限?我说对了吗?

public JsonResult GetFiles(string MCodeID)
{
  if (MCodeID == null)
  {
    throw new ArgumentNullException(nameof(MCodeID));
  }

  List<Files> filelist = new List<Files>();
  //Searching Files in //192.168.1.191
  string path = @"\\192.168.1.191\Materials Project\";
  string searchPattern = MCodeID + "*";
  DirectoryInfo fi = new DirectoryInfo(path);
  if (fi.GetFiles(searchPattern, SearchOption.AllDirectories).Any())
  {
    foreach (var file in fi.GetFiles(searchPattern, SearchOption.AllDirectories))
    {
      var changeSlash = file.FullName.Replace('\\', '/');
      var filepath = changeSlash.Replace("//192.168.1.191/Materials Project", "");
      filelist.Add(new Files
      {
        FCodeID = filepath,
        FDescr = file.Name + " - " + Math.Round((Convert.ToDouble(file.Length) / (1024*1024)), 2) + " MB",
        FSize = Math.Round((Convert.ToDouble(file.Length) / (1024 * 1024)), 2)
      });
    }
    filelist.Insert(0, new Files { FCodeID = "0", FDescr = "--Select File--" });
    return Json(new SelectList(filelist, "FCodeID", "FDescr"));
  }
  else
  {
    filelist.Insert(0, new Files { FCodeID = "0", FDescr = "--No File--" });
    return Json(new SelectList(filelist, "FCodeID", "FDescr"));
  }
}
<script type="text/javascript">
  $(document).ready(function () {
    $("#MCodeID").on("change", function () {
      var url = '@Url.Content("~/")' + "Materials/GetFiles";
      var ddlsource = "#MCodeID";
      var items = "<option value='0'>--Select File--</option>";
      $("#FCodeID").html(items);
      var items = " ";
      $("#pdfviewer").hide();
      $(".loading-eclipse").show();
      $.getJSON(url, { MCodeID: $(ddlsource).val() }, function (data) {
        $("#FCodeID").empty();
        $.each(data, function (i, file) {
        if (file.value.includes("in Use"))
          items += "<option value='" + file.value + "'" + "style='background-color:orange;'>" + file.text + "</option>";
        else
          items += "<option value='" + file.value + "'>" + file.text;
      });
      $("#FCodeID").html(items);
      $(".loading-eclipse").hide();
      $("#FCodeID").show();
    });
  })
})
    </script>

当应用程序通过 IIS 执行时,有什么方法可以使用用户凭据?

标签: javascriptc#jsonasp.net-mvciis

解决方案


我通过为用户提供对共享文件夹的完全访问权限而不仅仅是读取权限来解决上述错误。感谢@Jalpa 的帮助。


推荐阅读