I have created my first application using MVC in ASP.NET and C#.
On the controller.cs I use code below for recognize the user (tUser) whether registered or not on table database tUser.
If the user exists on the table tUser I set the session.
I don't have problem if the user registered has associated only one code (tCode), e.g.
+-----------------+--------+
| tUser | tCode |
+-----------------+--------+
| DOMAIN\Edward | D11SU1 |
+-----------------+--------+
But how can I manage a user enabled with multiple codes?
e.g.
+-----------------+--------+
| tUser | tCode |
+-----------------+--------+
| DOMAIN\Edward | D11SU1 |
| DOMAIN\Edward | D11SU2 |
+-----------------+--------+
Is that possible?
My code below
private void recognizeuser()
{
try
{
string cs = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
using (var connection =
new MySqlConnection(cs))
{
string commandText = " SELECT `tUser`, `tCode` FROM `tUser` " +
" WHERE `tUser` = @Username; ";
using (var command = new MySqlCommand(commandText, connection))
{
if (!String.IsNullOrEmpty(HttpContext.User.Identity.Name.ToString()))
{
command.Parameters.AddWithValue("@Username", HttpContext.User.Identity.Name.ToString());
}
connection.Open();
string tUser = string.Empty;
string tCode = string.Empty;
using (MySqlDataReader reader = command.ExecuteReader())
{
reader.Read();
tUser = reader["tUser"].ToString();
tCode = reader["tCode"].ToString();
System.Web.HttpContext.Current.Session["tUser"] = tUser.ToString();
System.Web.HttpContext.Current.Session["tCode"] = tCode.ToString();
System.Web.HttpContext.Current.Session.Timeout = 28800;
}
if (String.IsNullOrEmpty(userName))
{
ViewBag.Message = String.Format("No user!");
}
connection.Close();
}
}
}
catch (Exception ex)
{
TempData["Message"] = "Login failed.Error - " + ex.Message;
}
}
I using this information (the session) later for populated a drop down list according to the enabling code. I'm going to use a query with WHERE condition e.g.
SELECT * FROM mytable WHERE tCode IN ('D11SU1','D11SU2')