System.DirectoryServices.AccountManagement.UserPrincipal - localhost but not iis

I had a lot of issues deploying UserPrincipal.Current and still don't fully understand why.

I finally ended up using PrincipalSearcher, and created the following function to do what I thought UserPrincipal.Current was doing.

private UserPrincipal GetActiveDirectoryUser(string userName)
{
    using(var ctx = new PrincipalContext(ContextType.Domain))
    using(var user = new UserPrincipal(ctx) { SamAccountName = userName})
    using(var searcher = new PrincipalSearcher(user))
    {
        return searcher.FindOne() as UserPrincipal;
    }
}

And I passed System.Web.HttpContext.Current.User.Identity.Name into that method as the userName.


It seems like need some other method to determine user.
Here description from msdn for property:
"Gets a user principal object that represents the current user under which the thread is running."
So, UserPrincipal.Current returns user under what IIS running.

http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.userprincipal.aspx