相关疑难解决方法(0)

使用WindowsImpersonationContext模拟当前用户访问网络驱动器

我需要从Web App访问远程驱动器.ASP.NET进程无法访问该驱动器,因此我想模拟当前用户的请求.

我看到了一些使用WindowsImpersonationContext的基本示例,并尝试了以下内容.

WindowsImpersonationContext impersonationContext = null;

try
{
    impersonationContext = ((WindowsIdentity)User.Identity).Impersonate();

    file.SaveAs(filePath);
}
finally
{
    if (impersonationContext != null)
    {
        impersonationContext.Undo();
    }
}
Run Code Online (Sandbox Code Playgroud)

我仍然得到访问被拒绝的例外.

我已经阅读了一些相关内容LogonUser,但据我所知,您需要一个用户名和密码才能从中获取凭据.我不打算以不同的特定用户身份登录,只是当前用户(谁应该有权访问文件存储),所以我认为我实际上并不需要LogonUserAPI.

有谁知道我错过了什么?上述代码应该有效吗?

我还要注意包括

<identity impersonate="true" />

不起作用,但包括

<identity impersonate="true" userName="myName" password="myPassword" />

web.config在让我,我见过几个人问这是为什么的问题,但我见过的任何解释.我想知道它是否与我的问题有关.

.net c# security impersonation remote-access

3
推荐指数
2
解决办法
1万
查看次数

标签 统计

.net ×1

c# ×1

impersonation ×1

remote-access ×1

security ×1