![](/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.