I am struggling with this error which is highlighting on:
var account = db.Accounts.SingleOrDefault(a => a.Username.Equals(username));.
The error message I am getting is:
Unable to cast object of type 'System.Int32' to type 'System.String'.
My data type is not int. I am wondering where the Int32 part is coming from. Below is my code:
[HttpPost]
[Route("process")]
public IActionResult Process(string username, string password)
{
var account = processLogn(username, password);
if (account != null)
{
securityManager.SignIn(this.HttpContext, account);
return RedirectToAction("index","dashboard", new { area = "admin" });
//return View("Index");
}
else
{
ViewBag.error = "Invalis Account";
return View("Index");
}
}
private Account processLogn(string username, string password)
{
var account = db.Accounts.SingleOrDefault(a => a.Username.Equals(username));
if (account != null)
{
if (BCrypt.Net.BCrypt.Verify(password, account.Password))
{
return account;
}
}
return null;
}
Here is my class:
[Table("Account")]
public class Account {
public Account()
{
RoleAccounts = new HashSet<RoleAccount>();
}
[Key]
public string Id { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string FullName { get; set; }
public string Email { get; set; }
public bool Status { get; set; }
public virtual ICollection<RoleAccount> RoleAccounts { get; set; }
}
Table description:
Username varchar(250) Checked
Accountand the underlying table definition forAccounts? It seems as if your table is defined asUsernamebeing a numeric type.