Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're missing the windows identity part. Also, remember to dispose of any AD objects you are using:</p> <pre><code>public class AdLookup { public static DomainContext GetUserDetails() { using (PrincipalContext pc = new PrincipalContext(ContextType.Domain)) { IPrincipal principal = HttpContext.Current.User; WindowsIdentity identity = ((WindowsIdentity)principal.Identity); UserPrincipal user = UserPrincipal.FindByIdentity(pc, identity.Name); if (principal != null) { return new DomainContext() { userGuid = user.Guid, DisplayName = user.DisplayName, GivenName = user.GivenName, EmailAddress = user.EmailAddress }; } } return null; } } </code></pre> <p>The best thing to do so you can access this everyone within your application is to create a class called 'BaseController' which all your other controllers inherit from, and put this code on there (ideally as a property called DomainContext).</p> <p>To use this on all over your application, you can also add a method on the base controller to add this information to the ViewData:</p> <pre><code>protected override void OnActionExecuted(ActionExecutedContext filterContext) { base.OnActionExecuted(filterContext); ViewData["BaseController_DomainContext"] = AdLookup.GetUserDetails(); } </code></pre> <p>Then you can access this on any of your views rendered from this any inheriting controller by using:</p> <pre><code>@{ DomainContext domainContext = (DomainContext)ViewData["BaseController_DomainContext"]; } Welcome back: @domainContext.DisplayName </code></pre> <p>Of course you'll run into problems with nulls, but you could handle that in your GetUserDetails method with some sort of dummy DomainContext.</p> <p>This is just one way of doing it, alternatively you could create a base view model that all your view models inherit from and access them directly in the view, but I've found this way to be simpler as you don't add any complexity to your view models.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload