我在模拟登录用户然后访问 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
如果我尝试在本地访问一个文件,其中评论说使用经过身份验证的用户的身份访问资源,它完全可以正常工作。但是,如果我尝试使用 \\ServerName\Share\FileName.txt 之类的 UNC 对某个文件服务器上的文件执行相同的操作,那么模拟帐户是否具有足够的权限并不重要。应用程序抛出一个异常,指出 ASP.NET 帐户没有足够的权限。
我还尝试使用非托管代码来执行模拟,然后它就可以工作了!本地文件或 UNC,没关系,就像一个魅力!
问题是你必须提供密码,因为它是我想检查的登录用户权限,所以我不能提供。
有谁知道为什么应用程序会这样?我需要设置一些配置设置吗?
