当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  通过ASP.net程序创建域帐户故障

 通过ASP.net程序创建域帐户故障

点击次数:18 次 发布日期:2008-11-26 10:27:47 作者:源代码网
源代码网推荐      我曾经成功地使用windows程序成功的创建了一批带邮箱的域帐户,但是,当我把这段代码交给我的一个同事(她负责开发Web应用)迁移到asp.net中后,只能创建域帐户,不能创建邮箱。为什么呢?
源代码网推荐  
源代码网推荐  我们咨询了微软的工程师,他告诉我们,这是由于asp.net的权限不够,我们应该在asp.net模拟用户,这样就可以成功创建。
源代码网推荐  
源代码网推荐  我将微软的相关文章摘录下来:
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  模拟 IIS 验证的帐户或用户
源代码网推荐  
源代码网推荐  若要在收到 ASP.NET 应用程序中每个页的每个请求时模拟 Microsoft Internet 信息服务 (IIS) 身份验证用户,必须在此应用程序的 Web.config 文件中包含 <identity> 标记,并将 impersonate 属性设置为 true。例如:
源代码网推荐  
源代码网推荐  <identity impersonate="true" />
源代码网推荐  
源代码网推荐  
源代码网推荐  为 ASP.NET 应用程序的所有请求模拟特定用户
源代码网推荐  
源代码网推荐  若要为 ASP.NET 应用程序的所有页面上的所有请求模拟特定用户,可以在该应用程序的 Web.config 文件的 <identity> 标记中指定 userName 和 password 属性。例如:
源代码网推荐  
源代码网推荐  <identity impersonate="true" userName="accountname" password="password" />
源代码网推荐  
源代码网推荐  
源代码网推荐  注意:在线程上模拟特定用户的进程的标识必须具有“作为操作系统的一部分”权限。默认情况下,Aspnet_wp.exe 进程在名为 ASPNET 的计算机帐户下运行。不过,此帐户没有模拟特定用户所需的权限。如果您尝试模拟特定用户,则会出现一条错误信息。
源代码网推荐  
源代码网推荐  要解决此问题,请使用下列方法之一:
源代码网推荐  
源代码网推荐  •
源代码网推荐  
源代码网推荐  为 ASPNET 帐户(权限最低的帐户)授予“作为操作系统的一部分”权限。
源代码网推荐  
源代码网推荐  注意:虽然此方法可以解决问题,但 Microsoft 不建议使用此方法。
源代码网推荐  
源代码网推荐  •
源代码网推荐  
源代码网推荐  在 Machine.config 文件的 <processModel> 配置部分中,将运行 Aspnet_wp.exe 进程所使用的帐户更改为 System 帐户。
源代码网推荐  
源代码网推荐  在代码中模拟身份验证用户
源代码网推荐  
源代码网推荐  若要仅在运行代码特定部分时模拟身份验证用户 (User.Identity),您可以使用以下代码。此方法要求身份验证用户标识的类型为 WindowsIdentity。
源代码网推荐  
源代码网推荐  Visual Basic .NET
源代码网推荐  
源代码网推荐  Dim impersonationContext As System.Security.Principal.WindowsImpersonationContext
源代码网推荐  Dim currentWindowsIdentity As System.Security.Principal.WindowsIdentity
源代码网推荐  currentWindowsIdentity = CType(User.Identity, System.Security.Principal.WindowsIdentity)
源代码网推荐  impersonationContext = currentWindowsIdentity.Impersonate()
源代码网推荐  "Insert your code that runs under the security context of the authenticating user here.
源代码网推荐  impersonationContext.Undo()
源代码网推荐  
源代码网推荐  
源代码网推荐  Visual C# .NET
源代码网推荐  
源代码网推荐  System.Security.Principal.WindowsImpersonationContext impersonationContext;
源代码网推荐  impersonationContext =
源代码网推荐  ((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();
源代码网推荐  //Insert your code that runs under the security context of the authenticating user here.
源代码网推荐  impersonationContext.Undo();
源代码网推荐  
源代码网推荐  
源代码网推荐  Visual J# .NET
源代码网推荐  
源代码网推荐  System.Security.Principal.WindowsImpersonationContext impersonationContext;
源代码网推荐  impersonationContext =
源代码网推荐  ((System.Security.Principal.WindowsIdentity)get_User().get_Identity()).Impersonate();
源代码网推荐  //Insert your code that runs under the security context of the authenticating user here.
源代码网推荐  impersonationContext.Undo();
源代码网推荐  
源代码网推荐  
源代码网推荐  在代码中模拟特定用户
源代码网推荐  
源代码网推荐  若要仅在运行代码特定部分时模拟特定用户,请使用以下代码:
源代码网推荐  
源代码网推荐  Visual Basic .NET
源代码网推荐  
源代码网推荐  <%@ 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)。
源代码网推荐


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华