I'm trying to read a file from a android phone to a Windows share. I'm using now the StreamWriter, like below:
StreamWriter outfile = new StreamWriter(#"\\10.16.68.253\sam\AllTxtFiles.txt");
outfile.WriteLine("TESTGENREOIADNIWAN");
But i get a Access Denied. I already added the permission to the manifest. And i tested the share using the ES File Explorer (with everyone access). I used the same lines of code on a WPF app and works fine, so my problem is in Android app.
I already tried to change the connection string like the ES, smb://10.16.68.253/sam/AllTxtFiles.txt, but no luck.
Anyone accomplished this ?
Tks !
for me I had to set up the string like this "smb://username:password#local ip/" for the root of my server's Windows share.
You have to make sure the connection to the SMB share is authenticated before trying to access any files. You have to use API calls to do this, because this stuff is done in the Win32 layer outside of managed code.
Here's a start:
http://www.pinvoke.net/default.aspx/mpr/WNetAddConnection.html
Are you sure you are able to read/write files using ESFileExplorer with EVERYONE ACCESS permissions?
Windows is a very secured OS. You need to grant permissions to a particular profile.
Example:
Create a new profile name on windows.
Give that new profile name a password. (must have a password to share something)
Give the folder or files permission to share access to that profile with password.
Access the folder or files using smb along with the profile username and password. Such as
"smb://username:password#local ip"
StreamWriter outfile = new StreamWriter(smb:\\username:password#"\\10.16.68.253\sam\AllTxtFiles.txt");
outfile.WriteLine("TESTGENREOIADNIWAN");
Related
So part of my job is to write a file to iSeries IFS, or this path in particular \ServerIPAddress\home\test\
I have an ASP.NET web application to do this, and this is the code (C#) I use to write a simple text file to that directory:
string filename = #"\\SomeIPAdress\home\test\test.txt";
byte[] file = Encoding.ASCII.GetBytes("hello world");
FileStream fs = new FileStream(filename, FileMode.OpenOrCreate);
fs.Write(file, 0, file.Length);
fs.Close();
When executing this code, the program gives me "Access Denied" error
Exception Details: System.UnauthorizedAccessException: Access to the path '\SomeIPAddress\home\test\test.txt' is denied.
ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity ...
I can access this directory \SomeIPAddress\home\test using windows file explorer using IBM UID and password, and I can create and edit a text file manually as well.
I know it has to have something to do with granting access right to my ASP.NET app by providing that UID and password, but I can't quite figure it out, and I have been stuck for few days.
Let me know if you need any extra information. Thanks for the help
Two solutions.
Best Practice. Setup the iseries to use the same domain controller as everything else on your network. Then it will know the asp.net account requesting access and allow access to the IFS.
or
Setup a mapped drive on the machine hosting the asp.net page that points to the IFS. The mapped drive will have credentials saved in the vault.
Thanks Mike Wills for leading me to a solution. This is the code I use to connect to the network share using P/Invoke WNet Connection, which is from this answer here
DisconnectFromShare(#"\\server-a\DBFiles", true); //Disconnect in case we are currently connected with our credentials;
ConnectToShare(#"\\server-a\DBFiles", username, password); //Connect with the new credentials
if (!Directory.Exists(#"\\server-a\DBFiles\"))
Directory.CreateDirectory(#"\\server-a\DBFiles\"+Test);
File.Copy("C:\Temp\abc.txt", #"\\server-a\DBFiles\");
DisconnectFromShare(#"\\server-a\DBFiles", false); //Disconnect from the server.
Thanks guys for the help.
I think this is what you are looking for. Sorry, I don't have access to my code at work that I know works right now. It was really simple to do and worked perfectly.
If you want my working code, please let me know and I'll pull that tomorrow.
UPDATE: Sorry, I didn't post my code earlier, we are swamped at work.
NetworkCredential nc = new NetworkCredential("user", "password", "domain");
using (new NetworkConnection(#"\\server\directory1\directory2", nc))
{
// your IO logic here
}
I am trying to create a file on the remote machine but I am getting The "Network name cannot be found". I checked the network path and I was able to access the path from my machine. Could you please let me know what could be wrong?
Here is my code.
using (StreamWriter sw = new StreamWriter("\\\\servername\\TEST1\\TEST\\NEWFILE.csv", true))
{
sw.WriteLine(sw);
}
Go to \servername\TEST1 and give write permission to the user or aspnet (if you have a web application) on test folder and then re-run your program. It will work.
To give write permissions, just refer to this article:
How to share a folder/File
In case it still does not work, replace servername with server IP address and do the same as stated above.
Give the access rights to the user under which this application runs either it is a IIS pool or windows service etc
it is surely a security isssue. you need to give Write access to the remote machine
I'm using EWS to grab file attachments from emails in an inbox, and need to put those files (if they meet certain criteria) onto a network directory path that requires an active directory user/pass that is not the same as what the machine running the service is using.
There's probably multiple ways to attack this. Without having to set that directory path to allow the user/pass that is running the windows service to have rights to read/write is there a way in code that I can set the user/pass before I try and place the files in that path?
In the installer setup of the windows service I've tried the following:
this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.User;
this.serviceProcessInstaller1.Password = "password";
this.serviceProcessInstaller1.Username = #"\\serverName\user";
when I try and install I get an error about mapping the user pass, so I tried this:
this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.NetworkService;
this.serviceProcessInstaller1.Password = "password";
this.serviceProcessInstaller1.Username = #"\\serverName\user";
the installer works, the service shows up and I can start it, but when I debug/attach to the process it throws an exception when trying to write to the directory about access rights.
So maybe I'm not even attacking the right issue/section, as this is probably an active directory issue and something not done in code.
Any suggestions?
What you tried there is irrelevant to your problem.
If you're on windows 7, you may workaround by going to [Control Panel]->[User Accounts]->[Credential Manager] to store login information of target machines.
While I am trying to upload a registry in my registries using the C# code , the application is throwing the error "a required privilege is not held by the client". If I am using the same code on some other machine it is working fine but not particularly on my machine
I am using below mentioned code to upload the registry files
Process my_p = new Process();
my_p.StartInfo.FileName = "reg";
my_p.StartInfo.Arguments = "load HKLM\TEST C:\Documents and Settings\Administrator\NTUSER.DAT";
my_p.Start();
my_p.WaitForExit();
System.IO.StreamReader srOutPut = my_p.StandardOutput;
System.IO.StreamReader srError = my_p.StandardError;
my_p.Close();
results = srOutPut.ReadToEnd().Trim();
Errors = srError.ReadToEnd().Trim();
Moreover One thing I have noticed that the above code I am using in http based web site, but when I am using it in a File Syatem based web site it is working fine. Please help I am not getting the error.
Regards,
Vikram
You can load RegLoadKey function directly to load the hive as a subkey. Another API is RegLoadAppKey but it works only starting from Windows Vista. Different versions of Reg.exe use the API. How you can read in the description the RegLoadKey you need have SE_RESTORE_NAME and SE_BACKUP_NAME privileges and enable these (see http://msdn.microsoft.com/en-us/library/ms717797.aspx). If you are in the Group of Administrators or Backup Operators you have these privileges. One more problem can be if Reg.exe use RegLoadKey function and you have a local administrative rights, but you start on Vista or Windows 7 a command without admin rights because of UAC (User Account Control).
I have a file on a remote server and I want to read this file.
lets say the files location is:
string filePath = #"\\192.168.101.15\c$\program files\xxx\test.xml";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
This code is for sure throwing an error:
Logon failure: unknown user name or bad password.
How can I pass my credentials??
if I go start/run and put this path, I need to provide credentials lets say Admin and password 123.
Im using Asp.net, c# 3.5
Any Ideas
You have to use impersonation, ie execute your code with a user who has acces to the shared folder instead of asp.net user :
http://msdn.microsoft.com/en-us/library/aa292118%28VS.71%29.aspx
You have two way :
-with code
-with configuration
Your application will need to run as a user that has access to the UNC path, or else impersonate a user with such permissions, for the file load operation.
You'd need to be pre-authenticated on the share before you can access the files. It isn't something you can do just by passing a UNC path.
You might consider executing a net use command via the shell programatically. That's the only way I can find to do this.