设为首页收藏本站

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 856|回复: 3

Get a User's Full Name

[复制链接]
发表于 2012-2-6 10:39:12 | 显示全部楼层 |阅读模式
Use .NET 3.5's DirectoryServices namespace to quickly and easily get at a user's full name.

IntroductionThe System.DirectoryServices.AccountManagement namespace in .NET 3.5 gives you access to the UserPrinciple class which "Encapsulates principles that are user accounts." There is a .Current property which "Gets a user principle object that represents the current user under which the thread is running." Once you have this UserPrinciple object for the current user, you can get at all kinds of cool properties and methods.
BackgroundI wanted to display the current user's full name to see who is using my application and then log that to a table in SQL, how hard could it be!? I was using VB.NET for this particular project so I tried My.User.Name...whew that was pretty easy. On run, it returned "MyDomain\UsersInitials" since the username standard we have here are the initials. Getting initials was not very helpful in figuring out who was using my program (I'm too lazy to manually cross reference usernames, with full names in ADUC).
So I had to search around for quite a while and I couldn't find any articles or postings that quickly point to this class for user information. There were other people that had this question and they were turned to everything from using "WinNT://" queries, to using the Win32_UserAccount and the WMI Code Creator, to older articles saying you just can't do it!
Eventually I stumbled upon this article on MSDN.
I figured I'd post an article to maybe make it easier for people to find information on getting at current user properties.


Using the Code Using the code couldn't be easier. Just add a reference and using or imports statement to System.DirectoryServices.AccountManagement and enjoy.

  1. Imports System.DirectoryServices.AccountManagement
复制代码
  1. Dim userFullName As String = UserPrincipal.Current.DisplayName
复制代码


One caveat, as pointed out in a comment below, is that this should be wrapped with a Try...Catch block. If the current user is on a domain and can't reach the domain controller (network failure), he will receive a PrincipalServerDown exception. However if he is not on a domain, he will not receive this error on a network failure.

Also, if you change the username and don't log off, you will receive a NoMatchingPrincipleException since the .Current UserPrinciple name can't be found. This would probably never happen but you might as well Catch it.

If an exception occurs, I'd suggest just using the username from System.Environment.UserName or My.User.Name in VB.

Once you get at the .Current property, which returns a UserPrinciple class, you can access all kinds of properties on the current user.

http://www.codeproject.com/Articles/42282/Get-a-User-s-Full-Name
 楼主| 发表于 2012-2-6 10:42:23 | 显示全部楼层
Once you get at the .Current property, which returns a UserPrinciple class, you can access all kinds of properties on the current user. A table of properties is listed below (from here):

  NameDescription
AccountExpirationDateGets or sets a Nullable DateTime that specifies the date and time that the account expires. (Inherited from AuthenticablePrincipal.)
AccountLockoutTimeGets the Nullable DateTime that specifies the date and time that the account was locked out. (Inherited from AuthenticablePrincipal.)
AdvancedSearchFilterReturns an AdvancedSearchFilter object, for use with Query by Example to set read-only properties before passing the object to the PrincipalSearcher. (Overrides AuthenticablePrincipal..::.AdvancedSearchFilter.)
AllowReversiblePasswordEncryptionGets or sets a Boolean value that specifies whether reversible password encryption is enabled for this account. (Inherited from AuthenticablePrincipal.)
BadLogonCountGets the number of logon attempts using incorrect credentials for this account. (Inherited from AuthenticablePrincipal.)
CertificatesGets a X509Certificate2Collection that contains the X509 certificates for this account. (Inherited from AuthenticablePrincipal.)
ContextGets a principal context that is associated with the principal. (Inherited from Principal.)
ContextRawGets a principal context that is associated with this principal. (Inherited from Principal.)
ContextTypeGets the context type enumeration value that specifies the type of principal context associated with this principal. (Inherited from Principal.)
CurrentGets a user principal object that represents the current user under which the thread is running.
DelegationPermittedGets or sets a Nullable Boolean value that specifies whether the account may be delegated. (Inherited from AuthenticablePrincipal.)
DescriptionGets or sets the description of the principal. (Inherited from Principal.)
DisplayNameGets or sets the display name for this principal. (Inherited from Principal.)
DistinguishedNameGets the distinguished name (DN) for this principal. (Inherited from Principal.)
EmailAddressGets or sets the e-mail address for this account.
EmployeeIdGets or sets the employee ID for this user principal.
EnabledGets or sets a Nullable Boolean value that specifies whether this account is enabled for authentication. (Inherited from AuthenticablePrincipal.)
GivenNameGets or sets the given name for the user principal.

 楼主| 发表于 2012-2-6 10:43:34 | 显示全部楼层
GuidGets the GUID associated with this principal. (Inherited from Principal.)
HomeDirectoryGets or sets the home directory for this account. (Inherited from AuthenticablePrincipal.)
HomeDriveGets or sets the home drive for this account. (Inherited from AuthenticablePrincipal.)
LastBadPasswordAttemptGets the Nullable DateTime that specifies the date and time of the last incorrect password attempt on this account. (Inherited from AuthenticablePrincipal.)
LastLogonGets the Nullable DateTime that specifies the date and time of the last logon for this account. (Inherited from AuthenticablePrincipal.)
LastPasswordSetGets the Nullable DateTime that specifies the last date and time that the password was set for this account. (Inherited from AuthenticablePrincipal.)
MiddleNameGets or sets the middle name for the user principal.
NameGets or sets the name of this principal. (Inherited from Principal.)
PasswordNeverExpiresGets or sets a Boolean value that specifies whether the password expires for this account. (Inherited from AuthenticablePrincipal.)
PasswordNotRequiredGets or sets a Boolean value that specifies whether a password is required for this account. (Inherited from AuthenticablePrincipal.)
PermittedLogonTimesGets or sets the times when the principal can logon. (Inherited from AuthenticablePrincipal.)
PermittedWorkstationsGets the list of workstations that this principal is permitted to log into. (Inherited from AuthenticablePrincipal.)
SamAccountNameGets or sets the SAM account name for this principal. (Inherited from Principal.)
ScriptPathGets or sets the script path for this account. (Inherited from AuthenticablePrincipal.)
SidGets the Security ID (SID) of the principal. (Inherited from Principal.)
SmartcardLogonRequiredGets or sets a Boolean value that specifies whether a smartcard is required to log on to the account. (Inherited from AuthenticablePrincipal.)
StructuralObjectClassGets the structural object class directory attribute. (Inherited from Principal.)


 楼主| 发表于 2012-2-6 10:44:24 | 显示全部楼层
StructuralObjectClassGets the structural object class directory attribute. (Inherited from Principal.)
SurnameGets or sets the surname for the user principal.
UserCannotChangePasswordGets or sets a Boolean value that specifies whether the user can change the password for this account. Do not use this with a ComputerPrincipal. (Inherited from AuthenticablePrincipal.)
UserPrincipalNameGets or sets the user principal name (UPN) associated with this principal. (Inherited from Principal.)
VoiceTelephoneNumberGets or sets the voice telephone number for the user principal.

Points of InterestSome other interesting methods I found in there were ChangePassword, ExpirePasswordNow, GetGroups, UnlockAccount, SetPassword, and Save. I just haven't had the chance to check them out. Thanks .NET 3.5!

您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )  

GMT-8, 2025-12-12 20:15 , Processed in 0.016224 second(s), 16 queries .

Supported by Best Deal Online X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表