Reputation: 2698
In my ASP.NET application, I am creating a text file and writing data to that file. I am saving that file in specified location on the server. This is an intranet application.
When I run my application locally using Visual Studio, I can create/write and save the file to that network location through the application, but when I deployed the application on the server and then I tried to create the file.
It says "Access Denied".
I am not sure what extra things, I need to do so that I can Create a Text file and save it to a specified location.
In this application, I am authenticating a group of users who can use this application.
Do I need to take any extra steps to create the file on server location and then save the data to it?
Below is my code:
string DirectoryPath = getDirectortyPath();
StreamWriter file = new StreamWriter(DirectoryPath);
file.WriteLine(FullLine);
public string getDirectortyPath(string Year,string Quarter)
{
return ConfigurationManager.AppSettings["ReportPath"] + ".txt";
}
Upvotes: 2
Views: 7665
Reputation: 3875
Possible cause could be the application pool's identity doesn't have the write access to the network location where the file is being created / appended.
Make sure that application pool's identity is set to the account that has read/write access to that network location.
Upvotes: 0
Reputation: 3210
All you need to provide is the credentials for that network. That means you need to provide username and password of the network where you are going to store your files on it.
In your function add:
#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
Create separate controller to make connection in network using above credentials
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
}
}
Upvotes: 2
Reputation: 13783
Other answers have already stated that this is presumably due to the IIS account not having the proper permissions. I'm pretty sure they are right.
From experience, there are other options; although these might not be applicable to your specific situation.
*This is a personal anecdote. I had set up some code to write a file to \\fileserverName\directory\file.txt
; but this did not work after publishing the application.
As it turns out, when I used \\fileserverName.domain.local\directory\file.txt
, it did work. The web server simply did not know the shortname for the server; as it was only added onto our office domain, not the webserver domain.
Upvotes: 0
Reputation: 7454
Usually when developing IIS express runs under your local account. This means that it has access to the same network drives that you do.
When deployed applications typically run under the IIS_IUSRS group unless otherwise specified
You have a few options:
Allow this group access to your network drive
Run the application as your own user
Create a new user and run the application as this user
The first option is not ideal. It means that almost any web application running using the standard config on your network will have access to that network share.
The second option may be useful in the short term, but it means that the application has access to everything that your account does. If your account is an admin account and / or you have admin access to servers this could be very dangerous.
The third option is best overall, but takes a little more set up.
To set the Identity, you will need to create a new Application Pool in IIS and then using the Advanced Settings set the Identity
option to the Custom account
value and then enter the appropriate domain\username and password.
Upvotes: 1