微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

replace当前Windows用户与另一个用户运行EXE

说如果我build立一个Windows应用程序,从networking文件夹中读取文件。 networking折叠限制只有一个用户“fooUser”的访问。 该应用程序安装在networking上的多台机器上。

我需要用“fooUser”replace当前用户,以便能够通过代码访问networking文件夹中的文件

如何创build无限的canvas效果

InvisibleOperationException隐藏窗口时

强制.NET的Windows应用程序。 以英文forms显示数字

C#DriveInfo类导致Windows错误popup窗口

使连续运行的ac#应用程序

这是一个非常简单的模拟方案,可以让你成为任何人(假设你有相应的证书)。

这个班将为你做所有的繁重工作….

public class Impersonator : Idisposable { const int logoN32_PROVIDER_DEFAULT = 0; const int logoN32_logoN_INteraCTIVE = 2; [DllImport("advapi32.dll",SetLastError = true)] 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 extern static bool CloseHandle(IntPtr handle); private IntPtr token = IntPtr.Zero; private WindowsImpersonationContext impersonated; private readonly string _ErrMsg = ""; public bool IsImpersonating { get { return (token != IntPtr.Zero) && (impersonated != null); } } public string ErrMsg { get { return _ErrMsg; } } [PermissionSetAttribute(SecurityAction.Demand,Name = "FullTrust")] public Impersonator(string userName,string password,string domain) { StopImpersonating(); bool loggedOn = logonUser(userName,domain,password,logoN32_logoN_INteraCTIVE,logoN32_PROVIDER_DEFAULT,ref token); if (!loggedOn) { _ErrMsg = new System.ComponentModel.Win32Exception().Message; return; } WindowsIdentity identity = new WindowsIdentity(token); impersonated = identity.Impersonate(); } private void StopImpersonating() { if (impersonated != null) { impersonated.Undo(); impersonated = null; } if (token != IntPtr.Zero) { CloseHandle(token); token = IntPtr.Zero; } } public void dispose() { StopImpersonating(); } }

你可以像这样使用它;

using (Impersonator = new Impersonator(yourName,yourPassword,yourDomain)) { // Read files from network drives. // Other activities.... }

将模拟器放置在“使用”块中,或者在完成模拟任务时处置它,或者系统将继续无限地模拟,这将导致各种问题,这一点非常重要。

使用runas实用工具: http : //www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/runas.mspx? mfr=true

希望这可以帮助。

你可以检查这个问题logonUser和代表团会帮助你。

您可以将映射驱动器设置为使用“fooUser”凭据的文件夹共享。

虽然如果您有用户登录名/密码,您可以让您的代码使用模拟。 根据我对C#的Windows模拟的回答:

有关模拟代码,请参阅以下两个代码项目文章

http://www.codeproject.com/KB/cs/cpimpersonation1.aspx

http://www.codeproject.com/KB/cs/zetaimpersonator.aspx

以及他们基于的Microsoft知识库文章

http://support.microsoft.com/default.aspx?scid=kb;en-us;Q306158

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐