向您咨询下关于asp.net临时模拟windows用户的问题

ZBZGiGi 2008-03-05 04:05:08
我在vs2005中需要临时用到windows域帐号的权限,网上搜了下,人家说要

[align=left][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 WindowsIdentity ImpersonationUser
{
get
{
WindowsIdentity newId = null;

string[] args={"VASP","tianqing","!wenXiu01"};
IntPtr tokenHandle = new IntPtr(0);
IntPtr dupeTokenHandle = new IntPtr(0);

try
{
if (args.Length < 3)
{
Console.WriteLine("Usage: DomainName UserName Password");
return null;
}

const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_LOGON_INTERACTIVE = 2;
const int SecurityImpersonation = 2;

tokenHandle = IntPtr.Zero;
dupeTokenHandle = IntPtr.Zero;

bool returnValue = LogonUser(args[1], args[0], args[2],
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
ref tokenHandle);

if (false == returnValue)
{
Console.WriteLine("LogonUser failed with error code : {0}",
Marshal.GetLastWin32Error());
}

Console.WriteLine("Before impersonation: "
+ WindowsIdentity.GetCurrent().Name);

bool retVal = DuplicateToken(tokenHandle, SecurityImpersonation, ref dupeTokenHandle);
if (false == retVal)
{
CloseHandle(tokenHandle);
Console.WriteLine("Exception in token duplication.");
return null;
}

newId = new WindowsIdentity(tokenHandle);
WindowsImpersonationContext impersonatedUser = newId.Impersonate();

Console.WriteLine("After impersonation: "
+ WindowsIdentity.GetCurrent().Name);

impersonatedUser.Undo();

Console.WriteLine("After Undo: " + WindowsIdentity.GetCurrent().Name);

if (tokenHandle != IntPtr.Zero)
CloseHandle(tokenHandle);
if (dupeTokenHandle != IntPtr.Zero)
CloseHandle(dupeTokenHandle);
}
catch(Exception ex)
{
Console.WriteLine("Exception occurred. " + ex.Message);
}
return newId;
}
}

可是我在调用
bool returnValue = LogonUser(args[1], args[0], args[2], LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,ref tokenHandle);
返回false,登录失败

希望您能指点下
...全文
57 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhuanshen712 2008-03-05
  • 打赏
  • 举报
回复
我也顶顶。
蝶恋花雨 2008-03-05
  • 打赏
  • 举报
回复
没人那我就帮你顶顶。没有弄过。

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧