Display user name in the header when logged in

Last post 11-17-2009 10:08 PM by chetan.sarode. 89 replies.

Sort Posts:

  • Display user name in the header when logged in

    01-04-2009, 5:33 PM
    Locked
    • Member
      228 point Member
    • qarjami
    • Member since 12-30-2008, 7:49 PM
    • Posts 209

    How do I display user name in the header when logged in? (right above the log out) I am assuming in the master page but I did not see any  header.aspx. Please help. need some direction. Also, few lines of code will be appreciated it too.

  • Re: Display user name in the header when logged in

    01-04-2009, 8:35 PM
    Answer
    Locked
    • Contributor
      6,301 point Contributor
    • ldechent
    • Member since 12-29-2006, 6:13 AM
    • Posts 1,567

     I believe there is a good chance this post will be marked as resolved the next time the original poster looks at what has been added.  Of course anyone is welcome to read on and comment.

    -----------------------

    I think the code below does it (and I will check it).  Do you use C#?

    MembershipUser membershipUser = Membership.GetUser();
    if (membershipUser != null)
    {
        string loggedinuser = Membership.GetUser().ToString();
        yourdiv.InnerHtml = loggedinuser;
    }

    for the above to work you need

    using System.Web.Security; // this enables Membership

    I'm assuming that you want to place the username somewhere and you have a way of doing it like with a div or a span or a label.  Is this correct?

    -Larry

    Larry Dechent - Sampson Coatings

    www.wemakebetterpaint.com has 29 examples (C# & VB) to help beginners with ASP.NET.
  • Re: Display user name in the header when logged in

    01-05-2009, 6:20 PM
    Locked
    • Member
      228 point Member
    • qarjami
    • Member since 12-30-2008, 7:49 PM
    • Posts 209

    Hi Larry,

    1. I am using c#.
    2. Where would I place the above code (in master)
    3. I am open to suggession as which to use (div,span or label)?

    Please suggest.

    Thanks a lot

  • 4 page example (8 pages if you count aspx and aspx.cs as two pages)

    01-05-2009, 8:32 PM
    Answer
    Locked
    • Contributor
      6,301 point Contributor
    • ldechent
    • Member since 12-29-2006, 6:13 AM
    • Posts 1,567

    Because the answer is "You can put it anywhere" (and that sounds vague and evasive) I'm going to try a different strategy.  I built a web site that has four pages (a masterpage and three more pages: Default.aspx, Login.aspx, AnotherPage.aspx).  I'm showing all four pages below, using a single line of asterisks between aspx and aspx.cs  and using four lines of asterisks between pages.  A post after this will discuss what to look for:

    • ***MasterPage.master
    • **************************************
    • **************************************
    • ************************************** 
      <%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
      
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head runat="server">
          <title></title>
          
          <style type="text/css" media="all">
          #topline {background-color: #DDF;}
          #maincontent {background-color: #FF8;}
          #bottomline {background-color: #F99;}
          </style>
          
          <asp:ContentPlaceHolder id="head" runat="server">
          </asp:ContentPlaceHolder>
      </head>
      <body>
          <form id="form1" runat="server">
          
              <div id="topline">
      
              <span id="spanid" runat="server" />
              <div id="divid" runat="server" />
              <asp:Label ID="Labelid" runat="server" Text="You need to login..."></asp:Label>
              
              </div>
              
              <br /><br />
              
              <div id="maincontent">
              <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
              
              </asp:ContentPlaceHolder>
              </div>
              
              <div id="bottomline">
              <p>I went to the ASP.NET Web Site Administration Tool and went to Security and for Select Authentication type I 
              changed it to 'From the internet'.</p>
              <p>That was the only change I made at the Web Site Administration Tool.</p>
              </div>
      
          </form>
      </body>
      </html>

    *****************************************

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.Security; // this enables Membership
    
    
    public partial class MasterPage : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            MembershipUser membershipUser = Membership.GetUser();
            if (membershipUser != null)
            {
                string loggedinuser = Membership.GetUser().ToString();
                divid.InnerHtml = "divid username: " + loggedinuser;
                spanid.InnerHtml = "spanid username: " + loggedinuser;
                Labelid.Text = "Labelid username: " + loggedinuser;
            }
            else
            {
                Labelid.Text = "I went through protected void Page_Load and membershipUser was null, sorry!";
            }
        }
    }
     
    • ***Default.aspx
    • **************************************
    • **************************************
    • ************************************** 
      <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
      
      <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
      </asp:Content>
      <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
      <p>This is the Default.aspx page.</p>
      <p>Now go to <a href="AnotherPage.aspx">AnotherPage.aspx</a></p>
      </asp:Content

    *********************************

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    }
     
    • ***Login.aspx
    • **************************************
    • **************************************
    • ************************************** 
      <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>
      
      <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
      </asp:Content>
      <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
      
          <table><tr>
      
          <td>
      
          <asp:Login ID="Login1" runat="server">
          </asp:Login>
          
              <asp:LoginStatus ID="LoginStatus1" runat="server" />
          
          </td>
          <td width="50"></td>
          <td>
          
          <asp:CreateUserWizard ID="CreateUserWizard1" runat="server">
              <WizardSteps>
                  <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">
                  </asp:CreateUserWizardStep>
                  <asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server">
                  </asp:CompleteWizardStep>
              </WizardSteps>
          </asp:CreateUserWizard>
          
          </td>
          
          </tr><table>
          
          
              <p>Now go to <a href="AnotherPage.aspx">AnotherPage.aspx</a>.</p>
      
      </asp:Content>

    *****************************************

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    }
     
    • ***AnotherPage.aspx
    • **************************************
    • **************************************
    • ************************************** 
      <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="AnotherPage.aspx.cs" Inherits="AnotherPage" %>
      
      <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
      </asp:Content>
      <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
      
      <p>This is another page.</p>
      <p>Go back to the <a href="Login.aspx">Login page</a> and log out.</p>
      
      </asp:Content>

    *********************************

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class AnotherPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    }
     
    Larry Dechent - Sampson Coatings

    www.wemakebetterpaint.com has 29 examples (C# & VB) to help beginners with ASP.NET.
  • Re: 4 page example (8 pages if you count aspx and aspx.cs as two pages)

    01-05-2009, 8:47 PM
    Answer
    Locked
    • Contributor
      6,301 point Contributor
    • ldechent
    • Member since 12-29-2006, 6:13 AM
    • Posts 1,567

    Admittedly, it's going to be tedious to build the four pages above to prove the example.  I estimate about 5 minutes of work to build the example.  However, after the example is created you will have a model system to which you can make alterations to see what happens as a result of the changes.

    It doesn't matter whether you use div or span or textbox so I thought, why not put in all three and prove it.

    Next, we needed several pages.  You said masterpage so I wanted this example to show that if you build the feature on the masterpage you'll never have to write anything about it on any other page.  I could have just said that, and it probably would have made sense, but it will be more real when you see it.

    We built the Default.aspx page next because if you didn't have it at some point the program would look for it and if it wasn't there you would get a white screen, and that causes panic.

    We then built the Login.aspx page (which also has a register tool) so that you can login and see how the line at the top changes.  The Login.aspx page also has a tool for logout.  This is helpful because you can login and logout and verify that the upper line is mirroring the changes without fail.

    I mentioned on the masterpage what I did with the Web Site Administration Tool (look for the hammer and world icon under Solution explorer--it's the icon furthest to the right--at least this is true for Visual Web Developer.  This note was also built there to fill space in the part that is the bottomline div on the masterpage.

    Note that we built three different div areas with three different colors.    This takes us back to the original question "where should I put it?  or in this case, where should I put them?"

    Once you are satisfied that the example works (with "them" in the blue area) move "them" to another place on the masterpage and then test the different pages again.  I think after you have moved it to three different places you will be convinced that it doesn't matter--and I think as an indirect benefit you will have a higher confidence for what you can do with a masterpage which will benefit you when you work with future designs.

    Given the amount of time that I put into building this, and the amount of time that you put into making it, I would recommend that you tell me after you have finished this, and maybe we use this example to do a few more things (it is important to mark the post as resolved and issue points both to show good faith and also because other persons seeing a "Not Resolved" and coming here and finding it resolved (after reading for five minutes) will feel something bad and I don't want any of them to be unhappy about wasting the five minutes.

    for my notes- this project is "TestLogin"

    Larry Dechent - Sampson Coatings

    www.wemakebetterpaint.com has 29 examples (C# & VB) to help beginners with ASP.NET.
  • Re: 4 page example (8 pages if you count aspx and aspx.cs as two pages)

    01-06-2009, 3:19 PM
    Answer
    Locked
    • Member
      228 point Member
    • qarjami
    • Member since 12-30-2008, 7:49 PM
    • Posts 209

    wow. great answers in detail. I will try your sample tonight and give you the feedback. and will mark this post completed.

    Thanks a lotttttt.

  • Re: Display user name in the header when logged in

    01-06-2009, 10:30 PM
    Answer
    Locked

    You can use this code

    MembershipUser membershipUser = Membership.GetUser();
    if (membershipUser != null)
    {
        string loggedinuser = Membership.GetUser().ToString();
        yourdiv.InnerHtml = loggedinuser;
    }

    also there is another option to get user name

    HttpContext.Current.User.Identity

    http://msdn.microsoft.com/en-us/library/system.web.httpcontext.user.aspx

    Chetan Sarode
    Software Engineer,
    Approva Systems Pvt Ltd,
    Pune, India.
  • Re: Display user name in the header when logged in

    01-06-2009, 11:33 PM
    Answer
    Locked
    • Member
      228 point Member
    • qarjami
    • Member since 12-30-2008, 7:49 PM
    • Posts 209

    Great job guys. It worked like charm. I am going to elaborate which order they need to take place.

    step1: Add a Label right above <div login> area in masterpage.master page. Lets call that lblShowUser
    step2: on load for masterpage.master.cs page put this code

         MembershipUser
    membershipUser = Membership.GetUser();
                if (membershipUser != null)
                   {
                     
    string loggedinuser = Membership.GetUser().ToString();
                      lblShowUser.Text =
    "Welcome " + loggedinuser;
                   }
                  
    else
                  
    {
                      lblShowUser.Text =
    "";
                   }

     

  • Re: Display user name in the header when logged in

    01-07-2009, 9:24 AM
    Locked
    • Contributor
      6,301 point Contributor
    • ldechent
    • Member since 12-29-2006, 6:13 AM
    • Posts 1,567

    OK, I'm going to go ahead and build a "second generation example."   What we did here was an experiment and it appears the "gamble" paid off, so I'd like to roll the dice again.  Would you like me to also include the code mentioned by the second poster, Chetan?  The reason I bring it up, I'm planning to anyway, simply so we have the value of the experience of trying something new and exploring it--but also, if you used it and you found something you liked better about it then we could use that specifically.

    I don't want to promise it but I'm thinking the next generation could have a folder called Admin which has stuff that is accessible only if a logged in person happens to have been approved as an Admin role person.  Maybe also have a customer Role too.  Please note (if you haven't come across this already)--when a person registers they immediately have a status change--but maybe that person who registered is your business competitor so you want to create a role for Customers so that you have to approve them before they actually are granted access (in this case to a folder called Customers).

    I haven't done it for a while but my memory says we can do something in the web.config file to specify folder access and then maybe once a day you go into the page (can't remember the name but it is the hammer and world icon furthest to the right under Solution Explorer) and you look at people who are registered and you give them approvals there (I'm pretty sure this can also be done programmatically.)  Are you working on a "local machine" like a laptop or a desktop, or are you working on server (like a server at work or a commercial web site provider)?

    OK, this new example might have even more files to copy but, hey, we're ready to gamble that it will be worth it, right? (and the gamble is that another person coming here for the original question is going to want to go down this same path).

    Also, when you reply post, keep telling me what you would like.  It helps to have several things because I'll start with what I know right away and then I'll work on what I need to learn while doing the rest (I'll also have a long page that has all the files and I'll update it periodically until all are finished).

    -Larry

    Larry Dechent - Sampson Coatings

    www.wemakebetterpaint.com has 29 examples (C# & VB) to help beginners with ASP.NET.
  • Re: Display user name in the header when logged in

    01-15-2009, 10:28 PM
    Locked

    Nice one

    Chetan Sarode
    Software Engineer,
    Approva Systems Pvt Ltd,
    Pune, India.
  • Re: Display user name in the header when logged in

    02-22-2009, 2:59 AM
    Answer
    Locked
    • Member
      12 point Member
    • richsoryu
    • Member since 02-22-2009, 7:52 AM
    • Posts 1

    Bulding off this code, I put this code in my master page:

      

    1    <div id="userLoggedIn" runat="server">
    2    </div>
    

     

    Then in my Page_Load method, I put this code: 

    1            // if user is logged in, set the innerhtml of the userLoggedIn div to show the user name
    2            // else, set the text telling the user how to log in.
    3            if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
    4            {
    5                string userName = "<p>Hello, " + System.Web.HttpContext.Current.User.Identity.Name +
    6                    "! Not you? <a href=\"Default.aspx\">Click here to log in.</a><p>";
    7                userLoggedIn.InnerHtml = userName;
    8            }
    9            else
    10           { 
    11               string userName = "<p><a href=\"week2.aspx\">Click here to log in.</a><p>";
    12               userLoggedIn.InnerHtml = userName; 
    13           }
    

     

    Now the user name displays when the user is logged in.

    Thank you for answering this guy's questions. :)

  • Re: Display user name in the header when logged in

    02-23-2009, 10:22 PM
    Locked

    Thats nice

    Chetan Sarode
    Software Engineer,
    Approva Systems Pvt Ltd,
    Pune, India.
  • Re: Display user name in the header when logged in

    08-25-2009, 5:10 PM
    Locked
    • Member
      98 point Member
    • Cadeey
    • Member since 01-30-2008, 8:18 PM
    • Posts 53

     Hello-

    I saw this post which is pretty much what I am looking for except that I'm not using the Membership.  I am validating users against oracle table.  Is there a way to display username as they login to the page when connecting to oracle table???

     

     

  • Re: Display user name in the header when logged in

    08-25-2009, 7:32 PM
    Answer
    Locked
    • Member
      228 point Member
    • qarjami
    • Member since 12-30-2008, 7:49 PM
    • Posts 209

     quickest way to do it use session state to store name. (of course, u should b using stored procedure to do this).

  • Re: Display user name in the header when logged in

    08-25-2009, 11:24 PM
    Answer
    Locked

     Afetr authenticating the user, store that user name in Session. Add label t Master Page then set the text to it

    Label.Text = Session["UserName"].ToString();

    Chetan Sarode
    Software Engineer,
    Approva Systems Pvt Ltd,
    Pune, India.
Page 1 of 6 (90 items) 1 2 3 4 5 Next > ... Last »