Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Move a file accross a network in C# using different credentials

Posted on 2009-05-15
9
Medium Priority
?
916 Views
Last Modified: 2013-12-17
Hi all,

I have a windows service that is polling a local folder for a particular criteria of file, when it finds it, it should then move the file to a network share on another computer.

The local machine account (which the service runs under) is not able to access that share, nor can the service be run by another account, it must be run under the local machine account.

I want to be able to specify domain user credentials for the service to be able to move the file to the network share.

Can anyone help?

Thanks,

Xavier.
0
Comment
Question by:locdang
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 8

Accepted Solution

by:
Bob Hoffman earned 1200 total points
ID: 24400898
Use Impersonation, The attached module has the code, the code snipet below shows how to use it, pretty simple.
You can covert the vb to C# here:
http://www.developerfusion.com/tools/convert/vb-to-csharp/ 

            'Impersonate an admin user
            If Impersonate("UserId", "Password", Domain") Then
                
                 'Do what you need to do a Admin here
 
                Call UnImpersonate()
            End If

Open in new window

Impersonation.txt
0
 
LVL 3

Assisted Solution

by:nhenny2009
nhenny2009 earned 800 total points
ID: 24400907
If you want to run the service as a domain account, then you will need to edit the local security policy (secpol.msc).  Make sure the domain user has the following rights

Log on as a service
Allow logon locally
Log on as batch

All of these services may not be necessary, and you can trim down and retest.
0
 
LVL 1

Author Comment

by:locdang
ID: 24400915
HBHoffman,
I am afraid that won't work, the machine the service is running on is not on the same domain as the destination share.

nhenny2009,
As I said in the question I cannot change the account the service runs as, it as to be the local machine account.
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 8

Assisted Solution

by:Bob Hoffman
Bob Hoffman earned 1200 total points
ID: 24400960
Can you map a drive (on the machine running the service) to the share and have the service connect to the mapped drive?
0
 
LVL 1

Author Comment

by:locdang
ID: 24400962
A mapped drive wouldn't work as its a service. It needs to be able to run when no one is logged in.
0
 
LVL 3

Expert Comment

by:nhenny2009
ID: 24400963
Sorry, I misunderstood.
0
 
LVL 3

Assisted Solution

by:nhenny2009
nhenny2009 earned 800 total points
ID: 24400969
So I assume you also could not run the service and network service account?
0
 
LVL 8

Assisted Solution

by:Bob Hoffman
Bob Hoffman earned 1200 total points
ID: 24400973
You may have already considered a helper service, but if not:
http://stackoverflow.com/questions/182750/how-to-map-a-network-drive-to-be-used-by-a-service 
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 24401583
i have a question regarding the following statement

>> The local machine account (which the service runs under) is not able to access that share, nor can the service be run by another account, it must be run under the local machine account.

why the service cannot be run under any other domain authenticated account other then local machine account
are there any limitations
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question