1

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;
salsayk
  • 21
  • 3
  • Have you added the reference to `System.DirectoryServices.AccountManagement.dll`? (Though I believe you might had, because that's the reason your code might be compiling); but, still asking... Also, what .NET framework do you have installed? – Am_I_Helpful Feb 06 '18 at 10:28

0 Answers0