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();
}
...全文
100 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
于2024年4月-2025年9月期间,研究团队在贵州习水国家级自然保护区制定39条样线,涵盖灌木林、常绿阔叶林、针叶林、常绿落叶阔叶混交林、针阔混交林等不同植被类型,每条样线分春夏秋冬4个季节采集样品,用真菌采集软件记录经纬度、海拔、采集地点、时间、生境等信息,使用佳能相机(R6 mark Ⅱ)对大型真菌进行拍照,并采集标本,标本存放于贵州省生物研究所大型真菌标本馆(HGAMF)。 通过形态学初步鉴定,结合分子生物学最终鉴定,参考已]报道的中国毒蘑菇名录开展毒蘑菇的认定。 调查到保护区内有毒真菌7目25科64种,导致中毒的主要类型有急性肾衰竭型、神经精神型和胃肠炎型。最终形成贵州习水国家级自然保护区大型有毒真菌图片数据集,它由以下2个部分组成。 (1)附件1包含78张原始照片(.JPG),照片名字包括了大型有毒真菌的拉丁名和中文名,若无中文名的直接用拉丁名。 (2)附件2是一个压缩文件,包含了2张工作表,其中一张表是大型有毒真菌39条样线的信息,另一张表是大型有毒真菌的中毒类型。 照片采用佳能相机R6 mark Ⅱ拍摄,物种鉴定通过多种文献核实,并经两位以上专家鉴定确认。该数据集可为研究地及周边的普通人识别有毒大型真菌提供参考,通过及时的图片对比,能有效避免误采误食大型有毒真菌,同时为因误食大型真菌可能引发的身体损伤进行了总结,能为患者及时治疗提供参考。

62,269

社区成员

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

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

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

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