MSDN资源
在ASP.NET应用程序中使用身份模拟(Impersonation)
地址:
其他资源
using
System;
using
System.Security.Principal;
using
System.Security.Permissions;
using
System.Runtime.InteropServices;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
namespace
CommonLibrary
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 模拟其他用户帐号
/// </summary>
public class UserIdentity
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
public static WindowsImpersonationContext wic = null;
// public static void ImpersonateAdmin()
// {
// wic = CreateIdentity(BSConfig.User(),BSConfig.Domain(),BSConfig.Password()).Impersonate();
// }
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 身份摸拟
/// </summary>
/// <param name="User">Name</param>
/// <param name="Domain">Domain</param>
/// <param name="Password">Password</param>
public static void ImpersonateAdmin(string User, string Domain, string Password)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
wic = CreateIdentity(User, Domain, Password).Impersonate();
}
public static void EndImpersonate()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
wic.Undo();
}
protected static WindowsIdentity CreateIdentity(string User, string Domain, string Password)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
// The Windows NT user token.
IntPtr tokenHandle = new IntPtr(0);
const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_LOGON_NETWORK = 3;
tokenHandle = IntPtr.Zero;
// Call LogonUser to obtain a handle to an access token.
bool returnValue = LogonUser(User, Domain, Password,LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT,ref tokenHandle);
if (false == returnValue)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
int ret = Marshal.GetLastWin32Error();
throw new Exception("LogonUser failed with error code: " + ret);
}
System.Diagnostics.Debug.WriteLine("Created user token: " + tokenHandle);
//The WindowsIdentity class makes a new copy of the token.
//It also handles calling CloseHandle for the copy.
WindowsIdentity id = new WindowsIdentity(tokenHandle);
CloseHandle(tokenHandle);
return id;
}
[DllImport("advapi32.dll", SetLastError=true)]
private static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
private extern static bool CloseHandle(IntPtr handle);
}
}
用类似于如下的方式调用以上类
string
imPersonUser
=
ConfigurationSettings.AppSettings.Get(
"
ImPersonUser
"
);
string
imPersonDomain
=
ConfigurationSettings.AppSettings.Get(
"
ImPersonDomain
"
);
string
imPersonPassword
=
ConfigurationSettings.AppSettings.Get(
"
ImPersonPassword
"
);
DESCryptoServiceProvider des
=
new
DESCryptoServiceProvider();
imPersonPassword
=
des.Dencrypt(imPersonPassword);
UserIdentity.ImpersonateAdmin(imPersonUser,imPersonDomain,imPersonPassword);
.......
UserIdentity.EndImpersonate();