![](/img/trans.png)
[英]How do i pass a variable value from Console application to windows form application in C# (visual studio)
[英]Killing a Windows process from a C# console application: how do I set permissions?
使用ASP.NET Web应用程序中的Process.Kill()
,我得到一个Win32Exception文本为“拒绝访问”。
搜索网络多次告诉我设置权限。 但是,我对Windows XP用户系统的了解并不十分充分,以至于不知道如何入门。
在引发异常时, Thread.CurrentPrincipal.Identity
具有以下可见属性:
WindowsIdentity.GetCurrent()
显示我以“ NAME \\ ASPNET”身份登录,但我认为这无关。
我需要做什么? 我可以以某种方式让线程以Windows用户身份登录吗?
我认为您使用的是正确的方式,“访问被拒绝”的问题是由于以ASPNET用户身份运行的ASP.NET进程具有有限的权限,这就是您遇到的错误。 您可以做的就是为您的Web应用程序设置即时消息。 您可以通过更改web.config或代码来实现。 有关模拟的更多信息,您可以在这里阅读
web.comfig非常简单,您需要在web.config的system.web部分中添加以下行
<identity impersonate="true" userName="domain\user" password="password" />
用户需要在服务器上具有管理员权限
如果您想在下面的代码中进行模拟,请参见以下示例:
...
WindowsImpersonationContext context = ImpersonateUser("domain", "user", "password");
// kill your process
context.Undo();
...
[DllImport("advapi32.dll")]
private static extern bool LogonUser(
String lpszUsername, String lpszDomain, String lpszPassword,
int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
[DllImport("advapi32.dll")]
private static extern bool DuplicateToken(
IntPtr ExistingTokenHandle, int ImpersonationLevel,
ref IntPtr DuplicateTokenHandle);
[DllImport("kernel32.dll")]
private static extern bool CloseHandle(IntPtr hObject);
private enum SecurityImpersonationLevel
{
SecurityAnonymous,
SecurityIdentification,
SecurityImpersonation,
SecurityDelegation
}
private enum LogonTypes
{
LOGON32_PROVIDER_DEFAULT=0,
LOGON32_LOGON_INTERACTIVE=2,
LOGON32_LOGON_NETWORK=3,
LOGON32_LOGON_BATCH=4,
LOGON32_LOGON_SERVICE=5,
LOGON32_LOGON_UNLOCK=7,
LOGON32_LOGON_NETWORK_CLEARTEXT=8,
LOGON32_LOGON_NEW_CREDENTIALS=9
}
public static WindowsImpersonationContext ImpersonateUser(string domain, string username, string password)
{
WindowsImpersonationContext result = null;
IntPtr existingTokenHandle = IntPtr.Zero;
IntPtr duplicateTokenHandle = IntPtr.Zero;
try
{
if (LogonUser(username, domain, password,
(int)LogonTypes.LOGON32_LOGON_NETWORK_CLEARTEXT, (int)LogonTypes.LOGON32_PROVIDER_DEFAULT,
ref existingTokenHandle))
{
if (DuplicateToken(existingTokenHandle,
(int)SecurityImpersonationLevel.SecurityImpersonation,
ref duplicateTokenHandle))
{
WindowsIdentity newId = new WindowsIdentity(duplicateTokenHandle);
result = newId.Impersonate();
}
}
}
finally
{
if (existingTokenHandle != IntPtr.Zero)
CloseHandle(existingTokenHandle);
if (duplicateTokenHandle != IntPtr.Zero)
CloseHandle(duplicateTokenHandle);
}
return result;
}
希望这会有所帮助,问候
您需要以具有足够特权的用户身份运行C#应用程序。
如果您不能用这样的特权来信任ASP AppPool(您不应该),则需要创建一个单独的服务,该服务在具有足够特权的帐户下运行,并在低特权应用程序和高特权服务之间具有协议,以实现以下目的:杀死进程。
不要在AppPool中冒充高特权用户。 您必须提供其密码,这样,您才能有效地将低特权帐户提升为高特权帐户,在所有情况下,如果损害了AppPool,就好像您在高特权下运行AppPool并没有完成任何隔离一样。
在Serge Gubenko的答案的基础上,这是模拟和杀死进程的代码(在他的回答中,他写了“ // kill your process”):
using (WindowsImpersonationContext context = ImpersonateUser("domain", "user", "password"))
{
Process[] proc = Process.GetProcessesByName("process name");
if (proc.Length > 0)
proc[0].Kill();
context.Undo();
}
您仍然需要在他的答案中使用其余代码。 请注意,如果您不属于域,则输入一个空字符串
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.