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 8067271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:14:15+00:00 2026-06-05T12:14:15+00:00

I am using the Intranet template for MVC 4 under the VS 2012 RC.

  • 0

I am using the Intranet template for MVC 4 under the VS 2012 RC. The top right hand corner shows domain\username, and I want it to show full name instead.

I was able to get this working in _Layout.cshtml. I included the following helper in the body –

@helper AccountName()
{       
    using (var context = new System.DirectoryServices.AccountManagement.PrincipalContext(System.DirectoryServices.AccountManagement.ContextType.Domain))
    {
        try
        {
            var principal = System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(context, User.Identity.Name); 
            @String.Concat(principal.GivenName, " ", principal.Surname) 
        }
        catch { }

    } 
}

(I also had to include System.DirectoryServices.AccountManagement in the References)

I could then use the helper in the greeting code as follows –

 <section id="login">
      Hello, <span class="username">@AccountName()</span>!
 </section>

This works ok, but I don’t like having the code in the _Layout.cshtml. I tried to put the helper in a seperate file under a Helpers directory, but @AccountName() no longer resolved then. Ideally the code should really be in a straight CS file.

Can anyone suggest a better way to set this up?

  • 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-05T12:14:17+00:00Added an answer on June 5, 2026 at 12:14 pm

    You can certainly put the code in a regular C# file, e.g.

    AccountUtil.AccountName()
    

    In that case, you need to include the namespace your helpers are in by placing

    @using My.App.Helpers  @* Change to your own namespace *@
    

    at the top of any view that wants to reference that code.

    Alternatively, you can make that namespace available to all views by editing the web.config file in the Views directory (not the one at the project level)

      <system.web.webPages.razor>
        <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <pages pageBaseType="System.Web.Mvc.WebViewPage">
          <namespaces>
            <!-- Don't edit out the stuff that's already here! -->
            <add namespace="My.App.Helpers" />  <!-- Add your own namespace here -->
          </namespaces>
        </pages>
      </system.web.webPages.razor>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on an intranet MVC web application, using Fluent NHibernate. As everyone knows,
i want to create an intranet mailing system using java.so suggest me which API
I open _Layout.cshtml in a new blank ASP.Net MVC 3 application (using the Intranet
I have an mvc intranet application using windows authentication. It currently has one controller
I would like to display an exchange calendar on our intranet using SharePoint or
I'm developing an internal web app on our company intranet using PHP. One section
Inside the LAN, we are good to go… folks access our intranet using http://
I am using windows authentication without impersonation on my company's intranet website with IIS7.
Im using the following to list the files in a directory in a intranet
We are about to migrate an intranet web application from using a proprietary forms-based

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.