我在冒充登錄的用戶時遇到問題,然後訪問unc文件。我一直在使用此示例代碼嘗試:如何模擬登錄用戶並訪問unc文件夾?
using System.Security.Principal;
...
// Obtain the authenticated user's Identity
WindowsIdentity winId = (WindowsIdentity)HttpContext.Current.User.Identity;
WindowsImpersonationContext ctx = null;
try
{
// Start impersonating
ctx = winId.Impersonate();
// Now impersonating
// Access resources using the identity of the authenticated user
}
// Prevent exceptions from propagating
catch{}
finally
{
// Revert impersonation
if (ctx != null)
ctx.Undo();
}
// Back to running under the default ASP.NET process identity
如果我嘗試在評論說,使用經過驗證的用戶它的工作原理完全,因爲它應該的身份訪問資源訪問本地文件。但是,如果我嘗試使用UNC的某個文件服務器上的某個文件(如\\ ServerName \ Share \ FileName.txt)執行相同的操作,則模擬的帳戶擁有足夠的權限並不重要。應用程序拋出一個異常,說ASP.NET帳戶沒有足夠的權限。
我也嘗試使用非託管代碼來執行模擬,然後它工作!本地文件或UNC無關緊要,就像一個魅力!
問題是你必須提供密碼,因爲它是登錄用戶權限我想檢查我不能提供。
有誰知道爲什麼應用程序的行爲像這樣?是否需要設置一些配置設置?
所以爲了防止異常傳播你甚至沒有趕上並記錄他們!? cooool! –
您正在模擬ASP.NET帳戶...不是當前用戶帳戶。什麼是你的認證模式,你有指定模擬web.config? – 2GDev
@Davide:我認爲這是一個示例代碼...檢查這裏:http://stackoverflow.com/questions/4334665/steps-to-enable-double-hop-delegation-in-iis7-windows-2008,和在這裏http://stackoverflow.com/questions/983443/windowsidentity-winid-windowsidentityhttpcontext-current-user-identity-how-t並且這裏是來源:http://msdn.microsoft.com/en-us/library/ms998351的.aspx#paght000023_impersonatingbyusingwindowsidentity – 2GDev