【问题标题】:Access to share folder of another user C# WPF访问另一个用户 C# WPF 的共享文件夹
【发布时间】:2014-07-21 08:38:30
【问题描述】:

我有一些问题。我用 c# (WPF) 编写应用程序,我的应用程序需要当前登录到 PC 的用户的权限,并且应用程序启动它。现在我需要添加另一个我有登录/密码的用户的权限,但不删除当前登录的用户权限。 我需要这个将文件从 PC 复制到另一个用户的共享文件夹。 PC 在域中运行。我不能使用 LogOn 因为这个方法会删除当前登录到 PC 用户。

【问题讨论】:

  • 这必须授予登录到应用程序的权限。在那一刻,我不知道 UNC 路径(机器名称)。我只有通过和登录。在这个域中有一些 PC。我需要类似 LogonUser 模拟的东西,但这会删除当前登录的用户权限。
  • 现在我完全糊涂了,你不知道PC/Share?如果我理解正确的话。您要么需要冒充某人并让他们授予“您”对其共享的写权限,然后您就可以复制该文件。或者您需要模拟他们并让他们将文件从您共享的内容复制到他们的共享中。不得不说这看起来非常复杂,非常脆弱,根本不是一个好主意。
  • 我读到我可以为此使用 kerberos,但我找不到任何实施示例:/

标签: c# wpf dns directory share


【解决方案1】:

创建一个模拟类。

class UserImpersonation2:IDisposable
{
    [DllImport("advapi32.dll")]
    public static extern bool LogonUser(String lpszUserName,
        String lpszDomain,
        String lpszPassword,
        int dwLogonType,
        int dwLogonProvider,
        ref IntPtr phToken);

    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    public static extern bool CloseHandle(IntPtr handle);

    WindowsImpersonationContext wic;
    IntPtr tokenHandle;
    string _userName;
    string _domain;
    string _passWord;

    public UserImpersonation2(string userName, string domain, string passWord)
    {
        _userName = userName;
        _domain = domain;
        _passWord = passWord;
    }

    const int LOGON32_PROVIDER_DEFAULT = 0;
    const int LOGON32_LOGON_INTERACTIVE = 2;

    public bool ImpersonateValidUser()
    {
        bool returnValue = LogonUser(_userName, _domain, _passWord,
                LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
                ref tokenHandle);

        Console.WriteLine("LogonUser called.");

        if (false == returnValue)
        {
            int ret = Marshal.GetLastWin32Error();
            Console.WriteLine("LogonUser failed with error code : {0}", ret);
            return false;
        }

        Console.WriteLine("Did LogonUser Succeed? " + (returnValue ? "Yes" : "No"));
        Console.WriteLine("Value of Windows NT token: " + tokenHandle);

        // Check the identity.
        Console.WriteLine("Before impersonation: "
            + WindowsIdentity.GetCurrent().Name);
        // Use the token handle returned by LogonUser.
        WindowsIdentity newId = new WindowsIdentity(tokenHandle);
        wic = newId.Impersonate();

        // Check the identity.
        Console.WriteLine("After impersonation: "
            + WindowsIdentity.GetCurrent().Name);
        return true;
    }
    #region IDisposable Members
    public void Dispose()
    {
        if(wic!=null)
            wic.Undo();
        if (tokenHandle != IntPtr.Zero)
            CloseHandle(tokenHandle);

    }
    #endregion
}

然后使用它:

      const string file = @"\\machine\test\file.txt";

        using (UserImpersonation user = new UserImpersonation("user", "domain", "password"))
        {
            if (user.ImpersonateValidUser())
            {
                StreamReader reader = new StreamReader(file);
                Console.WriteLine(reader.ReadToEnd());
                reader.Close();
            }
        }

字体:https://msdn.microsoft.com/en-us/library/system.security.principal.windowsidentity.aspx

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-12
  • 2011-06-07
相关资源
最近更新 更多