C#指定用户执行程序的示例代码(转载)
2006-06-19 16:57
573 查看
#region using directives
using System;
using System.Security.Principal;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
#endregion
namespace testEnumWindows
{
/// <summary>
/// ChangeUser 的摘要说明。
/// </summary>
public class ChangeUser
{
[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);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public extern static bool DuplicateToken(IntPtr ExistingTokenHandle,
int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle);
public static void Main(string[] args)
{
Console.WriteLine("当前用户是: "+ WindowsIdentity.GetCurrent().Name);
ImpersonatedWork testDel = new ImpersonatedWork(Test);
ImpersonateAndDo("test", "bca~123", testDel);
Console.WriteLine("当前用户是: "+ WindowsIdentity.GetCurrent().Name);
}
static void Test()
{
Console.WriteLine("当前用户是: " + WindowsIdentity.GetCurrent().Name);
}
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)
{
string domainName = string.Empty;
string userName = string.Empty;
IntPtr tokenHandle = new IntPtr(0);
IntPtr dupeTokenHandle = new IntPtr(0);
const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_LOGON_INTERACTIVE = 2;
const int SecurityImpersonation = 2;
if (! Regex.IsMatch(UserName, @"^/w+[//]?/w+$"))
{
throw new ApplicationException("非法的用户名");
}
string[] tmp = UserName.Split(new char[] { '//' });
if (tmp.Length > 1)
{
domainName = tmp[0];
userName = tmp[1];
}
else
{
userName = tmp[0];
}
tokenHandle = IntPtr.Zero;
dupeTokenHandle = IntPtr.Zero;
bool returnValue = LogonUser(userName,domainName, PWD,
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
ref tokenHandle);
if (!returnValue)
{
throw new ApplicationException("取Handle出错了!");
}
//Console.WriteLine("当前用户是: "
// + WindowsIdentity.GetCurrent().Name);
bool retVal = DuplicateToken(tokenHandle, SecurityImpersonation, ref dupeTokenHandle);
if (!retVal)
{
CloseHandle(tokenHandle);
throw new ApplicationException("复制Handle出错了!");
}
WindowsIdentity newId = new WindowsIdentity(dupeTokenHandle);
WindowsImpersonationContext impersonatedUser = newId.Impersonate();
WhatToDo();
impersonatedUser.Undo();
if (tokenHandle != IntPtr.Zero)
CloseHandle(tokenHandle);
if (dupeTokenHandle != IntPtr.Zero)
CloseHandle(dupeTokenHandle);
}
}
}
using System;
using System.Security.Principal;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
#endregion
namespace testEnumWindows
{
/// <summary>
/// ChangeUser 的摘要说明。
/// </summary>
public class ChangeUser
{
[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);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public extern static bool DuplicateToken(IntPtr ExistingTokenHandle,
int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle);
public static void Main(string[] args)
{
Console.WriteLine("当前用户是: "+ WindowsIdentity.GetCurrent().Name);
ImpersonatedWork testDel = new ImpersonatedWork(Test);
ImpersonateAndDo("test", "bca~123", testDel);
Console.WriteLine("当前用户是: "+ WindowsIdentity.GetCurrent().Name);
}
static void Test()
{
Console.WriteLine("当前用户是: " + WindowsIdentity.GetCurrent().Name);
}
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)
{
string domainName = string.Empty;
string userName = string.Empty;
IntPtr tokenHandle = new IntPtr(0);
IntPtr dupeTokenHandle = new IntPtr(0);
const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_LOGON_INTERACTIVE = 2;
const int SecurityImpersonation = 2;
if (! Regex.IsMatch(UserName, @"^/w+[//]?/w+$"))
{
throw new ApplicationException("非法的用户名");
}
string[] tmp = UserName.Split(new char[] { '//' });
if (tmp.Length > 1)
{
domainName = tmp[0];
userName = tmp[1];
}
else
{
userName = tmp[0];
}
tokenHandle = IntPtr.Zero;
dupeTokenHandle = IntPtr.Zero;
bool returnValue = LogonUser(userName,domainName, PWD,
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
ref tokenHandle);
if (!returnValue)
{
throw new ApplicationException("取Handle出错了!");
}
//Console.WriteLine("当前用户是: "
// + WindowsIdentity.GetCurrent().Name);
bool retVal = DuplicateToken(tokenHandle, SecurityImpersonation, ref dupeTokenHandle);
if (!retVal)
{
CloseHandle(tokenHandle);
throw new ApplicationException("复制Handle出错了!");
}
WindowsIdentity newId = new WindowsIdentity(dupeTokenHandle);
WindowsImpersonationContext impersonatedUser = newId.Impersonate();
WhatToDo();
impersonatedUser.Undo();
if (tokenHandle != IntPtr.Zero)
CloseHandle(tokenHandle);
if (dupeTokenHandle != IntPtr.Zero)
CloseHandle(dupeTokenHandle);
}
}
}
相关文章推荐
- C#指定用户执行程序的示例代码(转载)
- 使用C#调用存储过程,用函数合理组织代码,使程序更加的清晰(示例)
- 使用C#调用存储过程,用函数合理组织代码,使程序更加的清晰(示例)
- CLR_via_C#.3rd 翻译[1.4执行程序集代码]
- kettle 程序调用执行ktr转换示例代码
- 编写高质量代码改善C#程序的157个建议[为泛型指定初始值、使用委托声明、使用Lambda替代方法和匿名方法]
- AD帐户操作C#示例代码(二)——检查密码将过期的用户
- 编写高质量代码改善C#程序的157个建议——建议45:为泛型类型参数指定逆变
- Linux学习笔记——使用指定的用户权限执行程序——sudo
- [CLR via C#]1.4 执行程序集的代码
- 转载:c++修改文件(夹)的用户访问权限程序代码
- Asp.net 在网页编写C#代码示例-- 一个简单的web MsSql 命令执行环境
- .Net(C#)获取AD用户信息的示例代码
- easyui datagrid 禁止选中行 EF的增删改查(转载) C# 获取用户IP地址(转载) MVC EF 执行SQL语句(转载) 在EF中执行SQL语句(转载) EF中使用SQL语句或存储过程 .net MVC使用Session验证用户登录 PowerDesigner 参照完整性约束(转载)
- 如何用java代码让android Market显示指定的程序以便用户下载?
- ScheduledExecutorService定时周期执行指定的任务 1 示例代码
- C#完整执行存储过程的代码加实例[转载]
- 如何在C#中用程序执行指定的SQL脚本文件,实现自动安装创建数据库.txt
- c#实现每隔规定时间自动执行程序代码
- [转载]C#计算程序执行时间