Why does the code below work fine when I run my local web application, but not when I install it on the IIS server?
using (HostingEnvironment.Impersonate()) { UserPrincipal activeUser = UserPrincipal.Current; String activeUserSid = activeUser.Sid.ToString(); String activeUserUPN = activeUser.UserPrincipalName; }
Please do not suggest me stick with HttpContext.Current.User , as it does not provide access to SID or UPN without additional calls to Active Directory.
The web application will be used by authenticated Windows users from three separate domains, the web server is located in the fourth domain. The application pool is configured to work under the NetworkService identifier, and the identity of the personification of authenticity is set in the configuration of the web application.
Error message when starting up in IIS:
Error in Page_Load (): UserPrincipal.Current.
System.InvalidCastException: Cannot reset an object of type 'System.DirectoryServices.AccountManagement.GroupPrincipal' to enter a type of 'System.DirectoryServices.AccountManagement.UserPrincipal.
in System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity (PrincipalContext context, IdentityType identityType, String identityValue)
in System.DirectoryServices.AccountManagement.UserPrincipal.get_Current ()
in webapp.Details.Default.Page_Load (object sender, EventArgs e)
EDIT : I tried the following, and unfortunately I got the same error.
UserPrincipal userPrincipal = UserPrincipal.Current; Response.Write(userPrincipal.Name);
Principal userOrGroup = UserPrincipal.Current; Response.Write(userOrGroup.Name);
RichardD
source share