Copy Files With Authentication in C#

Copying files over network (requiring authentication)

You can use WNetUseConnection with p/invokes.

See this thread:

Accessing a Shared File (UNC) From a Remote, Non-Trusted Domain With Credentials

Copy files over network via file share, user authentication

VB but easily translated to C#. Do this before your copy:

Private Sub Open_Remote_Connection(ByVal strComputer As String, ByVal strUserName As String, ByVal strPassword As String)
Dim ProcessStartInfo As New System.Diagnostics.ProcessStartInfo
ProcessStartInfo.FileName = "net"
ProcessStartInfo.Arguments = "use \\" & strComputer & "\c$ /USER:" & strUsername & " " & strPassword
ProcessStartInfo.WindowStyle = ProcessWindowStyle.Hidden
System.Diagnostics.Process.Start(ProcessStartInfo)
System.Threading.Thread.Sleep(2000)
End Sub

How to copy files from a remote server that uses basic authentication

You can do this using the WebClient class in the .NET framework.

Try the following:

using (var client = new System.Net.WebClient())
{
client.Credentials = new System.Net.NetworkCredential("username", "password");

var localPath = @"c:\file.txt";
var remotePath = "http://example.com/files/somefile.txt";

client.DownloadFile(remotePath, localPath);
}

This will download the file from http://example.com/files/somefile.txt and save it to c:\file.txt.

C# copy file to another directory using other domain/username/password

Good question but I dont think this is possible. I believe this type of file copy from one domain to another ( where there is not a trust set up) would be considered security hole.

Some options:

  1. Set up a trust between the domains and give rights to the current user
  2. You could try a Shell execute command and see if you can make this work through command line parameters. I was thinking "Net use" command.

Login with Admin user to some domain and copy files client machines with C#

I had a similar problem: Production needed to run one of my programs that processes files on a location on the network where they don't have any access.

I ended up using Impersonation, which allowed me to run the file processing thread under a set of credentials set at runtime by my program.

In AD I created a special user account with all required permissions for exclusive use by this program.

I know it’s not at all secure, but it works and the odds that it would even occur to someone to hack my program to get these credentials is remote.

Anyway, look into Impersonation I found these resources helpful:

Safely Impersonating Another User

Brian Low's ImpersonationHelper class

-Jay

Copy file on a network shared drive

Untested code, but it will be similiar to:

AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);

// http://pinvoke.net/default.aspx/advapi32/LogonUser.html
IntPtr token;
LogonUser("username", "domain", "password", LogonType.LOGON32_LOGON_BATCH, LogonProvider.LOGON32_PROVIDER_DEFAULT);

WindowsIdentity identity = new WindowsIdentity(token);

WindowsImpersonationContext context = identity.Impersonate();

try
{
File.Copy(@"c:\temp\MyFile.txt", @"\\server\folder\Myfile.txt", true);
}
finally
{
context.Undo();
}

C# uploading file to google drive using service authentication

  • If you create a folder through a service account, this folder will be created on the drive of the service account, to which you do not have access
  • To resolve this issue, you need to to set up an impersonation
  • In other words, you need to set-up the service account in such a way, that it acts on your (or another domain user's) behalf, rather than on its own behalf
  • In this case, the folder will be created on the drive of the user impersonated by the service account
  • To set up impersonation, you need to grant to the service account grant G Suite domain-wide authority through the Admin console
  • In your code, you need to set up a subject (the email of the user to impersonate)
  • The Google documentation provides sample codes for delegating domain-wide authority in Java, Python and HTTP/REST
  • For C#, you can find sample code snippets e.g. on stackoverflow


Related Topics



Leave a reply



Submit