I am trying to get the user.UserPrincipalName by using the System.Web.HttpContext.Current.User.Identity.Name. I have the following code:
string[] parts = System.Web.HttpContext.Current.User.Identity.Name.Split(new char[] { '\\' });
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
_log.Info($"DNS Domain={((System.DirectoryServices.AccountManagement.ADStoreCtx)ctx.userCtx).DnsDomainName}");
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, parts[1]);
return user.UserPrincipalName;
While using the VS 2017 watch on the ctx object (after 2nd row assignment) I got the DnsDomainName which I would like to log. BUT, When I try to copy the expression which extracts the value and put it in code I get the following error: The type or namespace name 'ADStoreCtx' does not exist in the namespace 'System.DirectoryServices.AccountManagement' (are you missing an assembly reference?). There is no "Suggested fix" for it. I am trying to figure out how come that in the "watch" it does have this but not in the code. What reference should I add or what needs to be configured so I'll be able to access the property I am searching for ("DnsDomainName"). By the way, The ctx.Name and ctx.Username are nullso I cannot use them. Thanks in advance, Yosy p.s.: I have the using statements for both:
using System.DirectoryServices.AccountManagement;
using System.DirectoryServices;