-1

i want to get current windows user full name not with domain name. am using below code to get user name but its giving name with domainname.

string UserName = Request.ServerVariables["AUTH_USER"];

string a=System.Web.HttpContext.Current.User.Identity.Name; string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

in above 3 statements am getting like APAC\san123. but here i want my fullname. here am using mvc with c#.

sandeep
  • 449
  • 2
  • 7
  • 22
  • 1
    Does `Environment.UserName` not fit your requirements? https://learn.microsoft.com/en-us/dotnet/api/system.environment.username?view=net-5.0 – ConnorTJ Sep 29 '21 at 09:24
  • 1
    Is the full name one of the items of data known to the identity system? – Caius Jard Sep 29 '21 at 09:30
  • @ConnorTJ `Environment.UserName` would be the name of the account running the application (e.g. an IIS AppPool), not the client. – Martin Costello Sep 29 '21 at 09:31
  • @MartinCostello ahh, please ignore my confusion, I must have misread the question, in that case, isn't this the same question answered in: https://stackoverflow.com/a/263853/13108684 or this question: https://stackoverflow.com/a/330408/13108684 – ConnorTJ Sep 29 '21 at 09:34
  • @ConnorTJ The OP is already using `User.Identity.Name`, it's part of the question. The actual question has been asked before, yes, I've just closed as a duplicate – Camilo Terevinto Sep 29 '21 at 09:50

1 Answers1

1

You can get the users full name from the active directory (using System.DirectoryServices.AccountManagement):

PrincipalContext context = new PrincipalContext(ContextType.Domain,Environment.UserDomainName);
string loginName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
UserPrincipal user = UserPrincipal.FindByIdentity(context, loginName);
string userName = user.Name; //=user.GivenName + " " + user.Surname;
Kai Thoma
  • 512
  • 4
  • 14