ASP.NET Active Directory C# 字段规范

发布于 2024-12-06 15:26:03 字数 112 浏览 0 评论 0原文

我们这里有一个活动目录。提供了用户的唯一用户 ID,我需要访问与该用户 ID 相关的组织->经理->名称属性。基本上,这将用于向提交请求的人的经理发送批准表。

知道如何做到这一点吗?

We've got an active directory here. provided the unique user id of the user, I need to access the organization->manager->name attribute related to that userid. Basically this will be used to send an approval form to the manager of the person submitting request.

Any idea how this could be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风轻花落早 2024-12-13 15:26:03

您可以使用以下代码:

/* Retreiving object from SID  
  */  
string SidLDAPURLForm = "LDAP://WM2008R2ENT:389/<SID={0}>";  
System.Security.Principal.SecurityIdentifier sidToFind = new System.Security.Principal.SecurityIdentifier("S-1-5-21-3115856885-816991240-3296679909-1106");  

/*
System.Security.Principal.NTAccount user = new System.Security.Principal.NTAccount("SomeUsername");
System.Security.Principal.SecurityIdentifier sidToFind = user.Translate(System.Security.Principal.SecurityIdentifier)
*/

DirectoryEntry userEntry = new DirectoryEntry(string.Format(SidLDAPURLForm, sidToFind.Value));  
string managerDn = userEntry.Properties["manager"].Value.ToString(); 

但您也可以找到 在这篇文章中其他方式来搜索绑定到活动目录。

You can use the following code :

/* Retreiving object from SID  
  */  
string SidLDAPURLForm = "LDAP://WM2008R2ENT:389/<SID={0}>";  
System.Security.Principal.SecurityIdentifier sidToFind = new System.Security.Principal.SecurityIdentifier("S-1-5-21-3115856885-816991240-3296679909-1106");  

/*
System.Security.Principal.NTAccount user = new System.Security.Principal.NTAccount("SomeUsername");
System.Security.Principal.SecurityIdentifier sidToFind = user.Translate(System.Security.Principal.SecurityIdentifier)
*/

DirectoryEntry userEntry = new DirectoryEntry(string.Format(SidLDAPURLForm, sidToFind.Value));  
string managerDn = userEntry.Properties["manager"].Value.ToString(); 

But you can also find in this post other ways to seach bind to Active-directory.

七颜 2024-12-13 15:26:03

由于您使用的是 .NET 3.5 及更高版本,因此您应该检查 System.DirectoryServices.AccountManagement (S.DS.AM) 命名空间。在这里阅读所有相关内容:

基本上,您可以定义域上下文并轻松查找 AD 中的用户和/或组:

// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");

if(user != null)
{
   // do something here....     
}

// find the group in question
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "YourGroupNameHere");

// if found....
if (group != null)
{
   // iterate over members
   foreach (Principal p in group.GetMembers())
   {
      Console.WriteLine("{0}: {1}", p.StructuralObjectClass, p.DisplayName);
      // do whatever you need to do to those members
   }
}

新的 S.DS.AM 使在 AD 中使用用户和组变得非常容易!

我不是 100% 确定您在具体情况下想要做什么... UserPrincipal 有一个 EmployeeId 属性 - 这是您想要搜索的内容吗?

Since you're on .NET 3.5 and up, you should check out the System.DirectoryServices.AccountManagement (S.DS.AM) namespace. Read all about it here:

Basically, you can define a domain context and easily find users and/or groups in AD:

// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");

if(user != null)
{
   // do something here....     
}

// find the group in question
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "YourGroupNameHere");

// if found....
if (group != null)
{
   // iterate over members
   foreach (Principal p in group.GetMembers())
   {
      Console.WriteLine("{0}: {1}", p.StructuralObjectClass, p.DisplayName);
      // do whatever you need to do to those members
   }
}

The new S.DS.AM makes it really easy to play around with users and groups in AD!

I'm not 100% sure what you want to do in your concrete case... the UserPrincipal has an EmployeeId property - is that what you want to search for?

杯别 2024-12-13 15:26:03

使用 System.DirectoryServices.DirectoryEntry< /a> 类来读出用户对象的适当属性。 DirectoryEntry 的构造函数要求您拥有用户的 LDAP 路径。尽管 IIS 更喜欢仅传递 SAM 帐户名,但获取 LDAP 路径通常很棘手。如果您提供有关您所拥有的用户 ID 的更多详细信息,则可以更轻松地为您指明正确的方向。

为此,运行 ASP.NET 应用程序的帐户需要对 AD 具有读取权限,而默认情况下可能没有该权限。如果 Web 服务器属于 AD,则将应用程序池更改为在“NetworkService”下运行是最简单的方法。然后,ASP.NET 应用程序将使用服务器的 MACHINE$ 帐户来访问 AD。

Use the System.DirectoryServices.DirectoryEntry class to read out the appropriate property of the user object. The constructor of DirectoryEntry requires that you have an LDAP path to the user. Getting the LDAP path can often be tricky though as IIS prefers handing over the SAM account name only. If you provide more details of what the user id you have looks like it is easier to point you in the right direction.

To do this the account which runs the ASP.NET application needs read access to the AD, which probably doesn't have by default. Changing the application pool to run under "NetworkService" is the easiest way if the web server belongs to the AD. The ASP.NET app will then use the MACHINE$ account of the server to access the AD.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文