通过ASP.net程序创建域帐户故障(2)
点击次数:14 次 发布日期:2008-11-26 14:07:17 作者:源代码网
|
源代码网推荐 源代码网推荐 <%@ Page Language="VB" %> 源代码网推荐 <%@ Import Namespace = "System.Web" %> 源代码网推荐 <%@ Import Namespace = "System.Web.Security" %> 源代码网推荐 <%@ Import Namespace = "System.Security.Principal" %> 源代码网推荐 <%@ Import Namespace = "System.Runtime.InteropServices" %> 源代码网推荐 <script runat=server> 源代码网推荐 Dim LOGON32_LOGON_INTERACTIVE As Integer = 2 源代码网推荐 Dim LOGON32_PROVIDER_DEFAULT As Integer = 0 源代码网推荐 Dim impersonationContext As WindowsImpersonationContext 源代码网推荐 Declare Function LogonUserA Lib "advapi32.dll" (ByVal lpszUsername As String, _ 源代码网推荐 ByVal lpszDomain As String, _ 源代码网推荐 ByVal lpszPassword As String, _ 源代码网推荐 ByVal dwLogonType As Integer, _ 源代码网推荐 ByVal dwLogonProvider As Integer, _ 源代码网推荐 ByRef phToken As IntPtr) As Integer 源代码网推荐 Declare Auto Function DuplicateToken Lib "advapi32.dll" ( _ 源代码网推荐 ByVal ExistingTokenHandle As IntPtr, _ 源代码网推荐 ByVal ImpersonationLevel As Integer, _ 源代码网推荐 ByRef DuplicateTokenHandle As IntPtr) As Integer 源代码网推荐 Declare Auto Function RevertToSelf Lib "advapi32.dll" () As Long 源代码网推荐 Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Long 源代码网推荐 Public Sub Page_Load(ByVal s As Object, ByVal e As EventArgs) 源代码网推荐 If impersonateValidUser("username", "domain", "password") Then 源代码网推荐 "Insert your code that runs under the security context of a specific user here. 源代码网推荐 undoImpersonation() 源代码网推荐 Else 源代码网推荐 "Your impersonation failed. Therefore, include a fail-safe mechanism here. 源代码网推荐 End If 源代码网推荐 End Sub 源代码网推荐 Private Function impersonateValidUser(ByVal userName As String, _ 源代码网推荐 ByVal domain As String, ByVal password As String) As Boolean 源代码网推荐 Dim tempWindowsIdentity As WindowsIdentity 源代码网推荐 Dim token As IntPtr = IntPtr.Zero 源代码网推荐 Dim tokenDuplicate As IntPtr = IntPtr.Zero 源代码网推荐 impersonateValidUser = False 源代码网推荐 If RevertToSelf() Then 源代码网推荐 If LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE, 源代码网推荐 LOGON32_PROVIDER_DEFAULT, token) <> 0 Then 源代码网推荐 If DuplicateToken(token, 2, tokenDuplicate) <> 0 Then 源代码网推荐 tempWindowsIdentity = New WindowsIdentity(tokenDuplicate) 源代码网推荐 impersonationContext = tempWindowsIdentity.Impersonate() 源代码网推荐 If Not impersonationContext Is Nothing Then 源代码网推荐 impersonateValidUser = True 源代码网推荐 End If 源代码网推荐 End If 源代码网推荐 End If 源代码网推荐 End If 源代码网推荐 If Not tokenDuplicate.Equals(IntPtr.Zero) Then 源代码网推荐 CloseHandle(tokenDuplicate) 源代码网推荐 End If 源代码网推荐 If Not token.Equals(IntPtr.Zero) Then 源代码网推荐 CloseHandle(token) 源代码网推荐 End If 源代码网推荐 End Function 源代码网推荐 Private Sub undoImpersonation() 源代码网推荐 impersonationContext.Undo() 源代码网推荐 End Sub 源代码网推荐 </script> 源代码网推荐 源代码网推荐 Visual C# .NET 源代码网推荐 源代码网推荐 <%@ Page Language="C#"%> 源代码网推荐 <%@ Import Namespace = "System.Web" %> 源代码网推荐 <%@ Import Namespace = "System.Web.Security" %> 源代码网推荐 <%@ Import Namespace = "System.Security.Principal" %> 源代码网推荐 <%@ Import Namespace = "System.Runtime.InteropServices" %> 源代码网推荐 <script runat=server> 源代码网推荐 public const int LOGON32_LOGON_INTERACTIVE = 2; 源代码网推荐 public const int LOGON32_PROVIDER_DEFAULT = 0; 源代码网推荐 WindowsImpersonationContext impersonationContext; 源代码网推荐 [DllImport("advapi32.dll")] 源代码网推荐 public static extern int LogonUserA(String lpszUserName, 源代码网推荐 String lpszDomain, 源代码网推荐 String lpszPassword, 源代码网推荐 int dwLogonType, 源代码网推荐 int dwLogonProvider, 源代码网推荐 ref IntPtr phToken); 源代码网推荐 [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)] 源代码网推荐 public static extern int DuplicateToken(IntPtr hToken, 源代码网推荐 int impersonationLevel, 源代码网推荐 ref IntPtr hNewToken); 源代码网推荐 [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)] 源代码网推荐 public static extern bool RevertToSelf(); 源代码网推荐 [DllImport("kernel32.dll", CharSet=CharSet.Auto)] 源代码网推荐 public static extern bool CloseHandle(IntPtr handle); 源代码网推荐 public void Page_Load(Object s, EventArgs e) 源代码网推荐 { 源代码网推荐 if(impersonateValidUser("username", "domain", "password")) 源代码网推荐 { 源代码网推荐 //Insert your code that runs under the security context of a specific user here. 源代码网推荐 undoImpersonation(); 源代码网推荐 } 源代码网推荐 else 源代码网推荐 { 源代码网推荐 //Your impersonation failed. Therefore, include a fail-safe mechanism here. 源代码网推荐 } 源代码网推荐 } 源代码网推荐 private bool impersonateValidUser(String userName, String domain, String password) 源代码网推荐 { 源代码网推荐 WindowsIdentity tempWindowsIdentity; 源代码网推荐 IntPtr token = IntPtr.Zero; 源代码网推荐 IntPtr tokenDuplicate = IntPtr.Zero; 源代码网推荐 if(RevertToSelf()) 源代码网推荐 { 源代码网推荐 if(LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE, 源代码网推荐 LOGON32_PROVIDER_DEFAULT, ref token) != 0) 源代码网推荐 { 源代码网推荐 if(DuplicateToken(token, 2, ref tokenDuplicate) != 0) 源代码网推荐 { 源代码网推荐 tempWindowsIdentity = new WindowsIdentity(tokenDuplicate); 源代码网推荐 impersonationContext = tempWindowsIdentity.Impersonate(); 源代码网推荐 if (impersonationContext != null) 源代码网推荐 { 源代码网推荐 CloseHandle(token); 源代码网推荐 CloseHandle(tokenDuplicate); 源代码网推荐 return true; 源代码网推荐 } 源代码网推荐 } 源代码网推荐 } 源代码网推荐 } 源代码网推荐 if(token!= IntPtr.Zero) 源代码网推荐 CloseHandle(token); 源代码网推荐 if(tokenDuplicate!=IntPtr.Zero) 源代码网推荐 CloseHandle(tokenDuplicate); 源代码网推荐 return false; 源代码网推荐 } 源代码网推荐 private void undoImpersonation() 源代码网推荐 { 源代码网推荐 impersonationContext.Undo(); 源代码网推荐 } 源代码网推荐 </script> 源代码网推荐 源代码网推荐 Visual J# .NET 源代码网推荐 源代码网推荐 <%@ Page language="VJ#" %> 源代码网推荐 <%@ Import Namespace="System.Web" %> 源代码网推荐 <%@ Import Namespace="System.Web.Security" %> 源代码网推荐 <%@ Import Namespace="System.Security.Principal" %> 源代码网推荐 <%@ Import Namespace="System.Runtime.InteropServices" %> 源代码网推荐 <script runat=server> 源代码网推荐 public static int LOGON32_LOGON_INTERACTIVE = 2; 源代码网推荐 public static int LOGON32_PROVIDER_DEFAULT = 0; 源代码网推荐 WindowsImpersonationContext impersonationContext; 源代码网推荐 /** @attribute DllImport("advapi32.dll") */ 源代码网推荐 public static native int LogonUserA(String lpszUserName, 源代码网推荐 String lpszDomain, 源代码网推荐 String lpszPassword, 源代码网推荐 int dwLogonType, 源代码网推荐 int dwLogonProvider, 源代码网推荐 System.IntPtr[] phToken); 源代码网推荐 /** @attribute DllImport("advapi32.dll", 源代码网推荐 CharSet=CharSet.Auto, SetLastError=true) */ 源代码网推荐 public static native int DuplicateToken(System.IntPtr hToken, 源代码网推荐 int impersonationLevel, 源代码网推荐 System.IntPtr[] hNewToken); 源代码网推荐 /** @attribute DllImport("kernel32.dll",CharSet=CharSet.Auto) */ 源代码网推荐 public static native boolean CloseHandle(System.IntPtr[] handle); 源代码网推荐 /** @attribute DllImport("advapi32.dll", 源代码网推荐 CharSet=CharSet.Auto,SetLastError=true) */ 源代码网推荐 public static native boolean RevertToSelf(); 源代码网推荐 public void Page_Load(Object s, System.EventArgs e) 源代码网推荐 { 源代码网推荐 if(impersonateValidUser("username", "domain", " password")) 源代码网推荐 { 源代码网推荐 //Insert your code that runs under the security context of a specific user here. 源代码网推荐 undoImpersonation(); 源代码网推荐 } 源代码网推荐 else 源代码网推荐 { 源代码网推荐 //Your impersonation failed. Therefore, include a fail-safe mechanism here. 源代码网推荐 } 源代码网推荐 } 源代码网推荐 private boolean impersonateValidUser(String userName, String domain, String password) 源代码网推荐 { 源代码网推荐 WindowsIdentity tempWindowsIdentity; 源代码网推荐 System.IntPtr[] token = new System.IntPtr[1]; 源代码网推荐 System.IntPtr[] tokenDuplicate = new System.IntPtr[1]; 源代码网推荐 if(RevertToSelf()) 源代码网推荐 { 源代码网推荐 if(LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE, 源代码网推荐 LOGON32_PROVIDER_DEFAULT, token) != 0) 源代码网推荐 { 源代码网推荐 if(DuplicateToken(token[0], 2, tokenDuplicate) != 0) 源代码网推荐 { 源代码网推荐 tempWindowsIdentity = new WindowsIdentity(tokenDuplicate[0]); 源代码网推荐 impersonationContext = tempWindowsIdentity.Impersonate(); 源代码网推荐 if (impersonationContext != null) 源代码网推荐 { 源代码网推荐 CloseHandle(tokenDuplicate); 源代码网推荐 CloseHandle(token); 源代码网推荐 return true; 源代码网推荐 } 源代码网推荐 } 源代码网推荐 } 源代码网推荐 } 源代码网推荐 if(!token[0].Equals(System.IntPtr.Zero)) 源代码网推荐 CloseHandle(token); 源代码网推荐 if(!tokenDuplicate[0].Equals(System.IntPtr.Zero)) 源代码网推荐 CloseHandle(tokenDuplicate); 源代码网推荐 return false; 源代码网推荐 } 源代码网推荐 private void undoImpersonation() 源代码网推荐 { 源代码网推荐 impersonationContext.Undo(); 源代码网推荐 } 源代码网推荐 </script> 源代码网推荐 源代码网推荐 源代码网推荐 注意:在线程上模拟特定用户的进程的标识必须具有“作为操作系统的一部分”权限。默认情况下,Aspnet_wp.exe 进程在名为 ASPNET 的计算机帐户下运行。不过,此帐户没有模拟特定用户所需的权限。如果您尝试模拟特定用户,则会出现一条错误信息。 源代码网推荐 源代码网推荐 要解决此问题,请使用下列方法之一: 源代码网推荐 源代码网推荐 • 源代码网推荐 源代码网推荐 为 ASPNET 帐户授予“作为操作系统的一部分”权限。 源代码网推荐 源代码网推荐 • 源代码网推荐 源代码网推荐 在 Machine.config 文件的 <processModel> 配置部分中,将运行 Aspnet_wp.exe 进程所使用的帐户更改为 System 帐户。 源代码网推荐 源代码网推荐 源代码网推荐 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
