部署IIS后访问共享文件失败

发布时间 2023-04-08 21:32:03作者: 林小强

   在本地调试时没有问题,但是部署到IIS后操作共享文件的时候失败,压根找不到目录。

在和共享文件服务器已经建立连接的情况下,本地调试通过的情况,猜测应该是部署后权限出现了问题,IIS 的权限不够导致的。

尝试了以下几种方法,我用第三种方法可行,可能因为我这边是内网环境,环境不同处理的方式也不太一样,简单的记录一下,仅供参考。

 

1、修改“应用程序池”下的标识为自定义账户。

这里的账户填写这台服务器的登录账户和密码,这里我配置完后重新启动应用池,报错503。  然后又网上查到需要将此账户添加到用户组下的IIS_USER组下。

添加到几个相关的用户组下后,重启服务器还是不行,仍然报错503,应用池会自动停止。 

参考:https://blog.csdn.net/yilianzj/article/details/10606801

 

2、将共享目录添加到本地(网络位置),然后使用IIS 创建虚拟目录

注意设置的特定用户需要在共享目录文件那边要有权限。

 

 参考:https://www.shuzhiduo.com/A/WpdKKwAdVQ/

 

 3、将此IIS应用池或者IIS服务器添加到共享文件权限下。

选择共享目录设置权限,把IIS服务器添加进去,我之前把服务器登录账号添加进去没起作用。在高级里面调整一下对象类型可以查找到服务器(我是在内网环境)

 参考:https://www.likecs.com/show-204583740.html

 

测试的时候把共享文件的权限设置了everyone,能是能访问的到了,但是写入内容后保存失败,文件竟然还直接被删除了.离谱...

 

连接共享文件夹方法: 

        /// <summary>
        /// 连接远程共享文件夹
        /// </summary>
        /// <param name="remoteHost">主机号(ip或主机名)</param>
        /// <returns></returns>
        public bool Connect(string remoteHost)
        {
            this.ShareDto = new ShareConfig()
            {
                RemoteHost = Config.GetSection("ShareParameter")?.GetSection("RemoteHost")?.Value,
                UserName = Config.GetSection("ShareParameter")?.GetSection("UserName")?.Value,
                Password = Config.GetSection("ShareParameter")?.GetSection("Password")?.Value,
            };
            if (string.IsNullOrWhiteSpace(remoteHost))
            {
                remoteHost = this.ShareDto.RemoteHost;
            }
            var flag = false;
            var proc = new Process();
            try
            {
                if (remoteHost.IsNullOrWhiteSpace())
                    throw new UserFriendlyException("主机号不能为空");

                proc.StartInfo.FileName = "cmd.exe";
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardInput = true;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardError = true;
                proc.StartInfo.CreateNoWindow = true;
                proc.Start();
                var host = @"\\" + remoteHost;
                var dosLine = "net use " + host + " " + this.ShareDto.Password
                    + " /user:" + this.ShareDto.UserName;
                proc.StandardInput.WriteLine(dosLine);
                proc.StandardInput.WriteLine("exit");

                while (!proc.HasExited) proc.WaitForExit(1000);

                var errormsg = proc.StandardError.ReadToEnd();
                proc.StandardError.Close();
                if (!errormsg.IsNullOrWhiteSpace())
                    throw new Exception(errormsg);

                flag = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                proc.Close();
                proc.Dispose();
            }
            return flag;
        }