LoginName Control to Display Full Name instead of username

Question:

  • How do I modify the text in LoginName Control.
  • I want to display "Full Name" of the currently logged-in User but the LoginName control displays the username (User Id) that user uses to login.
  • I want to display Full Name along with username in LoginName.

What value LoginName Control displays?

MSDN: "Displays the value of the System.Web.UI.Page.User.Identity.Name property."

So by default with FormsAuthentication it will LoginName will display the username that user used to login. And with Windows Authentication the currenlty logged-in Windows UserName.(Assuming Anonymous Identification is OFF and site requires Login)

LoginName.FormatString Property

Yes, FormatString property of LoginName is what you can use to Modify the text that is displayed.

MSDN: "The FormatString property contains a standard text format string that displays the user's name on the Web page. The string "{0}" indicates where in the string the user's name is inserted."

Suppose you are logged-in as "johndoe". Below are some sample values for FormatString and the corresponding OutPut.

FormatString Output
---------------------- --------------
"{0}" (i.e default) johndoe
Empty i.e. not defined johndoe
"Welcome, {0}" Welcome, johndoe
"Welcome to my site" Welcome to my site

What do we get from that?

So now if you want to display Full Name of the user who is logged-in inside LoginName control then in the page_Load of the page (Could be a MasterPage) you should set the FormatString Property like below:

<asp:LoginName ID="LoginName1" runat="server" />

protected void Page_Load(object sender, EventArgs e)

{

//this can come from anywhere like session, database

string fullName = "ABC XYZ";

 

LoginName1.FormatString = "{0}" + " - " + fullName ; //output: johndoe - ABC XYZ

 

LoginName1.FormatString = fullName; // output: ABC XYZ

}

 Yes, thats it. Play around it to display as you want.

Thanks for reading.

References:

 

8 Comments

  • hi guru,

    Thanks a lot !! your post has helped me solve my problem in modifying the display format for loginname control :)

  • ... very nice howto.

    how did you call the fullname?

    thx very much

  • marvin,
    You will need to get the fullname from where you stored it...In example above I have hard-coded as "ABC XYZ".

  • To get the Fullname from the database, I need to get the UserId of the logged in user. Now If I place this code in the master page's Page_load event, I get error. System does not find the UserId because the user is not logged in yet.

    Can u seggest me anything?? &nbsp;

  • Reza,

    You can put your code that needs UserId into the if block like:
    if(Request.IsAuthenticated){
    //Then get userID and get fullname
    }

  • great explnation to how to display Full Name instead of username

  • public string valor;

    public DisplayName(string txtUsername)
    {

    PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
    UserPrincipal user = UserPrincipal.FindByIdentity(ctx, txtUsername);

    valor = user.DisplayName.ToString();

    }
    /////////////////////////////////////////////////////////////////////
    if (true == Context.User.Identity.IsAuthenticated)
    {
    DisplayName disp = new DisplayName(Context.User.Identity.Name);
    LoginName loginName = HeadLoginView.FindControl("HeadLoginName") as LoginName;
    if (loginName != null)
    {
    loginName.FormatString = disp.valor.ToString();
    }
    NavigationMenu.Visible = true;
    }

  • Thanks for finally writing about &gt;LoginName Control to Display Full Name instead of username - Guru Sarkar's Blog &lt;Liked it!

Comments have been disabled for this content.