WCF service has one method ( Let's say TestMethod) in which I try to create a File Stream like this :
System.IO.FileStream fs = new System.IO.FileStream(#"D:\Test.xml", System.IO.FileMode.Open);
My Client and Service is on the same solution.
When the Client makes a call to TestMethod ( Exposed in Web service ) it will give this error:
Access to the path 'D:\DXDirectoryAuth.xml' is denied.
Please Help!!
Okay, if you have put the file in the directory or a sub-directory of your WCF service you should be able to access the file without any permissions issues.
The question is how are you attempting to access the file?
You should probably get the current directory of the service then append the relative file location onto the current directory and then attempt to open the file something like this:
var appPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
bodyFile = Path.Combine(appPath, #"templates\email.txt");
var body = File.OpenText(bodyFile).ReadToEnd();
HTH
Ollie
Have you tried:
System.IO.FileStream fs = new System.IO.FileStream(#"D:\Test.xml", System.IO.FileMode.Open, System.IO.FileAccess.Read);
The default constructor of FileStream() asks for read and write access.
Security!
The reason being you are trying to access a file location outside of the directory where you have hosted your WCF service...
You are either going to have to grant the account the WCF runs under permissions to that directory or move the file into the directory\sub-directory where you are hosting the WCF service.
Ollie
Related
I have a web service that is trying to write a file to a file share.
The application pool for the site in iis is running as a custom domain account: "domain\domainaccount"
I'm operating under the assumption and hope that when the code tries to write the file it will use the "domain\domainaccount" user to do so.
Executing the following line of code produces the error: Access to the path [filename] is denied
FileStream stream = File.Create(fileName, result.Length);
I have confirmed that the "domain\domainaccount" account has access to [filename] which is the full path of the file including the file name. I have even given the account access from the very top of the share structure, not just the specific folder the file needs to be written to. In fact, if I run notepad as "domain\domainaccount" I can save a file to that exact location.
What might I be doing incorrectly? Is it not using the domain account to write the file? If not, can I change something so that it does?
I should note that if I log into the iis server and run the web service from there, I do not get the access denied message and the file is created.
I have some files kept in SiteFolder/ which I need to access through the C# project but I do not want them to be listed via browser at those path. How can I accomplish this? Is there any setting in IIS that could be changed?
Also, as I gave full control to IIS user to access/read/write this directory files, what is the solution then?
Basically I do not want anyone to access this directory's content via browser but should be accessible by IIS.
Thanks in advance.
Edit 1: This is the file write functionality which should remain unaffected -
FileStream file = new FileStream(Server.MapPath("~/SiteFolder/example.txt"), FileMode.Append, FileAccess.Write, FileShare.Write);
using (StreamWriter fileWriter = new StreamWriter(file))
{
fileWriter.WriteLine("Something");
}
Additionally, as this code is running on Amazon EC2, I had to provide full control of the folder and file to these users -
IUSR
MyProjectName
I am going to assume that you're impersonating the caller when trying to write to the file in question. Otherwise, if you were running as the system account, it doesn't seem like write permission should be affected.
Therefore, what I suggest in addition to the configuration mentioned above (https://serverfault.com/questions/37762/block-access-to-subdirectory-using-web-config) is to run the code block that writes the file as the system account by temporarily undoing impersonation like this:
using (System.Security.Principal.WindowsImpersonationContext wic =
System.Security.Principal.WindowsIdentity.Impersonate(IntPtr.Zero))
{
FileStream file = new FileStream(Server.MapPath("~/SiteFolder/example.txt"), FileMode.Append, FileAccess.Write, FileShare.Write);
using (StreamWriter fileWriter = new StreamWriter(file))
{
fileWriter.WriteLine("Something");
}
}
If your utilizing a default Internet Information System, the default folder wwwroot is locked directly to the designated user through your Application Pool. Which in most instances, will be a Network Service Account.
Your specific application will have a degree of permission, but your web-application by default shouldn't have access to the other contents of wwwroot.
The directory would be:
inetpub - wwwroot - Example
inetpub - wwwroot - Demo
So Example and Demo are both two separate web-sites that are hosted. So if you attempt to navigate to another directory, from one application, it should protect you automatically.
However, if you need to block a web-site from particular users or an application it is indeed possible. In the current state, the question is too broad.
Request Filtering
URL Rewrite
Those are two approaches, however if your utilizing Virtual Directories you should take a look at the following here.
Without a Virtual Directory, my above protection stands true:
A site is a container for applications and virtual directories, and
you can access it through one or more unique bindings.
The binding includes two attributes important for communication: the
binding protocol and the binding information. The binding protocol
defines the protocol over which communication between the server and
client occurs. The binding information defines the information that is
used to access the site. For example, the binding protocol of a Web
site can be either HTTP or HTTPS, and the binding information is the
combination of IP address, port, and optional host header.
I'm trying to use C#'s FileWebRequest to create a new directory on a file share and upload a new file to that directory. Does anyone know if this is possible?
I can successfully upload a new file if it sits directly in the file share, but when I try to upload it inside a new directory, I get an exception.
For example, uploading newfile.txt to this location works fine:
\\servername\sharename\newfile.txt
But if I try to upload it to this location: \\servername\sharename\newfolder\newfile.txt'.
...then I get an exception: Could not find a part of the path \\servername\sharename\newfolder\newfile.txt'.
Background:
I have a web service that needs to generate a file and copy it to a file share. For security reasons, the web service impersonates the specific users who initiate the generation requests, and it needs to copy the file to the share using these (delegated) impersonated identities.
Apparently delegating the impersonated user identity from the web service to the file system doesn't work as-is, so instead, I'm attempting to use the FileWebRequest to upload the file using the URI of the share, so I can use the AuthenticationManager.CustomTargetNameDictionary to register the SPN of the file server so delegation can be achieved.
EDIT: I'm currently using the FileWebRequest to upload the file thus:
var uploadRequest = (FileWebRequest)WebRequest.Create(#"file://servername/sharename/newfolder/newfile.txt");
//...snip
using (var outputStream = uploadRequest.GetRequestStream())
{
outputStream.Write(buffer, 0, buffer.Length);
}
What I am trying to accomplish is to upload some files from one domain on my shared hosting to another domain on the same hosting where the files will be displayed. When I debug the application, the process gets to the SaveAs() method and then throws the exception,"Could not find a part of the path ..." .
I have followed these instructions on finding my site's folder's absolute path and I have implemented this path in my code, using the same method I've been using for a good part of my file uploading, and I have never ran into any problems. My read/write permissions are allowed for the folder that I'm attempting to save these files in.
I'm wondering "Is it because I'm trying to upload the file to a different directory?". If so, is there a better way to accomplish this?
var fileName = Path.GetFileName(file.FileName);
var path = #"D:\Hosting\someNumbers\html\SiteFile\SiteImages\" + fileName;
file.SaveAs(path);
myObject.FilePath1 = path;
Any help will be highly appreciated.
As it turns out, my error was more or less a security issue with GoDaddy's hosting. GoDaddy sees this type of action as a "third party FTP request", which is not allowed. In conclusion, GoDaddy does not allow a user to upload a file on one site, and then FTP that file to another site on the same hosting plan.
I have a doubt from a silverlight application we can access MyDocuments. I am creating an Application which will download a set of files from a remote server . Is it possible to save these file in MyDocuments instead of Isolated Storage. I am using Silverlight 4.0 . Can any one give me Sample codes for it.
In order to acheive that you need to use Silverlight 4 and specify that is should get elevated privileges when install as an Out-of-browser application. When running as an OOB the app will have access to the users Documents folder.
In all other cases you will need to use the SaveFileDialog where the user can explictly specify where to save the file.
Edit code example:-
if (Application.Current.HasElevatedPermissions)
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
path = Combine.Path(path, "MySaveFile.dat");
using (var filestream = File.OpenWrite(path))
{
// pump your input stream in to the filestream using standard Stream methods
}
}
No Isolated storage is currently the only option.