【问题标题】:Cannot write on a mapped drive using impersonation无法使用模拟在映射驱动器上写入
【发布时间】:2011-03-10 04:07:10
【问题描述】:

基本上我和这篇帖子Accessing mapped drives when impersonating in 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);
}

【问题讨论】:

    标签: asp.net mapping impersonation drive


    【解决方案1】:

    我也无法让它工作。

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

    //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 组,并为这些用户在隐藏的共享驱动器上授予读/写权限。这使得维护更容易,因为我不必为每个用户创建映射驱动器。

    【讨论】:

    • @ Ed B:谢谢,我会试试看。不过,我有几个问题:1) ¿ 此解决方案是否意味着所有管理员都需要使用同一个帐户登录? 2)我不明白为什么你必须为每个用户创建映射驱动器;在我的示例中,管理员将使用他们的帐户登录,但是当要在磁盘上写入时,使用的用户名和密码来自特权帐户(从 Web.config 中检索)
    • 1.不,他们没有。如果用户已经使用 Windows 凭据登录到您的站点,您可以像我在上面所做的那样在目标机器上模拟登录用户的身份。 2. 好吧,我没有意识到有一个特权帐户。我什至无法使用已设置映射驱动器的帐户来使用映射驱动器方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-14
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 2011-02-09
    • 2019-03-06
    • 2019-07-20
    相关资源
    最近更新 更多