gpt4 book ai didi

c# - 如何模拟登录用户并访问 unc 文件夹?

转载 作者:太空宇宙 更新时间:2023-11-03 16:43:43 24 4
gpt4 key购买 nike

我在模拟登录用户然后访问 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,没关系,就像一个魅力!

问题是您必须提供密码,因为它是我想检查的登录用户权限,所以我无法提供。

有谁知道为什么应用程序会这样?我需要设置一些配置设置吗?

最佳答案

Web application runs with specific identity, this identity is based on a user account on the local machine or domain.

The application uses this identity when it accesses resources on disk or services.

If the account does not have rights to the resource, the web application will not be able to use the resource.

Impersonation is where the web application assumes a different identity from the default

Impersonation can be configured to be used for the web application on startup, by adding an tag to the web.config file. Impersonation can also be implemented dynamically in code, so that it can be turned on and off as needed.

来自 ASP.NET Identity and Impersonation Different Impersonation

因此请检查您的 web.config 和 IIS 配置并确保您模拟的是正确的用户。

关于c# - 如何模拟登录用户并访问 unc 文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6624439/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com