Display username in LoginView

The default website template when creating an ASP.NET project in Visual Studio shows the username of the user when logging into LoginView. I would like to replace this with the username. I have it stored in the database, so on the Site.master page I tried the following (on the onload page):

MembershipUser user = Membership.GetUser(); string id = user.ProviderUserKey.ToString(); SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Connection"].ConnectionString); try { using (connection) { using (SqlCommand con_get = new SqlCommand("SELECT firstname FROM x WHERE userId='" + id + "'", connection)) //Executes SQL command { try { connection.Open(); //Opens the database connection using (SqlDataReader reader = con_get.ExecuteReader()) { if (reader != null) { while (reader.Read()) { Label label1 = (Label)sender; label1.Text = reader["x"].ToString(); } } } } catch { } finally { connection.Close(); //Closes the database connection } } } } catch { } 

This does not work at the moment. I also tried not to use the sender and just tried using label1.Text = reader ["x"]. ToString (); but it didn’t work.

Does anyone have any suggestions on how I can make this work?

Also, is this the right approach? Of course, there is a more efficient way to load the first name and not reload it every time the user goes to another page (therefore, reduces the number of database queries)?

+4
source share
1 answer

I think that you are faced with the fact that the label is on the main page.

 protected void Page_Load(object sender, EventArgs e) { if(!Page.IsPostBack) { var welcomeLabel = Page.Master.FindControl("lblWelcome") as Label; SetName(welcomeLabel); } } 

Here's a handwritten login template:

 protected void SetName(Label welcomeLabel) { //this is the type of thing you'd probably wanna do in the Global.asax on session start if (Session["userName"] == null || !Login()) return; //session variable is empty and the login attempt failed, give up var usersName = Session["userName"].ToString(); welcomeLabel.Text = usersName; } protected bool Login() { const string query = "SELECT Name FROM Users WHERE Password = @Password AND UserName = @UserName"; using (var conn = new SqlConnection(connectionString)) { using (var comm = new SqlCommand(query, conn)) { comm.CommandType = CommandType.Text; comm.Parameters.Add(new SqlParameter("@Password", password)); //or get this from a control or wherever comm.Parameters.Add(new SqlParameter("@UserName", userName)); //or get this from a control or wherever conn.Open(); var name = (string)comm.ExecuteScalar(); if (!string.IsNullOrEmpty(name)) { Session["userName"] = name; return true; } //"Login information is wrong or user doesn't exist. return false; } } } 

So, all we do is search our database for a match between username and password. The username must be unique, so it seems like your primary key, the AND filter just ensures that the passwords match the username. Plain.

Generally speaking, I think you should keep a little more user information in the session to make it worthwhile. Usually I create some form of user object so that I can hit it whenever I need it, and then all of it is fully accessible through the user object. I think the Windows Membership classes are trying to help, but I do not like to use them if I do not authenticate with Windows. But that is just preference.

+1
source

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


All Articles