Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8178075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:40:42+00:00 2026-06-06T23:40:42+00:00

I am building an Intranet application and I am using Windows authentication. I have

  • 0

I am building an Intranet application and I am using Windows authentication. I have this setup and working and I am able to see the current users username via @Context.User.Identity.Name

I would like to query the Active Directory to retrieve the Display Name, email address, etc.

I have looked at System.DirectoryServices.AccountManagement and from this have created a basic example. I am not clear though on how to display this information throughout my application.

What I have so far is as follows:

public class searchAD
{

    // Establish Domain Context
    PrincipalContext domainContext = new PrincipalContext(ContextType.Domain);

    public searchAD()
    {
        // find the user 
        UserPrincipal user = UserPrincipal.FindByIdentity(domainContext, HttpContext.Current.User.Identity.Name);

        if (user != null)
        {
            var userGuid = user.Guid;
            var DisplayName = user.DisplayName;
            var GivenName = user.GivenName;
            var EmailAddress = user.EmailAddress;
        } 

    }

}

I am assuming I need to create a ViewModel like so?

public class domainContext
{
    public Nullable<Guid> userGuid { get; set; }
    public string DisplayName { get; set; }
    public string GivenName { get; set; }
    public string EmailAddress { get; set; }
}

I want to be able to access this information in various sections of the application.

I am not getting any errors just seem to be missing something that ties all this together.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T23:40:44+00:00Added an answer on June 6, 2026 at 11:40 pm

    You’re missing the windows identity part. Also, remember to dispose of any AD objects you are using:

    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;
        }
    }
    

    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).

    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:

    protected override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        base.OnActionExecuted(filterContext);
        ViewData["BaseController_DomainContext"] = AdLookup.GetUserDetails();
    }
    

    Then you can access this on any of your views rendered from this any inheriting controller by using:

    @{
        DomainContext domainContext = (DomainContext)ViewData["BaseController_DomainContext"];
    }
    
    Welcome back: @domainContext.DisplayName
    

    Of course you’ll run into problems with nulls, but you could handle that in your GetUserDetails method with some sort of dummy DomainContext.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building an internal (intranet) MVC3 application using Windows Authentication and a custom role
I am building a local intranet web application which uses the current user's AD
We are currently building a mid-sized intranet application using MVC3. One requirement is the
We have some corporate intranet users using a WinForms app to work on a
I am in a process of building an intranet application entirely using silverlight 3.
I'm building an Intranet application based on Ext JS and webservices. Users are authorized
I'm building a new Intranet Portal with SharePoint 2007 that will have News, Site
Building my first SL MVVM application (Silverlight4 RC) and have some issues i don't
I am building an Ajax Based application that will run on our local intranet
Building a sample ASP.NET MVC app. Using the Membership API for authentication. For whatever

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.