Access Windows Share under Network Service account
Someone asked on Stack Overflow:
I have two computers with Windows Server 2003. One computer has some shared folders on the network, and the other has a Windows Service (written in C#, running under the Network Service account) that needs to access those shared folders.
The following code works fine as a logged-in user, but throws an exception when executed under the Network Service account.
File.WriteAllText(@"C:\temp\temp.txt", File.ReadAllLines(@"\\NetworkServer\Test\test.txt")[0]);The exception message is
Logon failure: unknown user name or bad password. How do I get this code to work under the Network Service account? Is it a setting in Windows Server 2003, or do I need to add some code to this to make it work?
I posted the following answer, which was chosen as the accepted answer and received 3 upvotes:
On the network share, you’ll need to add permissions for the “Network Service” account on the server running the service. While this will work, @nicholas points out that this may provide an overly broad group of users access to the share.
Another option, and in my opinion the better option, is to create a domain account and then give that account read/write permission on the share. Then you configure the service to “run as” the domain account with proper permissions.
Notable comments
Nate (0 upvotes): @nicholas Yes, I believe so. Creating a domain account is, IMHO, the best way to go. I’ve edited to make that clear.
Originally posted on Stack Overflow — 3 upvotes (accepted answer). Licensed under CC BY-SA.