全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1751
推到 Plurk!
推到 Facebook!

關於

尚未結案
geliswu
一般會員


發表:28
回覆:15
積分:13
註冊:2004-10-06

發送簡訊給我
#1 引用回覆 回覆 發表時間:2006-11-21 00:13:46 IP:219.68.xxx.xxx 訂閱
請教各位高手~
小毅遇到一個難題,這個問題在目前的討論過的問題中搜尋不到
這個問題就是 我需要在一般Users登入Windows的權限下,模擬Administrator or PowerUser登入Windows,以便執行RegAsm.exe註冊一個元件,否則RegAam.exe總是會Rise一個Access Denied的訊息!
我將MSDN的範例程式碼如下:看起來似乎可以模擬其他使用者的權限,但是在這個模擬之下叫用RegAsm.exe註冊元件依然得到 登錄檔 Access Denied的訊息
程式碼如下,Sorry 很久沒登入忘了貼Source Code的時候是要用什麼標籤包起來才會字鄧排版~容易閱讀

using System;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Security.Permissions;
using System.Windows.Forms;
[assembly:SecurityPermissionAttribute(SecurityAction.RequestMinimum, UnmanagedCode=true)]
[assembly:PermissionSetAttribute(SecurityAction.RequestMinimum, Name = "FullTrust")]
public class ImpersonationDemo
{
[DllImport("advapi32.dll", SetLastError=true)]
public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
[DllImport("kernel32.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto)]
private unsafe static extern int FormatMessage(int dwFlags, ref IntPtr lpSource,
int dwMessageId, int dwLanguageId, ref String lpBuffer, int nSize, IntPtr *Arguments);
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public extern static bool CloseHandle(IntPtr handle);
[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public extern static bool DuplicateToken(IntPtr ExistingTokenHandle,
int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle);
static WindowsIdentity newId = null;
static WindowsImpersonationContext impersonatedUser = null;
// Test harness.
// If you incorporate this code into a DLL, be sure to demand FullTrust.
[PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
public static LogonUser(string domainName, string userName, string password)
{

try
{
const int LOGON32_PROVIDER_DEFAULT = 0;
//This parameter causes LogonUser to create a primary token.
const int LOGON32_LOGON_INTERACTIVE = 2;
tokenHandle = IntPtr.Zero;

// Call LogonUser to obtain a handle to an access token.
bool returnValue = LogonUser(userName, domainName, Console.ReadLine(),
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
ref tokenHandle);

if (false == returnValue)
{
int ret = Marshal.GetLastWin32Error();
MessageBox.Show("LogonUser failed with error code : {0}", ret);
throw new System.ComponentModel.Win32Exception(ret);
}
// Use the token handle returned by LogonUser.
newId = new WindowsIdentity(tokenHandle);
impersonatedUser = newId.Impersonate();
}
catch(Exception ex)
{
Console.WriteLine("Exception occurred. " ex.Message);
}
}


static IntPtr tokenHandle = new IntPtr(0);
static IntPtr dupeTokenHandle = new IntPtr(0);

public static void Undo()
{
impersonatedUser.Undo();
// Free the tokens.
if (tokenHandle != IntPtr.Zero)
CloseHandle(tokenHandle);
}
}對了!不知道版主有沒有遇過類似問題,希望能Share一下,先說聲謝謝~萬分的感謝
系統時間:2024-05-13 20:17:35
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!