[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Display username instead of user id.

Posted on 2008-02-10
8
Medium Priority
?
821 Views
Last Modified: 2013-12-17
I have this?

FormsAuthentication.RedirectFromLoginPage(myUser.userID.ToString(), chkRememberPassword.Checked);

I need to display the username, but at the same time when I go to the default page
I acquire the userID. I can do myUser.UserName above. But I will need the ID to get the information about the user.

<asp:LoginView ID="LoginView" runat="server">
                              <AnonymousTemplate>
                                    <asp:HyperLink ID="LoginLink" runat="server" NavigateUrl="~/Members/Login.aspx">Login</asp:HyperLink>
                                    |
                                    <asp:HyperLink ID="RegisterLink" runat="server" NavigateUrl="~/Members/Registration.aspx">Register</asp:HyperLink>
                              </AnonymousTemplate>
                              <LoggedInTemplate>
                                    Welcome,
                                    <asp:LoginName ID="MemberName" runat="server" />
                                    |
                                    <asp:LoginStatus ID="MemberLoginStatus" runat="server" />
                              </LoggedInTemplate>
                        </asp:LoginView>


It displays the id instead of the username
 
 // Only attempt a login if all form fields on the page are valid
        if (Page.IsValid == true)
        {
 
            // Attempt to Validate User Credentials using CustomersDB
            string sSessionID = this.Session.SessionID;
 
            UserDB.UsersDB user = new UserDB.UsersDB();
            UserDB.UserDetails myUser = user.Login(txtUserName.Text, txtPassword.Text, sSessionID);
 
            if (myUser != null)
            {
 
 
                // Cookie with User Name
                if (!chkRememberPassword.Checked)
                {
                    // Store the user's fullname in a cookie for personalization purposes
                    Response.Cookies["UserName"].Value = myUser.sUserName;
                }
                else
                {
                    // Persist the cookie also up until 12:00AM since
                    // we need to allow user to rec points for loggin in
                    Response.Cookies["UserName"].Value = myUser.sUserName;
                    Response.Cookies["UserName"].Expires = DateTime.Now + new TimeSpan(365, 0, 0, 0);
                }
 
                //FormsAuthentication.SetAuthCookie(myUser.userID.ToString(),false);
                FormsAuthentication.RedirectFromLoginPage(myUser.userID.ToString(), chkRememberPassword.Checked);
 
 
 
 
            }
            else
            {
                FailureText.Text = "Login Failed!";
            }
 
        }

Open in new window

0
Comment
Question by:mathieu_cupryk
  • 4
  • 4
8 Comments
 
LVL 7

Expert Comment

by:lucky_james
ID: 20864015
>> FormsAuthentication.RedirectFromLoginPage(myUser.userID.ToString(),
This will redirect to some other page and from this point onwards you want to display user name. Right?

Solution:
1. You have already put that in the cookie. All you need to do is to fetch that name from cookie.
2. You can also make use of query string while accessing the next page.
for more about query strings, check out:
http://msdn.microsoft.com/msdnmag/issues/07/03/CuttingEdge/

3. You can make use of Session variables in your code behind.

Hope it helps.

James
0
 

Author Comment

by:mathieu_cupryk
ID: 20865982
it's all nice to give me a link. But an explanation would be nice.
0
 
LVL 7

Expert Comment

by:lucky_james
ID: 20866036
Your problem can be interpreted as the making available a data in later pages while the data fetch in a page navigated before.

let me explain it further:
you browsed a page A where you got data A. Now you want to get data A in the later pages you will be browsing through.

you can do it by 3 means:
1. Use a cookie. store the value in that cookie. later fetch the value from the same cookie.
2. Pass on to later pages a query string. it is something which gets appended in your url link in address bar.
3. use session variables. It is something like a bag of property which will remain till the session is there.

1 should not be followed as it comes up with a clause that users should keep the use of cookies enabled.

let me know if you need some more explanation.


James
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 

Author Comment

by:mathieu_cupryk
ID: 20866055
Cookies enabled?
I can deal with session variables I just do not know how to populate.
I just do not know how to populate the following with the user name.
<asp:LoginName ID="MemberName" runat="server" />
                                    |
                                    <asp:LoginStatus ID="MemberLoginStatus" runat="server" />
instead of the userid.
0
 
LVL 7

Expert Comment

by:lucky_james
ID: 20866073
you can put it as:
Session["your_session_variable_name"] = whatever_value;

check out:
http://www.exforsys.com/tutorials/asp.net-2.0/asp.net-2.0-customizing-the-session-state-mechanism.html

0
 

Author Comment

by:mathieu_cupryk
ID: 20866086
What I am trying to say is I do not know this control
<asp:LoginName ID="MemberName" runat="server" />
?
0
 
LVL 7

Accepted Solution

by:
lucky_james earned 2000 total points
ID: 20866137
it is System.Web.UI.Page.User.Identity.Name

ok, so here is the problem u r facing:
you want to fetch the user name in web control. right?

you can put the value in this System.Web.UI.Page.User.Identity.Name control.

check out:
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.loginname.aspx
0
 

Author Comment

by:mathieu_cupryk
ID: 20871598
I use it in
void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                UserDB.UsersDB user = new UserDB.UsersDB();

                SqlDataReader reader = user.GetUserInfo(Int32.Parse(Page.User.Identity.Name));

                while (reader.Read())
                {
                    lblUserName.Text = reader.GetString(1);
                    lblFirstName.Text = reader.GetString(2);
                    lblLastName.Text = reader.GetString(3);
                    lblZodiacSign.Text = reader.GetString(6);
                    lblAge.Text = reader.GetInt32(7).ToString();
                    lblGender.Text = (reader.GetInt32(8) == 0) ? "Male" : "Female";
                    lblDateOfBirth.Text = reader.GetDateTime(9).ToString("d");
                    lblLastOnline.Text = reader.GetDateTime(15).ToString();
                    lblLocation.Text =  reader.GetString(22).ToString() + ", " + GetCountryName(reader.GetString(19));


                }
            }

         
        }
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
There is a wide range of advantages associated with the use of ASP.NET. This is why this programming framework is used to create excellent enterprise-class websites, technologies, and web applications.
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses
Course of the Month7 days, 19 hours left to enroll

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question