Read file from network location with credentials in asp.net code

[亡魂溺海] 提交于 2019-12-24 03:39:09

问题


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.



来源:https://stackoverflow.com/questions/13919975/read-file-from-network-location-with-credentials-in-asp-net-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!