- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我使用 Windows 服务中的 CreateProcessAsUser 为当前事件用户启动应用程序。到目前为止,它适用于本地驱动器上的应用程序。
但是如果可执行文件存在于网络共享上,当我使用完整的服务器名称 (\myserver\path\app.exe) 时,服务会生成 5: ERROR_ACCESS_DENIED。如果我改用映射驱动器 (P:\path\app.exe),我还可以生成 2: ERROR_FILE_NOT_FOUND。
我可以从资源管理器中正常启动应用程序。听起来我确实无法获得正确的 token 副本,因为该服务无法在服务器上正确地模拟我。
我从各种帖子中尝试了 CreateProcessAsUser 的几种不同实现,但无济于事。这对我来说是全新的(迷幻的)东西,坦率地说,我迫不及待地想回到 .NET :) 我猜这里有问题:
DuplicateTokenEx(
hUserToken,
(Int32)MAXIMUM_ALLOWED,
ref sa,
(Int32)SECURITY_IMPERSONATION_LEVEL.SecurityIdentification,
(Int32)TOKEN_TYPE.TokenPrimary,
ref hUserTokenDup);
CreateEnvironmentBlock(ref pEnv, hUserTokenDup, true);
Int32 dwCreationFlags = NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE | CREATE_UNICODE_ENVIRONMENT;
PROCESS_INFORMATION pi;
STARTUPINFO si = new STARTUPINFO();
si.cb = Marshal.SizeOf(si);
si.lpDesktop = "winsta0\\default";
CreateProcessAsUser(hUserTokenDup, // client's access token
null, // file to execute
commandLine, // command line
ref sa, // pointer to process SECURITY_ATTRIBUTES
ref sa, // pointer to thread SECURITY_ATTRIBUTES
false, // handles are not inheritable
dwCreationFlags, // creation flags
pEnv, // pointer to new environment block
workingDirectory, // name of current directory
ref si, // pointer to STARTUPINFO structure
out pi); // receives information about new process
这是完整的示例代码,我想它可能会有用:
using System;
using System.Text;
using System.Security;
using System.Management;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Win32
{
public class Win32API
{
[StructLayout(LayoutKind.Sequential)]
struct SECURITY_ATTRIBUTES
{
public Int32 Length;
public IntPtr lpSecurityDescriptor;
public Boolean bInheritHandle;
}
enum TOKEN_TYPE
{
TokenPrimary = 1,
TokenImpersonation = 2
}
[StructLayout(LayoutKind.Sequential)]
struct STARTUPINFO
{
public Int32 cb;
public String lpReserved;
public String lpDesktop;
public String lpTitle;
public UInt32 dwX;
public UInt32 dwY;
public UInt32 dwXSize;
public UInt32 dwYSize;
public UInt32 dwXCountChars;
public UInt32 dwYCountChars;
public UInt32 dwFillAttribute;
public UInt32 dwFlags;
public short wShowWindow;
public short cbReserved2;
public IntPtr lpReserved2;
public IntPtr hStdInput;
public IntPtr hStdOutput;
public IntPtr hStdError;
}
[StructLayout(LayoutKind.Sequential)]
struct PROCESS_INFORMATION
{
public IntPtr hProcess;
public IntPtr hThread;
public UInt32 dwProcessId;
public UInt32 dwThreadId;
}
enum SECURITY_IMPERSONATION_LEVEL
{
SecurityAnonymous = 0,
SecurityIdentification = 1,
SecurityImpersonation = 2,
SecurityDelegation = 3,
}
const UInt32 MAXIMUM_ALLOWED = 0x2000000;
const Int32 CREATE_UNICODE_ENVIRONMENT = 0x00000400;
const Int32 NORMAL_PRIORITY_CLASS = 0x20;
const Int32 CREATE_NEW_CONSOLE = 0x00000010;
[DllImport("kernel32.dll", SetLastError = true)]
static extern Boolean CloseHandle(IntPtr hSnapshot);
[DllImport("kernel32.dll")]
public static extern UInt32 WTSGetActiveConsoleSessionId();
[DllImport("Wtsapi32.dll")]
static extern UInt32 WTSQueryUserToken(UInt32 SessionId, ref IntPtr phToken);
[DllImport("advapi32.dll", EntryPoint = "CreateProcessAsUser", SetLastError = true, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
extern static Boolean CreateProcessAsUser(
IntPtr hToken,
String lpApplicationName,
String lpCommandLine,
ref SECURITY_ATTRIBUTES lpProcessAttributes,
ref SECURITY_ATTRIBUTES lpThreadAttributes,
Boolean bInheritHandle,
Int32 dwCreationFlags,
IntPtr lpEnvironment,
String lpCurrentDirectory,
ref STARTUPINFO lpStartupInfo,
out PROCESS_INFORMATION lpProcessInformation);
[DllImport("advapi32.dll", EntryPoint = "DuplicateTokenEx")]
extern static Boolean DuplicateTokenEx(
IntPtr ExistingTokenHandle,
UInt32 dwDesiredAccess,
ref SECURITY_ATTRIBUTES lpThreadAttributes,
Int32 TokenType,
Int32 ImpersonationLevel,
ref IntPtr DuplicateTokenHandle);
[DllImport("userenv.dll", SetLastError = true)]
static extern Boolean CreateEnvironmentBlock(
ref IntPtr lpEnvironment,
IntPtr hToken,
Boolean bInherit);
[DllImport("userenv.dll", SetLastError = true)]
static extern Boolean DestroyEnvironmentBlock(IntPtr lpEnvironment);
/// <summary>
/// Creates the process in the interactive desktop with credentials of the logged in user.
/// </summary>
public static Boolean CreateProcessAsUser(String commandLine, String workingDirectory, out StringBuilder output)
{
Boolean processStarted = false;
output = new StringBuilder();
try
{
UInt32 dwSessionId = WTSGetActiveConsoleSessionId();
output.AppendLine(String.Format("Active console session Id: {0}", dwSessionId));
IntPtr hUserToken = IntPtr.Zero;
WTSQueryUserToken(dwSessionId, ref hUserToken);
SECURITY_ATTRIBUTES sa = new SECURITY_ATTRIBUTES();
sa.Length = Marshal.SizeOf(sa);
IntPtr hUserTokenDup = IntPtr.Zero;
DuplicateTokenEx(
hUserToken,
(Int32)MAXIMUM_ALLOWED,
ref sa,
(Int32)SECURITY_IMPERSONATION_LEVEL.SecurityIdentification,
(Int32)TOKEN_TYPE.TokenPrimary,
ref hUserTokenDup);
if (hUserTokenDup != IntPtr.Zero)
{
output.AppendLine(String.Format("DuplicateTokenEx() OK (hToken: {0})", hUserTokenDup));
Int32 dwCreationFlags = NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE;
IntPtr pEnv = IntPtr.Zero;
if (CreateEnvironmentBlock(ref pEnv, hUserTokenDup, true))
{
dwCreationFlags |= CREATE_UNICODE_ENVIRONMENT;
output.AppendLine(String.Format("CreateEnvironmentBlock() success."));
}
else
{
output.AppendLine(String.Format("CreateEnvironmentBlock() FAILED (Last Error: {0})", Marshal.GetLastWin32Error()));
pEnv = IntPtr.Zero;
}
// Launch the process in the client's logon session.
PROCESS_INFORMATION pi;
STARTUPINFO si = new STARTUPINFO();
si.cb = Marshal.SizeOf(si);
si.lpDesktop = "winsta0\\default";
output.AppendLine(String.Format("CreateProcess (Path:{0}, CurrDir:{1})", commandLine, workingDirectory));
if (CreateProcessAsUser(hUserTokenDup, // client's access token
null, // file to execute
commandLine, // command line
ref sa, // pointer to process SECURITY_ATTRIBUTES
ref sa, // pointer to thread SECURITY_ATTRIBUTES
false, // handles are not inheritable
dwCreationFlags, // creation flags
pEnv, // pointer to new environment block
workingDirectory, // name of current directory
ref si, // pointer to STARTUPINFO structure
out pi // receives information about new process
))
{
processStarted = true;
output.AppendLine(String.Format("CreateProcessAsUser() OK (PID: {0})", pi.dwProcessId));
}
else
{
output.AppendLine(String.Format("CreateProcessAsUser() failed (Last Error: {0})", Marshal.GetLastWin32Error()));
}
if (DestroyEnvironmentBlock(pEnv))
{
output.AppendLine("DestroyEnvironmentBlock: Success");
}
else
{
output.AppendLine(String.Format("DestroyEnvironmentBlock() failed (Last Error: {0})", Marshal.GetLastWin32Error()));
}
}
else
{
output.AppendLine(String.Format("DuplicateTokenEx() failed (Last Error: {0})", Marshal.GetLastWin32Error()));
}
CloseHandle(hUserTokenDup);
CloseHandle(hUserToken);
}
catch (Exception ex)
{
output.AppendLine("Exception occurred: " + ex.Message);
}
return processStarted;
}
}
}
它非常适合像这样的本地可执行文件:
StringBuilder result = new StringBuilder();
Win32API.CreateProcessAsUser(@"C:\Windows\notepad.exe", @"C:\Windows\", out result);
我的问题:需要调整什么才能使用重复 token 正确访问网络共享?
最佳答案
当您将此命令用于允许 guest 访问的共享(即无用户名/密码)时,该命令可以正常工作,但是当您将它用于需要身份验证才能使用的共享时,它不起作用。
UI 调用涉及重定向器,它自动建立与执行所需的远程服务器的连接。
请注意,一个解决方法,但不是真正的解决方案是使用基于 cmd
的中继来获取可执行文件,因此对于命令行,您可以将其设置为:
CreateProcessAsUser(@"cmd /c ""start \\server\share\binary.exe""", @"C:\Windows", out result);
然后将 startupinfo 更改为 SW_HIDE
cmd
窗口使用:
si.cb = Marshal.SizeOf(si);
si.lpDesktop = "winsta0\\default";
si.dwFlags = 0x1; // STARTF_USESHOWWINDOW
si.wShowWindow = 0; // SW_HIDE
cmd
调用是在启动命令之前完全进入用户环境的一些垫片 - 这将利用所有凭据来访问服务器。
请注意,您可能需要一些逻辑来防止直接调用的应用程序出现 SW_HIDE
(例如,在命令行字符串的开头检查 cmd?)
关于c# - CreateProcessAsUser : Service gets "5: Access Denied" trying to access network share,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14463556/
我正在尝试编写 Access Access 数据库的脚本,以便在命令行上使用。 Access 数据库使用工作组文件进行保护。 Dim oApp, sWGF,myWS Set sApp = Create
我有一个包含数据表的表格。我希望用户能够选择多行,单击按钮并运行一些 sql 查询并对这些行执行一些工作。 查看我的 VBA 代码,我发现如何使用 CurrentRecord 属性 Access 最后
如果我在某个网络位置有 Microsoft Access 2007 数据库,那么可以使用该数据库的客户端计算机的数量是否有限制?客户端不会安装 Access,而是使用 Access Runtime 2
我正在开发一个注册系统。但我收到此错误:You tried to execute a query that does not include the specified expression.. 我正
我有一个产品设计为使用 MS Access 文件作为数据库的桌面产品。 现在,一些用户需要将它安装在几台 PC(比如说 2 或 3 台)上并共享数据库。 我想将 MS Access 文件放在共享文件夹
我接手了一个旧的软件项目,该项目使用 MS Access 数据库来存储数据。但是数据库不会在 Access 中打开,如下所示: "You do not have the necessary permi
我有一个文件夹,里面装满了 100 多个 Access97 文件。我需要将它们全部更新到 Access2003。 我可以手动完成,但使用 VBA 可能会快很多。 有没有人会有一个片段可以做到这一点?或
我正在通过 SQL Server 迁移助手 (SSMA) 将数据从 Access 数据库迁移到 SQL Server。 Access 应用程序将继续与转换为链接表的本地表一起使用。 一个连续的表单在加
我正在通过 SQL Server 迁移助手 (SSMA) 将数据从 Access 数据库迁移到 SQL Server。 Access 应用程序将继续与转换为链接表的本地表一起使用。 一个连续的表单在加
我的公司用 Visual Basic 6 开发了一个应用程序。 该应用程序通过 ODBC 数据源使用 Access 数据库。 Access 数据库是一个扩展名为“.mdb”的文件。 在以下环境中运行应
我一直在尝试让 Microsoft Access 从主 Access 窗口中“退出”,以便我可以隐藏 Access 窗口并仅在桌面上显示表单,以便可以轻松地将其放置在其他应用程序旁边。 起初我发现了一
我想在 access 2010 中使用 access 2000 和 2003 数据库。由于我不想检查一切是否手动工作,我正在寻找一种工具来分析 VBA 代码以查找使用 access 2010 发生的错
所以我有一个 Excel 工作簿,其中有一个很好的 shaperange 对象的全局 map 。通过一些非常简单的代码,我可以更改颜色、将国家/地区集合分组和取消分组为数组等......并且效果非常好
我们希望有大约 35-40 人通过共享驱动器上的脚本写入 Access 数据库。这些指标分解为他们需要每小时写大约 3-7 次。 Access 会支持这一点而不会对我产生影响吗? 是的,我很乐意将其用
我正在寻找一种使用 VBA 代码从外部数据库文件中删除 VBA 模块的方法。名为“myfile.accdb”的外部文件有一个名为“mod1”的模块,我希望能够在单独的项目中使用 VBA 代码删除该模块
我在 Access 2003 数据库(在 Access 2007 中开发)中有三个表单,它们处于父级 -> 子级 -> 孙子级关系中。在子窗体的 'Form_Load' 子窗体中,我设置了孙子窗体的一
MS Access 2007 存在拒绝在设计模式下显示表单的问题。我可以看到表单的代码(如果我查看显示表单的按钮的事件属性),但我看不到作为 GUI 布局的表单。而且,当我尝试从应用程序的主窗口调用此
我编写了代码,使用 Excel 中的下拉列表提供的标准将两个表连接起来,然后将数据返回到电子表格上的特定位置(工作表上已经有标题)。 这在我的机器上和其他机器上使用 MS Access 的机器上都可以
我正在开始构建一个应用程序,该应用程序从给定的根路径开始遍历文件夹结构,并将所有找到的 Access 1997 .mdb 文件转换为较新的 Access 2007/2010 .accdb 格式。但是,
我有一个表单和一个按钮。我想通过单击按钮打开另一个表单,并将参数从父表单传递到子表单(子表单的 RecordSource 有参数)。我该怎么做? 最佳答案 您可以通过引用表单的对象来引用调用表单的任何
我是一名优秀的程序员,十分优秀!