我需要能够在非域环境中尝试在远程计算机上读取和写入文件时以编程方式进行身份validation。
当您在类似于\\ targetComputer \ C $ \ targetFolder或\\ targetComputer \ admin $(其中targetComputer不在域中)的Windows RUN提示符中input命令时,系统将提示您input用户名和密码。 一旦你input用户名和密码,你可以完全访问远程文件夹。
我怎样才能在C#中以编程方式完成此身份validation?
我试过了..
– 人员,但似乎只能在域环境中工作。
–CMDKEY.exe,但它似乎也只能在域环境中工作。
必须有办法做到这一点,但我迄今为止没有运气search高和低。 也许我只是在寻找错误的东西? 我确定我不是第一个有这个问题的人。 任何帮助将不胜感激。
谢谢!
编辑:
我想我只是发现了一个不同的SOpost,回答我的问题: 使用凭据从远程,不受信任的域访问共享文件(UNC)
我现在就要处理这个问题,看看它到底在哪里。
谢谢!
冒充与对等/局域网络一起工作。 我有一些默认的“工作组”的一些机器的典型家庭网络和一些如果我记得在安装时做一个名字。
这里是我从我的IIS服务器应用程序访问我的另一台计算机上的文件的代码(无需在两台计算机上拥有相同的用户和密码,从某处复制并修改以供我使用):
using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.Security.Principal; using System.ComponentModel; /// <summary> /// Class to impersonate another user. Requires user, pass and domain/computername /// All code run after impersonationuser has been run will run as this user. /// Remember to Dispose() afterwards. /// </summary> public class ImpersonateUser:IDisposable { private WindowsImpersonationContext LastContext = null; private IntPtr LastUserHandle = IntPtr.Zero; #region User Impersonation api [DllImport("advapi32.dll", SetLastError = true)] public static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, out IntPtr phToken); [DllImport("advapi32.dll", SetLastError = true)] public static extern bool ImpersonateLoggedOnUser(int Token); [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern bool DuplicateToken(IntPtr token, int impersonationLevel, ref IntPtr duplication); [DllImport("kernel32.dll")] public static extern Boolean CloseHandle(IntPtr hObject); public const int LOGON32_PROVIDER_DEFAULT = 0; public const int LOGON32_PROVIDER_WINNT35 = 1; public const int LOGON32_LOGON_INTERACTIVE = 2; public const int LOGON32_LOGON_NETWORK = 3; public const int LOGON32_LOGON_BATCH = 4; public const int LOGON32_LOGON_SERVICE = 5; public const int LOGON32_LOGON_UNLOCK = 7; public const int LOGON32_LOGON_NETWORK_CLEARTEXT = 8;// Win2K or higher public const int LOGON32_LOGON_NEW_CREDENTIALS = 9;// Win2K or higher #endregion public ImpersonateUser(string username, string domainOrComputerName, string password, int nm = LOGON32_LOGON_NETWORK) { IntPtr userToken = IntPtr.Zero; IntPtr userTokenDuplication = IntPtr.Zero; bool loggedOn = false; if (domainOrComputerName == null) domainOrComputerName = Environment.UserDomainName; if (domainOrComputerName.ToLower() == "nt authority") { loggedOn = LogonUser(username, domainOrComputerName, password, LOGON32_LOGON_SERVICE, LOGON32_PROVIDER_DEFAULT, out userToken); } else { loggedOn = LogonUser(username, domainOrComputerName, password, nm, LOGON32_PROVIDER_DEFAULT, out userToken); } WindowsImpersonationContext _impersonationContext = null; if (loggedOn) { try { // Create a duplication of the usertoken, this is a solution // for the known bug that is published under KB article Q319615. if (DuplicateToken(userToken, 2, ref userTokenDuplication)) { // Create windows identity from the token and impersonate the user. WindowsIdentity identity = new WindowsIdentity(userTokenDuplication); _impersonationContext = identity.Impersonate(); } else { // Token duplication failed! // Use the default ctor overload // that will use Mashal.GetLastWin32Error(); // to create the exceptions details. throw new Win32Exception(); } } finally { // Close usertoken handle duplication when created. if (!userTokenDuplication.Equals(IntPtr.Zero)) { // Closes the handle of the user. CloseHandle(userTokenDuplication); userTokenDuplication = IntPtr.Zero; } // Close usertoken handle when created. if (!userToken.Equals(IntPtr.Zero)) { // Closes the handle of the user. CloseHandle(userToken); userToken = IntPtr.Zero; } } } else { // Logon failed! // Use the default ctor overload that // will use Mashal.GetLastWin32Error(); // to create the exceptions details. throw new Win32Exception(); } if (LastContext == null) LastContext = _impersonationContext; } public void Dispose() { LastContext.Undo(); LastContext.Dispose(); } }
我经过一番尝试后发现的具体代码是这样的:
using (var impersonation = new ImpersonateUser("OtherMachineUser", "OtherMachineName", "Password", LOGON32_LOGON_NEW_CREDENTIALS)) { var files = System.IO.Directory.GetFiles("\\OtherMachineName\fileshare"); }