2010-05-18 29 views
3

我們正在使用Windows網絡(AD正在使用)
我們有文件夾由用戶共享(僅限此用戶的訪問限制)用戶憑據已知 我需要在我的應用內訪問該共享。使用.NET中的不同用戶憑據對RW共享Windows文件夾的訪問

注意我讀過有關模擬但我可以做的就是打開新的用戶上下文整個應用程序(但我需要的是工作作爲當前登錄的用戶,只需訪問Windows的共享文件夾上代表其他用戶)

會有可能嗎?一塊代碼讚賞..

+0

你有任何運氣搞清楚了這一點的工作(注意,您需要System.Security.Principal +互操作,還需要添加一些API靜態方法)? – dredful 2010-06-23 18:12:48

+0

是的,我做到了。請看下面的答案 – Maciej 2010-06-24 12:21:52

回答

2

我終於管理,並作爲對我的傷害!
對於那些有興趣 - 請找樣品的方法做

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

    public bool ImpersonateUser(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; 
    } 
相關問題