Mina N
Mina N

Reputation: 231

get current user full name in asp.net

I have below code which works perfect but when i deploy it in IIS, it doesn't work.

DirectoryEntry de = new DirectoryEntry("WinNT://" + Environment.UserDomainName + "/" + Environment.UserName);
return de.Properties["fullName"].Value.ToString();

any help would be appreciated.

Upvotes: 2

Views: 8358

Answers (4)

Tawab Wakil
Tawab Wakil

Reputation: 2333

If using Windows authentication in a domain context, this solution should work in both the debugger and when deployed to IIS to get the current user's first/last name. Add a reference to System.DirectoryServices.AccountManagement first.

string currentUserID = HttpContext.Current.User.Identity.Name;

using (PrincipalContext adContext = new PrincipalContext(ContextType.Domain))
{
    UserPrincipal currentUser = UserPrincipal.FindByIdentity(adContext, currentUserID);
    return string.Format("{0} {1}", currentUser.GivenName, currentUser.Surname);
}

Upvotes: 0

codebased
codebased

Reputation: 7073

As per the .Net 4.5 recommendation you should always use ClaimsPrincipal. I am giving you a following program sample that I have just ran through console app and it has worked perfectly.

I have used an extension method here and attached to the claimsprincipal then now you can access anywhere.

private static void Main(string[] args)
        {

            var fullName = ClaimsPrincipal.Current.GetFullName();
        }

        public static string GetFullName(this ClaimsPrincipal principal)
        {

            if (!principal.HasClaim(claim => claim.Type == ClaimTypes.Surname) ||
                !principal.HasClaim(claim => claim.Type == ClaimTypes.GivenName))
            {
                var dcUsername = principal.Identity;
                // get the value from dc.
                var de = new DirectoryEntry("WinNT://" + dcUsername);

                var claims = new List<Claim> {new Claim(ClaimTypes.GivenName, ""), new Claim(ClaimTypes.Surname, "")};
                var id = principal.Identities.First();
                id.AddClaims(claims);
            }

            var givenName = principal.FindFirst(ClaimTypes.GivenName).Value;
            var surname = principal.FindFirst(ClaimTypes.Surname).Value;

            return string.Format("{0} {1}", givenName, surname);
        }

Upvotes: 1

Kiran Varsani
Kiran Varsani

Reputation: 587

Environment.UserName normally returns ApplicationPool user when hosted on the IIS. Try to use HttpContext.User.Identity.Name to get the username with domain.

For example,

string userName = HttpContext.User.Identity.Name;
DirectoryEntry de = new DirectoryEntry("WinNT://" + userName);
return de.Properties["fullName"].Value.ToString();

Upvotes: 0

mason
mason

Reputation: 32699

Environment contains information about the environment on the server, so you'll just get the username of the process your web application is running as. That's not what you want.

Instead, you should user User.Identity.Name to retrieve the user's username, then you can use that to obtain the user's name from AD.

Upvotes: 1

Related Questions