get UserName without the 20 char Limit

2020-05-07 13:14发布

问题:

I have to read the user log in my app even with run as and the name created in domain in windows server 2012have 23 char, i used :

Environment.UserName


new System.Security.Principal.WindowsPrincipal(System.Security.Principal.WindowsIdentity.GetCurrent()).Identity.Name;

System.Security.Principal.WindowsIdentity.GetCurrent().Name

but all this give me the name without the 3 last char I used :

UserPrincipal.Current.UserPrincipalName
UserPrincipal.Current.Name

but give me the displayed name

how can i get the full length login name

回答1:

This is a hard limit on the samAccountName property:

"You can't modify the length of the user's samAccountName, it is a defined value within the schema and is set to 20."

See: this page for source

Maybe you could try the UserPrincipalname

System.DirectoryServices.AccountManagement.UserPrincipal.Current().UserPrincipalName

you need to add a reference to the AccountManagement Namespace