| 网管联盟 | 网管论坛 | 网管u家 | 网管博客 | 网管软件 | 网管求职 | 小游戏 | 网管搜索 | 网管原创 | 网管聚合 | 网管读摘 | 网管焦点 | 世界素材 | 会员投稿 | 会员中心 |
![]() |
| Windows Linux Cisco 网络技术 数据库 黑客攻防 DotNet Java PHP 认证 新闻资讯 服务器 存储资讯 网络设备 网管学堂 技术专题 焦点 网吧频道 |
using directives#region Using directives 网管联盟bitsCN@com
using System;
using System.Security.Principal;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
#endregion 中国网管论坛bbs.bitsCN.com
namespace Impersonal
{
class Program
{
测试代码#region 测试代码
public static void Main(string[] args)
{
Console.WriteLine("当前用户是: "
+ WindowsIdentity.GetCurrent().Name);
ImpersonatedWork testDel = new ImpersonatedWork(Test);
ImpersonateAndDo("epro\\liping", "88888888", testDel);
Console.WriteLine("当前用户是: "
+ WindowsIdentity.GetCurrent().Name);
}
static void Test()
{
[DllImport("advapi32.dll", SetLastError = true)]
public extern static bool LogonUser(String lpszUsername, String lpszDomain,
String lpszPassword, int dwLogonType,
int dwLogonProvider, ref IntPtr phToken);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public extern static bool CloseHandle(IntPtr handle);
网管bitscn_com
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public extern static bool DuplicateToken(IntPtr ExistingTokenHandle,
int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle);
public delegate void ImpersonatedWork();
/**//// <summary>
/// 以指定用户的身份去做一件事情
/// </summary>
/// <param name="UserName"></param>
/// <param name="PWD"></param>
/// <param name="WhatToDo"></param>
public static void ImpersonateAndDo(string UserName, string PWD, ImpersonatedWork WhatToDo)
{
扮演用户#region 扮演用户
string domainName = string.Empty;
string userName = string.Empty;
网管bitscn_com
网管网www_bitscn_com
//Console.WriteLine("当前用户是: "
// + WindowsIdentity.GetCurrent().Name);
中国网管论坛bbs.bitsCN.com
bool retVal = DuplicateToken(tokenHandle, SecurityImpersonation, ref dupeTokenHandle);
if (!retVal)
{
CloseHandle(tokenHandle);
throw new ApplicationException("复制Handle出错了!");
}
WindowsIdentity newId = new WindowsIdentity(dupeTokenHandle);
WindowsImpersonationContext impersonatedUser = newId.Impersonate();
#endregion
以新用户身份调用#region 以新用户身份调用
|
0
|
评论加载中…