在asp.net中的网络位置创建文件

时间:2017-04-27 14:56:58

标签: c# asp.net

在我的ASP.net应用程序中,我正在创建一个Text文件并将数据写入该文件。我将该文件保存在服务器上的指定位置。这是一个Intranet应用程序。当我使用Visual studio.net在本地运行我的应用程序时,我可以通过应用程序创建/写入并将文件保存到该网络位置,但是当我在服务器上部署应用程序然后我尝试创建该文件时。它说“拒绝访问”。

我不确定还有什么额外的东西,我需要这样做,我可以创建一个文本文件并将其保存到指定的位置。

在此应用程序中,我正在验证可以使用此应用程序的一组用户。

我是否需要采取任何额外步骤在服务器位置创建文件,然后将数据保存到服务器位置。以下是我的代码:

string DirectoryPath = getDirectortyPath();
StreamWriter file = new StreamWriter(DirectoryPath);
file.WriteLine(FullLine);

public string getDirectortyPath(string Year,string  Quarter)
{
    return ConfigurationManager.AppSettings["ReportPath"] + ".txt";
}

任何帮助都会得到很大的帮助。

4 个答案:

答案 0 :(得分:0)

可能的原因可能是应用程序池的标识没有对创建/附加文件的网络位置的写访问权。

确保将应用程序池的标识设置为对该netowrk位置具有读/写访问权限的帐户。

希望这是有道理的。

答案 1 :(得分:0)

通常在您的本地帐户下开发IIS Express时。这意味着它可以访问您所使用的相同网络驱动器。

部署的应用程序通常在IIS_IUSRS组下运行,除非另有说明

您有几个选择:

  • 允许此群组访问您的网络驱动器

  • 以您自己的用户身份运行该应用程序

  • 创建新用户并以此用户身份运行应用程序

第一种选择并不理想。这意味着几乎所有使用网络上标准配置运行的Web应用程序都可以访问该网络共享。

第二个选项在短期内可能有用,但这意味着应用程序可以访问您帐户的所有内容。如果您的帐户是管理员帐户和/或您拥有对服务器的管理员权限,则可能非常危险。

第三种选择总体上是最好的,但需要更多设置。

要设置标识,您需要在IIS中创建一个新的应用程序池,然后使用高级设置将Identity选项设置为Custom account值,然后输入相应的域\用户名和密码。

答案 2 :(得分:0)

其他答案已经说过,这可能是由于IIS帐户没有适当的权限。我很确定他们是对的。

根据经验,还有其他选择;虽然这些可能不适用于您的具体情况。

  • 如果这是一个业务环境,则Web服务器可能与员工机器位于不同的域中;并且无论访问权限如何,都无法访问文件服务器
  • 网络服务器可能依赖于不同的DNS条目,可能与您所知的服务器名称不同。*

*这是个人轶事。我已经设置了一些代码来将文件写入\\fileserverName\directory\file.txt;但是在发布申请后这没有用 事实证明,当我使用\\fileserverName.domain.local\directory\file.txt时,它确实有效。 Web服务器根本不知道服务器的短名称;因为它只被添加到我们的办公室域,而不是web服务器域。

答案 3 :(得分:-1)

您需要提供的只是该网络的凭据。这意味着您需要提供要在其中存储文件的网络的用户名和密码。


在您的函数中添加:

#region network connection
        string networkPath = \\192.168.10.19\e;
        string userName = @"anil";
        string password = "1234";
        NetworkCredential credentials = new NetworkCredential(userName, password);
        string myNetworkPath = string.Empty;
     using (new ConnectToSharedFolder(networkPath, credentials))
            {
              //your stuff here
              // your new path must include: networkpath+ [your folder]
           }
  #endregion

创建单独的控制器以使用上述凭据在网络中建立连接

 public class ConnectToSharedFolder : IDisposable
    {
        readonly string _networkName;

        public ConnectToSharedFolder(string networkName, NetworkCredential credentials)
        {
            _networkName = networkName;

            var netResource = new NetResource
            {
                Scope = ResourceScope.GlobalNetwork,
                ResourceType = ResourceType.Disk,
                DisplayType = ResourceDisplaytype.Share,
                RemoteName = networkName
            };

            var userName = string.IsNullOrEmpty(credentials.Domain)
                ? credentials.UserName
                : string.Format(@"{0}\{1}", credentials.Domain, credentials.UserName);

            var result = WNetAddConnection2(
                netResource,
                credentials.Password,
                userName,
                0);

            if (result != 0)
            {
                throw new Win32Exception(result, "Error connecting to remote share");
            }
        }

        ~ConnectToSharedFolder()
        {
            Dispose(false);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            WNetCancelConnection2(_networkName, 0, true);
        }

        [DllImport("mpr.dll")]
        private static extern int WNetAddConnection2(NetResource netResource,
            string password, string username, int flags);

        [DllImport("mpr.dll")]
        private static extern int WNetCancelConnection2(string name, int flags,
            bool force);

        [StructLayout(LayoutKind.Sequential)]
        public class NetResource
        {
            public ResourceScope Scope;
            public ResourceType ResourceType;
            public ResourceDisplaytype DisplayType;
            public int Usage;
            public string LocalName;
            public string RemoteName;
            public string Comment;
            public string Provider;
        }

        public enum ResourceScope : int
        {
            Connected = 1,
            GlobalNetwork,
            Remembered,
            Recent,
            Context
        };

        public enum ResourceType : int
        {
            Any = 0,
            Disk = 1,
            Print = 2,
            Reserved = 8,
        }

        public enum ResourceDisplaytype : int
        {
            Generic = 0x0,
            Domain = 0x01,
            Server = 0x02,
            Share = 0x03,
            File = 0x04,
            Group = 0x05,
            Network = 0x06,
            Root = 0x07,
            Shareadmin = 0x08,
            Directory = 0x09,
            Tree = 0x0a,
            Ndscontainer = 0x0b
        }
    }
相关问题