2010-06-30 51 views
1

基本上我运行相同的问题,因为这个帖子Accessing mapped drives when impersonating in ASP.NET不能使用模拟

我工作的一个传统网站映射驱动器上写,我需要让管理员来更改网站的标志,横幅,从桌面上的映像文件映射到服务器上的映射驱动器。

因此,他们的网站每当需要保存在驱动器上时都会使用模拟功能,并且工作得很好;但是我无法设法使其在测试环境或测试环境中工作。

¿有什么想法?我仔细检查过用户名和密码(代码没有指定域名),这不是问题。

下面是从代码的摘录,处理模拟:

public bool ImpersonateUser(String user, String password, String domain) 
{ 
    WindowsIdentity tempWindowsIdentity; 
    IntPtr token = IntPtr.Zero; 
    IntPtr tokenDuplicate = IntPtr.Zero; 

    if (RevertToSelf()) 
    { 
     if (LogonUserA(user, 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; 
       } 
      } 
     } 
    } 
    //... rest of the code 

并有-sanitized-测试:

if (impUtility.ImpersonateUser("user", "password", string.Empty)) 
{ 
    fu.SaveAs(@"C:\Images\" + imgName); 
} 

回答

0

我不能得到这工作的。

然后我意识到即使我可以实现它,也有一种更简单的方法。 我所做的就是在目标机器上共享文件夹,并且仅对将使用我的应用程序的用户授予读/写权限。

//Impersonate user to save file on server 
WindowsIdentity wi = (WindowsIdentity)User.Identity; 
WindowsImpersonationContext wic = null; 

try 
{ 
    wic = wi.Impersonate(); 
    if (wi.IsAuthenticated) 
     asyncFileUpload.SaveAs(location); 
} 
catch (Exception ex) 
{ 
    //Log Error or notify here 
    success = false; 
} 
finally 
{ 
    if (wic != null) 
     wic.Undo(); 
} 

我为用户创建了一个AD组,为这些隐藏的共享驱动器上的用户授予读/写权限。这使得维护更容易,因为我不必为每个用户创建映射的驱动器。

+0

@ Ed B:谢谢,我会试试看。我有几个问题: 1)¿此解决方案是否意味着所有管理员都需要使用同一个帐户登录? 2)我没有得到你为什么必须为每个用户创建映射驱动器;在我的示例中,管理员将使用他们的帐户登录,但是当即将在磁盘上写入时,使用的用户名和密码来自特权帐户(从Web.config中检索到) – 2010-06-30 15:29:03

+0

1.不是。如果用户已经使用Windows凭据登录到您的网站,则可以在目标机器上模拟登录用户的身份,就像我在上面做的一样。好吧,我没有意识到有一个特权帐户。我甚至无法使用映射驱动器方法来处理已经使用映射驱动器设置的帐户。 – 2010-06-30 21:58:27