簡體   English   中英

在asp.net中的網絡位置上創建文件

[英]Creating a file on network location in 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";
}

任何幫助將不勝感激。

可能的原因可能是應用程序池的標識對創建/附加文件的網絡位置沒有寫訪問權。

確保將應用程序池的標識設置為對該網絡位置具有讀/寫訪問權限的帳戶。

希望這是有道理的。

通常,在開發IIS Express時,將以您的本地帳戶運行。 這意味着它可以訪問與您相同的網絡驅動器。

除非另外指定,否則當部署的應用程序通常在IIS_IUSRS組下運行時

您有幾種選擇:

  • 允許該組訪問您的網絡驅動器

  • 以您自己的用戶身份運行該應用程序

  • 創建一個新用戶並以該用戶身份運行該應用程序

第一種選擇並不理想。 這意味着幾乎所有在網絡上使用標准配置運行的Web應用程序都可以訪問該網絡共享。

第二個選項在短期內可能有用,但是這意味着應用程序可以訪問您帳戶執行的所有操作。 如果您的帳戶是管理員帳戶和/或您具有服務器的管理員訪問權限,則可能非常危險。

第三種選擇總體上最好,但是需要更多設置。

要設置身份,您將需要在IIS中創建一個新的應用程序池,然后使用“高級設置”將“ Identity選項設置為“ Custom account值,然后輸入適當的域\\用戶名和密碼。

其他答案已經表明,這大概是由於IIS帳戶沒有適當的權限。 我很確定他們是對的。

根據經驗,還有其他選擇。 盡管這些可能不適用於您的特定情況。

  • 如果這是商業環境,則Web服務器可能與員工計算機不在同一個域中。 並且不管訪問權限是什么,都無法訪問文件服務器。
  • Web服務器可能依賴於不同的DNS條目,並且可能不知道與您使用相同名稱的服務器。*

*這是個人軼事。 我已經設置了一些代碼來將文件寫入\\\\fileserverName\\directory\\file.txt 但這在發布應用程序后不起作用。
事實證明,當我使用\\\\fileserverName.domain.local\\directory\\file.txt ,它確實起作用了。 Web服務器根本不知道服務器的簡稱。 因為它僅被添加到我們的辦公室域,而不是網絡服務器域。

您需要提供的只是該網絡的憑據。 這意味着您需要提供要在其中存儲文件的網絡的用戶名和密碼。


在您的函數中添加:

#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
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM