Read file from network location with credentials i

2019-07-19 15:08发布

问题:

I want to read files from a folder that exists on the network.

When I try to access this folder manually (from run command giving a path like \\ABCServer\Documents ) it ask me for credentials (username and password). After giving the correct credentials I am able to access/read files.

When I try to read the same files from C# code in ASP.NET it gives me an error:

Login Failure: unkown username or bad password

How can I pass credentials via C# code during reading file?

Below is a part of the code that I am using:

Stream s = File.OpenRead(filePath);
byte[] buffer = new byte[s.Length];            
try
{
    s.Read(buffer, 0, (Int32)s.Length);
}            
finally 
{ 
    s.Close();
}     

Note:

  • The code works fine
  • I'm using ASP.NET 4.0 with C#
  • IIS version is 7.5

回答1:

This is from http://support.microsoft.com/kb/306158

public const int LOGON32_LOGON_INTERACTIVE=2;
public const int LOGON32_PROVIDER_DEFAULT=0;

WindowsImpersonationContext impersonationContext;

[DllImport("advapi32.dll")]
public static extern int LogonUserA(String lpszUserName,
    String lpszDomain,
    String lpszPassword,
    int dwLogonType,
    int dwLogonProvider,
    ref IntPtr phToken);

[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern int DuplicateToken(IntPtr hToken,
    int impersonationLevel,
    ref IntPtr hNewToken);

[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern bool RevertToSelf();

[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern bool CloseHandle(IntPtr handle);

private bool impersonateValidUser(String userName, String domain, String password) {
  WindowsIdentity tempWindowsIdentity;
  IntPtr token=IntPtr.Zero;
  IntPtr tokenDuplicate=IntPtr.Zero;

  if(RevertToSelf()) {
    if(LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
      LOGON32_PROVIDER_DEFAULT, ref token)!=0) {
        if(DuplicateToken(token, 2, ref tokenDuplicate)!=0) {
          tempWindowsIdentity=new WindowsIdentity(tokenDuplicate);
        impersonationContext=tempWindowsIdentity.Impersonate();
        if(impersonationContext!=null) {
          CloseHandle(token);
          CloseHandle(tokenDuplicate);
          return true;
        }
      }
    }
  }
  if(token!=IntPtr.Zero)
    CloseHandle(token);
  if(tokenDuplicate!=IntPtr.Zero)
    CloseHandle(tokenDuplicate);
  return false;
}

private void undoImpersonation() {
  impersonationContext.Undo();
}


回答2:

You can do this using impersonation. Here is a question and answers of how it is done.