我曾经成功地使用 windows 程序成功的创建了一批带邮箱的域帐户,但是,当我把这段代码交给我的一个同事(她负责开发 Web 应用)迁移到 asp .net 中后,只能创建域帐户,不能创建邮箱。为什么呢?
我们咨询了微软的工程师,他告诉我们,这是由于 asp.net 的权限不够,我们应该在 asp.net 模拟用户,这样就可以成功创建。
我将微软的相关文章摘录下来:
模拟 IIS 验证的帐户或用户
若要在收到 ASP.NET 应用程序中每个页的每个请求时模拟 Microsoft Internet 信息服务 (IIS) 身份验证用户,必须在此应用程序的 Web.config 文件中包含
1<identity> 标记,并将 impersonate 属性设置为 true 。例如:
2
3<identity impersonate="true"></identity>
4
5
6为 ASP.NET 应用程序的所有请求模拟特定用户
7
8若要为 ASP.NET 应用程序的所有页面上的所有请求模拟特定用户,可以在该应用程序的 Web.config 文件的 <identity> 标记中指定 userName 和 password 属性。例如:
9
10<identity impersonate="true" password="password" username="accountname"></identity>
11
12
13注意: 在线程上模拟特定用户的进程的标识必须具有“作为操作系统的一部分”权限。默认情况下,Aspnet_wp.exe 进程在名为 ASPNET 的计算机帐户下运行。不过,此帐户没有模拟特定用户所需的权限。如果您尝试模拟特定用户,则会出现一条错误信息。
14
15要解决此问题,请使用下列方法之一:
16
17•
18
19为 ASPNET 帐户(权限最低的帐户)授予“作为操作系统的一部分”权限。
20
21注意: 虽然此方法可以解决问题,但 Microsoft 不建议使用此方法。
22
23•
24
25在 Machine.config 文件的 <processmodel> 配置部分中,将运行 Aspnet_wp.exe 进程所使用的帐户更改为 System 帐户。
26
27在代码中模拟身份验证用户
28
29若要仅在运行代码特定部分时模拟身份验证用户 ( User.Identity ),您可以使用以下代码。此方法要求身份验证用户标识的类型为 WindowsIdentity 。
30
31Visual Basic .NET
32
33Dim impersonationContext As System.Security.Principal.WindowsImpersonationContext
34Dim currentWindowsIdentity As System.Security.Principal.WindowsIdentity
35currentWindowsIdentity = CType(User.Identity, System.Security.Principal.WindowsIdentity)
36impersonationContext = currentWindowsIdentity.Impersonate()
37'Insert your code that runs under the security context of the authenticating user here.
38impersonationContext.Undo()
39
40
41Visual C# .NET
42
43System.Security.Principal.WindowsImpersonationContext impersonationContext;
44impersonationContext =
45((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();
46//Insert your code that runs under the security context of the authenticating user here.
47impersonationContext.Undo();
48
49
50Visual J# .NET
51
52System.Security.Principal.WindowsImpersonationContext impersonationContext;
53impersonationContext =
54((System.Security.Principal.WindowsIdentity)get_User().get_Identity()).Impersonate();
55//Insert your code that runs under the security context of the authenticating user here.
56impersonationContext.Undo();
57
58
59在代码中模拟特定用户
60
61若要仅在运行代码特定部分时模拟特定用户,请使用以下代码:
62
63Visual Basic .NET
@ Page Language="VB"
@ Import Namespace = "System.Web"
@ Import Namespace = "System.Web.Security"
@ Import Namespace = "System.Security.Principal"
@ Import Namespace = "System.Runtime.InteropServices"
1<script runat="server">
2Dim LOGON32_LOGON_INTERACTIVE As Integer = 2
3Dim LOGON32_PROVIDER_DEFAULT As Integer = 0
4Dim impersonationContext As WindowsImpersonationContext
5Declare Function LogonUserA Lib "advapi32.dll" (ByVal lpszUsername As String, _
6ByVal lpszDomain As String, _
7ByVal lpszPassword As String, _
8ByVal dwLogonType As Integer, _
9ByVal dwLogonProvider As Integer, _
10ByRef phToken As IntPtr) As Integer
11Declare Auto Function DuplicateToken Lib "advapi32.dll" ( _
12ByVal ExistingTokenHandle As IntPtr, _
13ByVal ImpersonationLevel As Integer, _
14ByRef DuplicateTokenHandle As IntPtr) As Integer
15Declare Auto Function RevertToSelf Lib "advapi32.dll" () As Long
16Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Long
17Public Sub Page_Load(ByVal s As Object, ByVal e As EventArgs)
18If impersonateValidUser("username", "domain", "password") Then
19'Insert your code that runs under the security context of a specific user here.
20undoImpersonation()
21Else
22'Your impersonation failed. Therefore, include a fail-safe mechanism here.
23End If
24End Sub
25Private Function impersonateValidUser(ByVal userName As String, _
26ByVal domain As String, ByVal password As String) As Boolean
27Dim tempWindowsIdentity As WindowsIdentity
28Dim token As IntPtr = IntPtr.Zero
29Dim tokenDuplicate As IntPtr = IntPtr.Zero
30impersonateValidUser = False
31If RevertToSelf() Then
32If LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
33LOGON32_PROVIDER_DEFAULT, token) <> 0 Then
34If DuplicateToken(token, 2, tokenDuplicate) <> 0 Then
35tempWindowsIdentity = New WindowsIdentity(tokenDuplicate)
36impersonationContext = tempWindowsIdentity.Impersonate()
37If Not impersonationContext Is Nothing Then
38impersonateValidUser = True
39End If
40End If
41End If
42End If
43If Not tokenDuplicate.Equals(IntPtr.Zero) Then
44CloseHandle(tokenDuplicate)
45End If
46If Not token.Equals(IntPtr.Zero) Then
47CloseHandle(token)
48End If
49End Function
50Private Sub undoImpersonation()
51impersonationContext.Undo()
52End Sub
53</script>
54
55Visual C# .NET
@ Page Language="C#"
@ Import Namespace = "System.Web"
@ Import Namespace = "System.Web.Security"
@ Import Namespace = "System.Security.Principal"
@ Import Namespace = "System.Runtime.InteropServices"
1<script runat="server">
2public const int LOGON32_LOGON_INTERACTIVE = 2;
3public const int LOGON32_PROVIDER_DEFAULT = 0;
4WindowsImpersonationContext impersonationContext;
5[DllImport("advapi32.dll")]
6public static extern int LogonUserA(String lpszUserName,
7String lpszDomain,
8String lpszPassword,
9int dwLogonType,
10int dwLogonProvider,
11ref IntPtr phToken);
12[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
13public static extern int DuplicateToken(IntPtr hToken,
14int impersonationLevel,
15ref IntPtr hNewToken);
16[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
17public static extern bool RevertToSelf();
18[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
19public static extern bool CloseHandle(IntPtr handle);
20public void Page_Load(Object s, EventArgs e)
21{
22if(impersonateValidUser("username", "domain", "password"))
23{
24//Insert your code that runs under the security context of a specific user here.
25undoImpersonation();
26}
27else
28{
29//Your impersonation failed. Therefore, include a fail-safe mechanism here.
30}
31}
32private bool impersonateValidUser(String userName, String domain, String password)
33{
34WindowsIdentity tempWindowsIdentity;
35IntPtr token = IntPtr.Zero;
36IntPtr tokenDuplicate = IntPtr.Zero;
37if(RevertToSelf())
38{
39if(LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
40LOGON32_PROVIDER_DEFAULT, ref token) != 0)
41{
42if(DuplicateToken(token, 2, ref tokenDuplicate) != 0)
43{
44tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
45impersonationContext = tempWindowsIdentity.Impersonate();
46if (impersonationContext != null)
47{
48CloseHandle(token);
49CloseHandle(tokenDuplicate);
50return true;
51}
52}
53}
54}
55if(token!= IntPtr.Zero)
56CloseHandle(token);
57if(tokenDuplicate!=IntPtr.Zero)
58CloseHandle(tokenDuplicate);
59return false;
60}
61private void undoImpersonation()
62{
63impersonationContext.Undo();
64}
65</script>
66
67Visual J# .NET
@ Page language="VJ#"
@ Import Namespace="System.Web"
@ Import Namespace="System.Web.Security"
@ Import Namespace="System.Security.Principal"
@ Import Namespace="System.Runtime.InteropServices"
1<script runat="server">
2public static int LOGON32_LOGON_INTERACTIVE = 2;
3public static int LOGON32_PROVIDER_DEFAULT = 0;
4WindowsImpersonationContext impersonationContext;
5/** @attribute DllImport("advapi32.dll") */
6public static native int LogonUserA(String lpszUserName,
7String lpszDomain,
8String lpszPassword,
9int dwLogonType,
10int dwLogonProvider,
11System.IntPtr[] phToken);
12/** @attribute DllImport("advapi32.dll",
13CharSet=CharSet.Auto, SetLastError=true) */
14public static native int DuplicateToken(System.IntPtr hToken,
15int impersonationLevel,
16System.IntPtr[] hNewToken);
17/** @attribute DllImport("kernel32.dll",CharSet=CharSet.Auto) */
18public static native boolean CloseHandle(System.IntPtr[] handle);
19/** @attribute DllImport("advapi32.dll",
20CharSet=CharSet.Auto,SetLastError=true) */
21public static native boolean RevertToSelf();
22public void Page_Load(Object s, System.EventArgs e)
23{
24if(impersonateValidUser("username", "domain", " password"))
25{
26//Insert your code that runs under the security context of a specific user here.
27undoImpersonation();
28}
29else
30{
31//Your impersonation failed. Therefore, include a fail-safe mechanism here.
32}
33}
34private boolean impersonateValidUser(String userName, String domain, String password)
35{
36WindowsIdentity tempWindowsIdentity;
37System.IntPtr[] token = new System.IntPtr[1];
38System.IntPtr[] tokenDuplicate = new System.IntPtr[1];
39if(RevertToSelf())
40{
41if(LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
42LOGON32_PROVIDER_DEFAULT, token) != 0)
43{
44if(DuplicateToken(token[0], 2, tokenDuplicate) != 0)
45{
46tempWindowsIdentity = new WindowsIdentity(tokenDuplicate[0]);
47impersonationContext = tempWindowsIdentity.Impersonate();
48if (impersonationContext != null)
49{
50CloseHandle(tokenDuplicate);
51CloseHandle(token);
52return true;
53}
54}
55}
56}
57if(!token[0].Equals(System.IntPtr.Zero))
58CloseHandle(token);
59if(!tokenDuplicate[0].Equals(System.IntPtr.Zero))
60CloseHandle(tokenDuplicate);
61return false;
62}
63private void undoImpersonation()
64{
65impersonationContext.Undo();
66}
67</script>
68
69
70注意: 在线程上模拟特定用户的进程的标识必须具有“作为操作系统的一部分”权限。默认情况下,Aspnet_wp.exe 进程在名为 ASPNET 的计算机帐户下运行。不过,此帐户没有模拟特定用户所需的权限。如果您尝试模拟特定用户,则会出现一条错误信息。
71
72要解决此问题,请使用下列方法之一:
73
74•
75
76为 ASPNET 帐户授予“作为操作系统的一部分”权限。
77
78•
79
80在 Machine.config 文件的 <processmodel> 配置部分中,将运行 Aspnet_wp.exe 进程所使用的帐户更改为 System 帐户。
81
82返回页首</processmodel></processmodel></identity></identity>