1

基本上我遇到了与这篇文章在 ASP.NET 中模拟时访问映射驱动器相同的问题

我正在开发一个旧网站,我需要允许管理员将网站的徽标、横幅等从他们桌面上的图像文件更改为服务器上的映射驱动器。

因此,他们的网站在需要保存驱动器时使用模拟,并且运行良好;但是我无法让它在他们的测试环境和我的测试环境中工作。

有任何想法吗?我已经仔细检查了用户和密码(代码没有指定域),这不是问题。

以下是处理模拟的代码摘录:

public bool ImpersonateUser(String user, String password, String domain)
{
    WindowsIdentity tempWindowsIdentity;
    IntPtr token = IntPtr.Zero;
    IntPtr tokenDuplicate = IntPtr.Zero;

    if (RevertToSelf())
    {
        if (LogonUserA(user, 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;
                }
            }
        }
    }
    //... rest of the code

还有一个-sanitized-测试:

if (impUtility.ImpersonateUser("user", "password", string.Empty))
{
    fu.SaveAs(@"C:\Images\" + imgName);
}
4

1 回答 1

0

我也无法让它工作。

然后我意识到,即使我可以实现它,还有一种更简单的方法。我所做的是在目标机器上共享文件夹,并且只向将使用我的应用程序的用户授予读/写权限。

//Impersonate user to save file on server
WindowsIdentity wi = (WindowsIdentity)User.Identity;
WindowsImpersonationContext wic = null;

try
{
    wic = wi.Impersonate();
    if (wi.IsAuthenticated)
        asyncFileUpload.SaveAs(location);
}
catch (Exception ex)
{
    //Log Error or notify here
    success = false;
}
finally
{
    if (wic != null)
        wic.Undo();
}

我为用户创建了一个 AD 组,并为这些用户在隐藏的共享驱动器上授予读/写权限。这使得维护更容易,因为我不必为每个用户创建映射驱动器。

于 2010-06-30T14:32:11.223 回答