I am completely new to ASP.NET MVC and don't know what is what at this point. I have spent many hours trying to figure out what I think should be quite simple and searched the web quite a bit!
I have created an ASP.NET MVC project which prepopulated things such as the Login and Register page.
I got Entity Framework to connect/use my existing DB and wanted to register a user(i.e insert a new user into the DB). Using the code that was pre-populated worked, but inserted into an "internal DB" which is not what I wanted, but using the async await did not work when trying to insert into my existing DB (gave an error in the lines of Model.User
can not be converted to Model.DB.User
or something along those lines), so I am now doing it synchronously but quite liked the validations it did (i.e checking if user already existed etc., which I have now coded myself).
(sorry for all the typing)
Now I want to log in, and this is the code :
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
return View(model);
}
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, change to shouldLockout: true
var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, isPersistent:false, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
return RedirectToLocal(returnUrl);
case SignInStatus.LockedOut:
return View("Lockout");
//case SignInStatus.RequiresVerification:
// return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
case SignInStatus.Failure:
default:
ModelState.AddModelError("", "Invalid login attempt.");
return View(model);
}
}
But this fails. I dug a little deeper and its searching in the [AspNetUsers] table? How do I get it to search in MY Employee table in my Existing DB ? I like the validations and based on what I read what async and await does, id like to keep using it.
Ultimately my question is how can I for example get the above code to read from my entity Framework(existing DB) and not from this "internal DB" ?
I hope this makes sense.
Thanks for the help.
EDIT:
Below is the LoginModelView class being used :
public class LoginViewModel
{
public string FirstName { get; set; }
public string Surname { get; set; }
public string Email { get; set; }
[Required]
[Display(Name = "User Name")]
public string UserName { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
public DateTime LastLoggedIn { get; set; }
public bool isLockedOut { get; set; }
public bool isAuthorized { get; set; }
public bool isActive { get; set; }
}
My Employee
table consists of the following columns (if this even matters):
PK_UserID, Firstname, Surname, Email, UserName, Password, LastLoggedIn, isLockedOut, isAuthorized, isActive