[ASP.NET]在ASP.NET應用程式中模擬高權限使用者執行程式

[ASP.NET]在ASP.NET應用程式中模擬高權限使用者執行程式

今天在討論區看到這樣的問題

有時利用ASP.NET執行時,會遇到權限不足的問題

所以必需提高權限才能執行某些程式

小朱大大提供了一個不錯的參考網址

小弟就用一的實例教大家如何實現這個功能

ASP.NET(C#)

HighLevelUser.aspx

01 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="HighLevelUser.aspx.cs" Inherits="HighLevelUser" %>
02   
03 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04   
05 <html xmlns="http://www.w3.org/1999/xhtml" >
06 <head runat="server">
07     <title>HighLevelUser</title>
08 </head>
09 <body>
10     <form id="form1" runat="server">
11     <div>
12       
13     </div>
14     </form>
15 </body>
16 </html>

 

HighLevelUser.aspx.cs

001 using System;
002 using System.Data;
003 using System.Configuration;
004 using System.Collections;
005 using System.Web;
006 using System.Web.Security;
007 using System.Web.UI;
008 using System.Web.UI.WebControls;
009 using System.Web.UI.WebControls.WebParts;
010 using System.Web.UI.HtmlControls;
011 using System.Runtime.InteropServices;
012 using System.Security.Principal;
013 using System.IO;
014   
015 public partial class HighLevelUser : System.Web.UI.Page
016 {
017   
018     public const int LOGON32_LOGON_INTERACTIVE = 2;
019     public const int LOGON32_PROVIDER_DEFAULT = 0;
020   
021     WindowsImpersonationContext impersonationContext;
022   
023     [DllImport("advapi32.dll")]
024     public static extern int LogonUserA(String lpszUserName,
025         String lpszDomain,
026         String lpszPassword,
027         int dwLogonType,
028         int dwLogonProvider,
029         ref IntPtr phToken);
030     [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
031     public static extern int DuplicateToken(IntPtr hToken,
032         int impersonationLevel,
033         ref IntPtr hNewToken);
034   
035     [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
036     public static extern bool RevertToSelf();
037   
038     [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
039     public static extern bool CloseHandle(IntPtr handle);
040   
041     public void Page_Load(Object s, EventArgs e)
042     {
043         //使用者(ASPNET)
044         //下面這行在沒有高權限的使用者會產生,拒絕存取路徑 'e:\log.txt'。
045         //File.AppendAllText(@"e:\log.txt", "F6 Team");
046   
047         if (impersonateValidUser("Administrator", "localhost", "123456"))
048         {
049             Response.Write(string.Format("驗證成功,目前使用者:{0}<br/>", WindowsIdentity.GetCurrent().Name));
050   
051             //提高權限(Administrator),才能寫檔
052             File.AppendAllText(@"e:\log.txt", "F6 Team");
053   
054             //還原使用者
055             undoImpersonation();
056   
057             Response.Write(string.Format("作業完成,目前使用者:{0}<br/>", WindowsIdentity.GetCurrent().Name));
058         }
059         else
060         {
061             Response.Write(string.Format("驗證失敗,目前使用者:{0}<br/>", WindowsIdentity.GetCurrent().Name));
062         }
063     }
064   
065     private bool impersonateValidUser(String userName, String domain, String password)
066     {
067         WindowsIdentity tempWindowsIdentity;
068         IntPtr token = IntPtr.Zero;
069         IntPtr tokenDuplicate = IntPtr.Zero;
070   
071         if (RevertToSelf())
072         {
073             if (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
074                 LOGON32_PROVIDER_DEFAULT, ref token) != 0)
075             {
076                 if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
077                 {
078                     tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
079                     impersonationContext = tempWindowsIdentity.Impersonate();
080                     if (impersonationContext != null)
081                     {
082                         CloseHandle(token);
083                         CloseHandle(tokenDuplicate);
084                         return true;
085                     }
086                 }
087             }
088         }
089         if (token != IntPtr.Zero)
090             CloseHandle(token);
091         if (tokenDuplicate != IntPtr.Zero)
092             CloseHandle(tokenDuplicate);
093         return false;
094     }
095   
096     private void undoImpersonation()
097     {
098         impersonationContext.Undo();
099     }
100   
101   
102 }

 

執行結果:

沒有權限的結果

有權限的結果

參考網址:
http://www.blueshop.com.tw/board/show.asp?subcde=BRD20090224193357J4D&fumcde=FUM20041006161839LRJ
http://support.microsoft.com/default.aspx/kb/306158

推到 Twitter!
posted @ 2010-08-27 09:35  peterlee  阅读(290)  评论(0)    收藏  举报