I have a Controller that grabs all the items from a Mysql database and puts them into my models. This method is called in my Index() so it can display all the items in a gridview.
I want to be able to sign in on the webpage with a 'company number' that is used in the Login.cshtml. The company number also needs to be used in my Contoller ( I think ). It will be used in the SQL Query so the company only sees their own items. but how can I make it so the Controller is able to reach the information?
public List<ContainerInfo> TablesColumnDisplay()
{
//string on = the connectionstring
using SqlCommand SqlComm = new SqlCommand($"SELECT * FROM TheItemList"); //I think this needs WHERE company_number = '{Identity.Companynumber}'
using (SqlDataAdapter sda = new SqlDataAdapter())
{
{
SqlComm.Connection = SqlConn;
SqlConn.Open();
sda.SelectCommand = SqlComm;
SqlDataReader sdr = SqlComm.ExecuteReader();
while (sdr.Read())
{
ItemList item = new ItemList();
//The entire sdr reader that puts all the items into the models
}
}
Or am I horribly wrong and do I have to make a deperate method for it? I'm a bit lost on how to attack this problem.
Edit I am using the standard scaffolded Identity Area where i've changed some model items. in my Login.cshtml is a standard form which is asking for company_number, password and if a remember me is true or not.
public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
returnUrl = returnUrl ?? Url.Content("~/");
if (ModelState.IsValid)
{
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, set lockoutOnFailure: true
var result = await _signInManager.PasswordSignInAsync(Input.company_number.ToString(), Input.Password, Input.RememberMe, lockoutOnFailure: false);
if (result.Succeeded)
{
_logger.LogInformation("User logged in.");
return LocalRedirect(returnUrl);
}
if (result.RequiresTwoFactor)
{
return RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe });
}
if (result.IsLockedOut)
{
_logger.LogWarning("User account locked out.");
return RedirectToPage("./Lockout");
}
else
{
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
return Page();
}
}
// If we got this far, something failed, redisplay form
return Page();
}