Search code examples
asp.netmaster-pages

How to add Welcome username in all pages


How to display “Welcome %username%” in all the webpages, is there any possibly to make it in the Master page? if so, where in master page should i write the code, in page_load??


Solution

  • There is a login name control in asp.net to show this information. Just place it on your Master page like this:

    <asp:LoginView runat="server">
        <LoggedInTemplate>
            Welcome <asp:LoginName runat="server" />
        </LoggedInTemplate>
    </asp:LoginView>
    

    It will show login name only for logged in users.

    If you need this information somewhere else, you can use current user identity to get the name using current http context:

    if (HttpContext.Current.User.IsAuthenticated)
    {
         string currentUserName = HttpContext.Current.User.Identity.Name;
    }