Reputation: 39
I am currently still a junior programmer and have taken over from a previous programmer and i am busy carrying on with his ASP.NET website. He has created his own login and i cant figure out how to display the currently logged in user into a ReadOnly Textbox.
The reason the textBox is readonly is because once a user has logged onto the website any data that is inserted into the SQL database gets added with the currently logged in userID but for Convenience the User name is shown in a readOnly textBox.
here is the Code for the login.aspx
protected void LoginUser_Authenticate(object sender, AuthenticateEventArgs e)
{
LoggedMember member = new LoggedMember();
if (LoginUser.UserName.Contains('@'))
{
member = Login.LogInContact(LoginUser.UserName, LoginUser.Password);
}
else
{
member = Login.LogInUser(LoginUser.UserName, LoginUser.Password);
}
if (member.ID != 0)
{
Session["LoggedMember"] = member;
FormsAuthentication.SetAuthCookie(member.UserName, true);
e.Authenticated = true;
Response.Redirect("~/pages/Monitor.aspx");
}
else
{
Session["LoggedMember"] = null;
e.Authenticated = false;
}
}
LoggedMember Class
public class LoggedMember
{
public int ID { get; set; }
public string UserName { get; set; }
public UserProfile Profile { get; set; }
}
Login Class
public class Login
{
public static LoggedMember LogInUser(string username, string password)
{
LoginContext db = new LoginContext();
User user = db.Users.Where(u => u.UserName == username && u.Password == password).SingleOrDefault();
LoggedMember member = new LoggedMember();
if (user != null)
{
if (user.UserID != 0)
{
member = new LoggedMember()
{
ID = user.UserID,
UserName = user.UserName
};
}
}
if (user != null)
{
member.Profile = UserProfile.GetUserProfileAndSettings((int)user.UserProfileID);
}
else
{
member.Profile = UserProfile.GetUserProfileAndSettings(0);
}
return member;
}
public static LoggedMember LogInContact(string username, string password)
{
LoginContext db = new LoginContext();
Contact contact = db.Contacts.Where(u => u.Email == username && u.Password == password).SingleOrDefault();
LoggedMember member = new LoggedMember();
if (contact.ContactID != 0)
{
member = new LoggedMember()
{
ID = contact.ContactID,
UserName = contact.FirstName + ' ' + contact.LastName
};
}
return member;
}
}
How would i go about loading the currently logged in userName into the ReadOnly textBox and once a record is added to the database to get the userID of the currently logged in user.
I am using C#,Sql server and linq
Thank you
Upvotes: 2
Views: 9441
Reputation: 5636
It's very simple just assigning username
to TextBox
like
Textbox1.Text = username;
For getting the UserID
of the Current Login User
you need to call a select query
like
Select UserId from tablename where username = "Pass username";
Your username
must be unique otherwise you wil get multiple UserId
from this query.
You need to use this type of scenario.
Hope you understand and works for you.
Upvotes: 1
Reputation: 1062600
Since the login code sets a forms-auth cookie, you should be able to use:
public static string GetUsername() {
var ident = System.Web.HttpContext.Current.User.Identity;
return ident.IsAuthenticated ? ident.Name : null;
}
Upvotes: 3