首 页  资讯中心 下载中心 资讯教程 最新下载 发布软件 发布文章 网通站 电信站繁體中文
设为首页
加入收藏
联系我们
 
您当前的位置:曾子源码软件下载站 -> 网络编程 -> asp.net -> 文章内容 退出登录 用户管理
热门文章
· 常用C,VC,C++书籍下..
· 新概念英语视频教程..
· 常用 JAVA JAVA2 J..
· 《梦幻麻将馆9雀圣争..
· 新东方英语视频教程..
· 常用VB,Visual Basi..
· [组图] After Effect..
· WINDOWS 所有系统文..
· [组图] 让机器运行多..
· 全美经典学习指导系..
相关文章
· 看好你的电话:黑客..
· 通过启动脚本来感受..
· ASP.NET通过DSO访问..
· 通过DataTable获得表..
· 利用WebRequest来实..
· 2个页面间不通过Ses..
· [组图] 通过系统配置..
· 通过系统配置来提高..
· 通过ASP.net程序创建..
· [组图] Vista Home B..
通过ASP.net程序创建域帐户故障(2)
作者: luGong     来源:不详  发布时间:2006-3-8 4:34:04  发布人:我爱源码

减小字体 增大字体

     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 帐户。
  
  
  
[] [返回上一页] [打 印] [收 藏]
∷相关文章评论∷    (评论内容只代表网友观点,与本站立场无关!) [发表评论...]
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 友情连接 - 网站地图 - 网站信息排名查询
Copyright © 2004-2006 Zasp.Net. All Rights Reserved .