我试图在Windows服务中模拟域用户,并将该服务作为本地系统帐户登录.
到目前为止,我只能通过记录服务并使用用户凭据设置进程来实现此功能,如下所示.
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = CommandDetails.Command;
startInfo.WorkingDirectory = Settings.RoboCopyWorkingDirectory;
startInfo.Arguments = commandLine;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardOutput = true;
// Credentials
startInfo.Domain = ImperDomain;
startInfo.UserName = ImperUsername;
startInfo.Password = ImperPasswordSecure;
process = Process.Start(startInfo);
Run Code Online (Sandbox Code Playgroud)
我的目标是不在域用户中使用服务日志,而是将域帐户密码重置为本地系统.
当我使用本地系统时,我得到Access被拒绝
任何想法如何实现这一目标?
StackTace
Access is denied
at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start()
at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
at Ace.WindowsService.ProcessCmd.ProcessCommand.StartProcess(ProcessStartInfo startInfo) in
Run Code Online (Sandbox Code Playgroud)
我已经尝试将代码包装在下面列出的Impersonate代码中但没有成功.
冒充代码
public class Impersonation2 : IDisposable
{
private WindowsImpersonationContext _impersonatedUserContext;
// Declare signatures for Win32 LogonUser and …Run Code Online (Sandbox Code Playgroud) 背景:我有一个应用程序必须从网络驱动器上的文件读取(Z :)
这在我的办公室域中很有用,但它不能在站点上工作(在不同的域中).据我所知,域用户和网络驱动器的设置方式相同,但我无法访问客户域中的用户等.
当我无法访问网络驱动器时,我想我需要一个用户令牌.这就是我对用户进行压制的方式:
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
Run Code Online (Sandbox Code Playgroud)
...
const string userName = "USER";
const string pass = "PASS";
const string domainName = "VALIDDOMAIN.local" //tried with valid domain name and with null, same result
const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_LOGON_INTERACTIVE = 2;
IntPtr tokenHandle = new IntPtr(0);
bool returnValue = LogonUser(userName, domainName, pass,
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
ref tokenHandle); …Run Code Online (Sandbox Code Playgroud) 我有一个在不在域上的机器上运行的Windows窗体应用程序,它需要能够将文件从本地文件系统移动到UNC路径.我有该路径的用户名和密码.我想知道有没有办法直接执行这个net.exe命令?
理想情况下,我不必映射驱动器.
在C#中,如何在网络计算机上对用户进行身份验证?例如,我想从与网络连接的另一台机器在机器上testuser使用密码验证用户.例如,我在,我想验证与上.testpasswordEXAMPLEMACHINEEXAMPLEMACHINEMYMACHINEtestusertestpasswordEXAMPLEMACHINE
我尝试了以下但它一直告诉我,LDAP服务器不可用:
PrincipalContext context =
new PrincipalContext(ContextType.Domain, exampleMachineDomain);
return context.ValidateCredentials(username, password);
Run Code Online (Sandbox Code Playgroud) 是否有某种方法可以作为本地(非网络)用户进行身份验证,以便在.Net中通过网络复制文件?
net use不是一个选项,我似乎无法让LogonUser工作.
有任何想法吗?
[编辑]这是一些代码:
public class UserImpersonator : IDisposable
{
private WindowsImpersonationContext _impersonationContext;
private IntPtr _userHandle = IntPtr.Zero;
[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool LogonUser(
string lpszUsername,
string lpszDomain,
string lpszPassword,
int dwLogonType,
int dwLogonProvider,
out IntPtr phToken
);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool CloseHandle(IntPtr hHandle);
public UserImpersonator(string username, string password)
{
LogonUser(username, "", password, (int)LogonType.LOGON32_LOGON_NETWORK,
(int)LogonProvider.LOGON32_PROVIDER_DEFAULT, out _userHandle);
_impersonationContext = WindowsIdentity.Impersonate(_userHandle);
}
public void Dispose()
{
CloseHandle(_userHandle);
_impersonationContext.Undo();
} …Run Code Online (Sandbox Code Playgroud) 我正在开发一个使用UNC路径访问同一LAN中的远程文件的应用程序.问题是,当尝试访问时,它会抛出与Windows凭据相关的异常.
有没有办法将凭证添加到我的UNC路径?
谢谢
I need to download files from a server to a shared drive directory, creating the directory if it doesn't exist. There are a few things making this more complicated:
I attempt to impersonate, as so:
class Impersonation
{
const …Run Code Online (Sandbox Code Playgroud) 我有一个托管在 Windows Server 2008 R2 IIS 7.5 上的 Web 应用程序,需要访问另一台 Windows 计算机上的远程目录文件共享。我的网站在 User1 下运行,通过 UNC 路径访问远程文件共享需要 User2 的用户名/密码凭据。
在试图找到最好的方法来做到这一点时,我遇到了各种选择,但我并不真正理解其中的陷阱。
我们关心的问题之一是性能 - 我们不想打开/关闭或验证每个请求。到目前为止,我见过的许多解决方案都会在每次操作后处理连接。我认为我真正正在寻找的是一种持久映射网络驱动器的形式。
那么所有这些选项之间有什么区别呢?任何人都可以提供最佳实践是什么,以及示例代码(如果尚未在下面的帖子之一中提供)吗?
一些相关帖子:
http://forums.asp.net/t/1915751.aspx?C+Access+Network+Drive+Without+mapping+with+credentials
谢谢你!
我必须将一些文件从服务器复制到另一个服务器,我想使用File.Copy但我如何在那里指定文件的位置?并且还可以在 from 和 to 上设置密码标识。
有任何想法吗 ?
我需要能够在尝试在非域环境中的远程计算机上读取和写入文件时以编程方式进行身份验证.
当您在Windows RUN提示符中键入类似于\\ targetComputer\C $\targetFolder或\\ targetComputer\admin $的命令时,如果targetComputer不在域中,则系统将提示您输入用户名和密码.输入用户名和密码后,您就可以完全访问远程文件夹.
如何在C#中以编程方式完成此身份验证?
我试过了..
--Impersonation,但它似乎只在域环境中工作.
--CMDKEY.exe,但它似乎也只适用于域环境.
必须有一种方法可以做到这一点,但到目前为止,我一直没有运气地搜索高低.也许我只是在寻找错误的东西?我敢肯定我不是第一个有这个问题的人.任何帮助将不胜感激.
谢谢!
编辑:
我想我刚刚发现了一个不同的SO帖子来回答我的问题: 从具有凭据的远程非受信任域访问共享文件(UNC)
我现在将继续使用它,看看它在哪里.
谢谢!