specifying credentials for reading file from UNC path  
Author Message
quuxo





PostPosted: .NET Framework Networking and Communication, specifying credentials for reading file from UNC path Top

Hi there;

My question revolves around connecting to an UNC path and reading a file there. I'm using VS C# 2005 Express and c#.

Importantly, I need to be able to programmatically specify the username and password for this connection; the logged-on user most likely will not have the requisite permissions.

p-code:

string strUser = bob;
string strPass = bobspassword;
Streamreader readme = new StreamReader(
strUser, strPass);
while ((line = readme.ReadLine()) != null)
 {
   process lines;
 }
readme.Close();

The highlighted bits are where I'm confused. How do I specify the credentials to go get the file I'm used to specifying credentials for WMI via ConnectionOptions properties such as Username, Password, Domain. Is there any equivalent for filestream operations



.NET Development22  
 
 
SvenC





PostPosted: .NET Framework Networking and Communication, specifying credentials for reading file from UNC path Top

Hi,

I guess you will need to impersonate that user for the current thread that wants to access the share.

Have a look at http://www.pluralsight.com/wiki/default.aspx/Keith.GuideBook/HowToGetATokenForAUser.html to see how to use LogonUser or SSPI to get a logon token.

Use that token to construct a WindowsIdentity and WindowsImpersonationContext. After impersonation succeeded you can access the share.

Here is a sample: http://msdn2.microsoft.com/en-us/library/system.security.principal.windowsimpersonationcontext(VS.80).aspx

--
SvenC


 
 
quuxo





PostPosted: .NET Framework Networking and Communication, specifying credentials for reading file from UNC path Top

SvenC, thank you for your answer. I did have a look at that but it seemed unnecesarily complex in the context of what I was doing; I went with this instead (actually before I saw that you had replied).

Hope I can return the favor someday, though!


 
 
SvenC





PostPosted: .NET Framework Networking and Communication, specifying credentials for reading file from UNC path Top

No problem - good to see that you have a solution.

--
SvenC