To saucer(思归, .NET MVP) :如何在ASP.NET中取到AD用户,急死了

legend-never-die 2003-08-25 02:28:48
我按照你建议的方法用了伪装,可是却总是伪装不成功,帮忙看看吧,急死了

代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Security;
using System.Security.Principal;
using System.Runtime.InteropServices;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.DirectoryServices;
using EBudgetLib;

namespace EBudgetWeb.UserPower
{
/// <summary>
/// UserAccount 的摘要说明。
/// </summary>
public class UserAccount : System.Web.UI.Page
{
protected System.Web.UI.WebControls.ListBox ListBoxADUser;
protected System.Web.UI.WebControls.LinkButton LinkButtonToRight;
protected System.Web.UI.WebControls.LinkButton LinkButtonToLeft;
protected System.Web.UI.WebControls.ListBox ListBoxSelectUser;
protected System.Web.UI.WebControls.TextBox tbUserName;
public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_PROVIDER_DEFAULT = 0;

WindowsImpersonationContext impersonationContext;

[DllImport("advapi32.dll", CharSet=CharSet.Auto)]
public static extern int LogonUser(String lpszUserName,
String lpszDomain,
String lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
[DllImport("advapi32.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto,
SetLastError=true)]
public extern static int DuplicateToken(IntPtr hToken,
int impersonationLevel,
ref IntPtr hNewToken);

private void Page_Load(object sender, System.EventArgs e)
{
if(impersonateValidUser("UserName", "DomainName", "UserPwd"))
{
string[] strDomainUser = EBudgetLib.CGetDomainUser.GetDomainUser();
undoImpersonation();
}
else
{

}
}

private bool impersonateValidUser(String userName, String domain, String password)
{
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;

if(LogonUser(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, ref token) != 0)
{
if(DuplicateToken(token, 2, ref tokenDuplicate) != 0)
{
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
impersonationContext = tempWindowsIdentity.Impersonate();
if (impersonationContext != null)
return true;
else
return false;
}
else
return false;
}
else
return false;
}
private void undoImpersonation()
{
impersonationContext.Undo();
}
...全文
46 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
saucer 2003-08-26
  • 打赏
  • 举报
回复
you didn't see the small print from
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q306158

"....
NOTE: The identity of the process that impersonates a specific user on a thread must have the "Act as part of the operating system" privilege. By default, the Aspnet_wp.exe process runs under a computer account named ASPNET. However, this account does not have the required privileges to impersonate a specific user. You receive an error message if you try to impersonate a specific user.

To work around this problem, use one of the following methods:
Grant the "Act as part of the operating system" privilege to the ASPNET account.
Change the account that the Aspnet_wp.exe process runs under to the System account in the <processModel> configuration section of the Machine.config file.
..."


see the relevant part in
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT02.asp
  • 打赏
  • 举报
回复
Dim objDE As DirectoryServices.DirectoryEntry
Dim strPath As String = "WinNT://kfgs"
' Create a new DirectoryEntry with the given path.
objDE = New DirectoryServices.DirectoryEntry(strPath)

Dim objChildDE As DirectoryServices.DirectoryEntry
For Each objChildDE In objDE.Children
Response.Write(objChildDE.Name + "<BR>")

Next objChildDE
gweidian 2003-08-25
  • 打赏
  • 举报
回复
UP
legend-never-die 2003-08-25
  • 打赏
  • 举报
回复
ding
legend-never-die 2003-08-25
  • 打赏
  • 举报
回复
To:cyp503(谁怕?一蓑烟雨任平生)

该示例我看过了,与我的代码完全一样
我使用该代码在if(LogonUser(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, ref token) != 0)
处运行为0,即我扮演用户不能通过,不知为何???
alaisalaix 2003-08-25
  • 打赏
  • 举报
回复
关注,up
菁菁报表 2003-08-25
  • 打赏
  • 举报
回复
有一个ad的控件。
cyp503 2003-08-25
  • 打赏
  • 举报
回复
参考:

http://www.aspcool.com/lanmu/browse1.asp?ID=965&bbsuser=csharp

62,040

社区成员

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

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

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

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