0
var query = db.Users.Where(x => x.UserName == username.Text && x.PassWord == password.Text)
                    .FirstOrDefault();

if (query != null)
{
    MessageBox.Show("sign in successful", "success", MessageBoxButtons.OK, MessageBoxIcon.Information);

    HomeScreen hs = new HomeScreen();

    hs.labelControl1.Text = query.UserName;
    hs.pictureEdit1.EditValue = query.Image;

    this.Hide();
    hs.Show();
}

What I am doing is showing username at homescreen form but when I go to another form and come back to homescreen form it does not show user name. What I want to do/show is username all time whether I go to another form and later comeback to homescreen. How is this possible?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

The quick & dirty as a starting point? Create a simple serializable UserInfo class with UserName & image if you want that:

[Serializable]
public class UserInfo
{
    public string UserName {get; set;}
}

Then on successful login

Session["UserInfo"] = new UserInfo { UserName = query.UserName };

On the other pages:

var userInfo = Session["UserInfo"];
if (userInfo == null)
   // Redirect back to your login, the session timed out.

Generally you will want to tie this in to supplement your site authentication to assert the current logged in Identity.

Aside from that, never store passwords in plain text in your database. Generate a hash to store in the database then when a user logs in with a password, hash that value and compare the hashes.

Hash and salt passwords in C#

Steve Py
  • 26,149
  • 3
  • 25
  • 43
  • can you please tell me what here Session (Session["UserInfo"] = new UserInfo { UserName = query.UserName };) is about? – Muhammad saad Jun 18 '21 at 14:42
  • It's storing the user name in an object within server session state. While that user session is active, future requests that request that session state will receive the saved value. (requires a single web server or a shared session state manager when the server is load balanced.) – Steve Py Jun 20 '21 at 07:27