【问题标题】:Read file from network location with credentials in asp.net code使用 asp.net 代码中的凭据从网络位置读取文件
【发布时间】:2012-12-04 21:24:40
【问题描述】:

我想从网络上存在的文件夹中读取文件。

当我尝试手动访问此文件夹时(通过运行命令提供类似 \\ABCServer\Documents 的路径),它会询问我的凭据(用户名和密码)。提供正确的凭据后,我就可以访问/读取文件了。

当我尝试从 ASP.NET 中的 C# 代码中读取相同的文件时,它给了我一个错误:

登录失败:用户名未知或密码错误

如何在读取文件时通过 C# 代码传递凭据?

以下是我正在使用的部分代码:

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

注意:

  • 代码运行良好
  • 我正在使用带有 C# 的 ASP.NET 4.0
  • IIS 版本为 7.5

【问题讨论】:

  • 听起来你可能想看看使用 Impersonation .. 如果是这样,请查看带有链接 platinumdogs.me/2008/10/30/… 的这个 stackoverflow 帖子

标签: c# asp.net iis-7 io asp.net-4.0


【解决方案1】:

这是来自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();
}

【讨论】:

  • LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token)!=0 给我错误代码“1326”,当我手动输入相同的凭据时,它工作正常。
  • 我讨厌这样说,但它在我的机器上运行良好。你确定域名是对的吗?我用好的密码和坏的密码都试过了,用好的密码,用坏的密码失败。
  • 感谢 weloytty 的编辑,顺便说一句,你以前在 AOL 工作过吗,我似乎记得我在那里时有一个 Bill Loytty(虽然我认为它的拼写不同)
  • 首先感谢您的回复。文件所在的服务器是其他域的一部分,例如 - 如果我的系统是 DomainA 的一部分并且我需要的文件存在于属于 DomainB 的系统中.我通过运行命令和浏览器成功访问了这些文件
  • 您是在域中发送,还是为用户使用“\\domain\user”,我会尝试两者,抱歉我现在没有单独的域来测试。跨度>
【解决方案2】:

您可以使用模拟来做到这一点。 Here 是一个关于如何完成的问答。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多