知识大全 通过ASP.net程序创建域帐户故障

Posted 帐户

篇首语:人往大处看,鸟往高处飞。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 通过ASP.net程序创建域帐户故障相关的知识,希望对你有一定的参考价值。

通过ASP.net程序创建域帐户故障  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

  我曾经成功地使用windows程序成功的创建了一批带邮箱的域帐户 但是 当我把这段代码交给我的一个同事(她负责开发Web应用)迁移到中后 只能创建域帐户 不能创建邮箱 为什么呢?

  我们咨询了微软的工程师 他告诉我们 这是由于的权限不够 我们应该在模拟用户 这样就可以成功创建

  我将微软的相关文章摘录下来

  模拟 IIS 验证的帐户或用户

  若要在收到 ASP NET 应用程序中每个页的每个请求时模拟 Microsoft Internet 信息服务 (IIS) 身份验证用户 必须在此应用程序的 nfig 文件中包含 <identity> 标记 并将 impersonate 属性设置为 true 例如

  <identity impersonate= true />

  为 ASP NET 应用程序的所有请求模拟特定用户

  若要为 ASP NET 应用程序的所有页面上的所有请求模拟特定用户 可以在该应用程序的 nfig 文件的 <identity> 标记中指定 userName 和 password 属性 例如

  <identity impersonate= true userName= accountname password= password />

  注意 在线程上模拟特定用户的进程的标识必须具有 作为操作系统的一部分 权限 默认情况下 Aspnet_wp exe 进程在名为 ASPNET 的计算机帐户下运行 不过 此帐户没有模拟特定用户所需的权限 如果您尝试模拟特定用户 则会出现一条错误信息

  要解决此问题 请使用下列方法之一

  为 ASPNET 帐户(权限最低的帐户)授予 作为操作系统的一部分 权限

  注意 虽然此方法可以解决问题 但 Microsoft 不建议使用此方法

  在 nfig 文件的 <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 LOGON _LOGON_INTERACTIVE As Integer = Dim LOGON _PROVIDER_DEFAULT As Integer = Dim impersonationContext As WindowsImpersonationContext Declare Function LogonUserA Lib advapi 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 advapi dll ( _ ByVal ExistingTokenHandle As IntPtr _ ByVal ImpersonationLevel As Integer _ ByRef DuplicateTokenHandle As IntPtr) As Integer Declare Auto Function RevertToSelf Lib advapi dll () As Long Declare Auto Function CloseHandle Lib kernel 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 LOGON _LOGON_INTERACTIVE LOGON _PROVIDER_DEFAULT token) <> Then If DuplicateToken(token tokenDuplicate) <> Then tempWindowsIdentity = New WindowsIdentity(tokenDuplicate)

  impersonationContext = tempWindowsIdentity Impersonate()

  If Not impersonationContext Is Nothing Then impersonateValidUser = True 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 LOGON _LOGON_INTERACTIVE = public const int LOGON _PROVIDER_DEFAULT = WindowsImpersonationContext impersonationContext [DllImport( advapi dll )] public static extern int LogonUserA(String lpszUserName String lpszDomain String lpszPassword int dwLogonType int dwLogonProvider ref IntPtr phToken) [DllImport( advapi dll CharSet=CharSet Auto SetLastError=true)] public static extern int DuplicateToken(IntPtr hToken int impersonationLevel ref IntPtr hNewToken) [DllImport( advapi dll CharSet=CharSet Auto SetLastError=true)] public static extern bool RevertToSelf() [DllImport( kernel 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 LOGON _LOGON_INTERACTIVE LOGON _PROVIDER_DEFAULT ref token) != )

   if(DuplicateToken(token ref tokenDuplicate) != )

   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 LOGON _LOGON_INTERACTIVE = public static int LOGON _PROVIDER_DEFAULT = WindowsImpersonationContext impersonationContext /** @attribute DllImport( advapi dll ) */ public static native int LogonUserA(String lpszUserName String lpszDomain String lpszPassword int dwLogonType int dwLogonProvider System IntPtr[] phToken) /** @attribute DllImport( advapi dll CharSet=CharSet Auto SetLastError=true) */ public static native int DuplicateToken(System IntPtr hToken int impersonationLevel System IntPtr[] hNewToken) /** @attribute DllImport( kernel dll CharSet=CharSet Auto) */ public static native boolean CloseHandle(System IntPtr[] handle) /** @attribute DllImport( advapi 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[ ] System IntPtr[] tokenDuplicate = new System IntPtr[ ] if(RevertToSelf())

   if(LogonUserA(userName domain password LOGON _LOGON_INTERACTIVE LOGON _PROVIDER_DEFAULT token) != )

   if(DuplicateToken(token[ ] tokenDuplicate) != )

   tempWindowsIdentity = new WindowsIdentity(tokenDuplicate[ ]) impersonationContext = tempWindowsIdentity Impersonate() if (impersonationContext != null)

   CloseHandle(tokenDuplicate) CloseHandle(token) return true if(!token[ ] Equals(System IntPtr Zero))

  CloseHandle(token) if(!tokenDuplicate[ ] Equals(System IntPtr Zero))

  CloseHandle(tokenDuplicate) return false private void undoImpersonation()

   impersonationContext Undo() </script>

  注意 在线程上模拟特定用户的进程的标识必须具有 作为操作系统的一部分 权限 默认情况下 Aspnet_wp exe 进程在名为 ASPNET 的计算机帐户下运行 不过 此帐户没有模拟特定用户所需的权限 如果您尝试模拟特定用户 则会出现一条错误信息

  要解决此问题 请使用下列方法之一

  为 ASPNET 帐户授予 作为操作系统的一部分 权限

cha138/Article/program/net/201311/12774

相关参考

知识大全 ASP.NET MVC创建TaskList应用程序

ASP.NETMVC创建TaskList应用程序  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!如果

知识大全 基于asp.net的web页面动态控件创建以及使用

 摘要web设计中有很多场合页面的控件要动态创建甚至只能动态创建这样可以增加页面的灵活性但是给程序员带来了一些麻烦比如要使用动态创建的控件怎么使用都是要求解决的问题本文基于aspnet简要介绍了页面的

知识大全 asp.net获得客户端域账号

  TogetthewindowsusernamethroughprogramaticallyCompulsaryweneedtosetthewebserver  OpentheInternetInf

知识大全 ASP.NET后台代码实现XmlHttp跨域访问

ASP.NET后台代码实现XmlHttp跨域访问  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  

知识大全 ASP.NET入门教程 9.5 变量的作用域和生存期

ASP.NET入门教程9.5变量的作用域和生存期  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!&n

知识大全 创建ASP.NET网站

ASP.NET开发宝典:创建ASP.NET网站  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  &

知识大全 ASP.NET应用程序结构及安全规划[1]

ASP.NET应用程序结构及安全规划[1]  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  通过上

知识大全 为 ASP.NET 创建缓存配置对象

为ASP.NET创建缓存配置对象  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  简介    如果

知识大全 ASP.NET创建文件并写入内容

ASP.NET创建文件并写入内容  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  在ASPNET中

知识大全 ASP.NET创建Web服务之设计方针

ASP.NET创建Web服务之设计方针  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  使用ASP