Display full name in ASP.NET LoginName control

I have a .aspx page using a user authentication login control. I was wondering if it is possible to get the message "Welcome [FirstName] [LastName]" using the LoginName control, and not the [UserName] that is accessed by default.

I am going to save this information in a Session object if this is not possible.

Thanks!

+3
source share
4 answers

You need to override the method RenderContentsor create your own LoginName element. Something like this will do the trick:

protected override void RenderContents(HtmlTextWriter writer)
{
      if (string.IsNullOrEmpty(Profile.FullName))
            return;

      nameToDisplay = HttpUtility.HtmlEncode(Profile.FullName);
      string formatExpression = this.FormatString;
      if (formatExpression .Length == 0)
      {
            writer.Write(nameToDisplay);
      }
      else
      {
            try
            {
                  writer.Write(string.Format(CultureInfo.CurrentCulture, formatExpression, new object[1] { nameToDisplay });
            }
            catch (FormatException exception)
            {
                  throw new FormatException("Invalid FormatString", exception1);
            }
      }
}

Also see here a short article on working with LoginName .

+2

LoginName , Masterpage.aspx .

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

page_load .cs

protected void Page_Load(object sender, EventArgs e)
{
    //this can come from anywhere like session, database
    string fullName = "ABC XYZ";
    LoginName1.FormatString = "welcome" + " - " + fullName ; //output: welcome - ABC XYZ

    or

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

???

+2

, . : . , , .

0

Source: https://habr.com/ru/post/1704564/


All Articles