繁体   English   中英

如何在 vb.net windows 应用程序中获取当前域用户地址、城市、邮政编码的详细信息?

[英]How to get current domain user address,city,postal code details in vb.net windows application?

我需要从我的用户帐户中获取当前登录的用户地址、城市、邮政编码详细信息。

现在使用域用户登录,现在我得到了我的用户名、显示名称、电子邮件地址。 但我还需要用户地址、邮政编码、城市、州。 那么我如何通过 vb.net 代码或其他 c# 获得它。

您所说的信息在 Active Directory 中的 UserPrincipal 对象上不可用。 查看UserPrincipal 属性

如果您正在谈论从其他地方获取此信息,那么您必须告诉我们它在哪里。

我知道这是一个旧帖子,但以防万一它可以帮助其他人。 您可以使用DirectoryEntry对象而不是 UserPrincipal 来获取所有 AD 用户属性。

这是 C# 中的一个片段,用于使用 UserPrincipal 和 DirectoryEntries 获取属性,但我认为您可以在 VB.NET 中轻松重写

PrincipalContext context = new PrincipalContext(ContextType.Domain, server, username, password);
UserPrincipal userPrincipal = new UserPrincipal(context);
userPrincipal.UserPrincipalName = userPrincipalName;

PrincipalSearcher searcher = new PrincipalSearcher(userPrincipal);
UserPrincipal foundUserPrincipal = (UserPrincipal)searcher.FindOne();
searcher.Dispose();

if (foundUserPrincipal != null)
{
    string ldapUserPath = $"LDAP://{server}/{foundUserPrincipal.DistinguishedName}"
    DirectoryEntry directoryEntryUser = new DirectoryEntry(ldapUserPath, username, password);
    Console.Write($"Postal Code: {directoryEntryUser.properties["PostalCode"].value}");
    Console.Write($"City: {directoryEntryUser.properties["l"].value}");
    Console.Write($"Address: {directoryEntryUser.properties["streetAddress"].value}");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM