My problem is this i followed complete on this tutorial video on youtube but at the end the login function he was teaching, I ended up can't using it i tried multiple times and it still stuck on the login page and can go to the logged In page.
it still stuck on the login page and can go to the logged In page
Did you spell it wrong here? Do you mean it stays in the Login view after logging in?If this is the problem, you can check your code according to the suggestions I give below.
You can test the code by breaking points and use F10 to execute it step by step.
You need to check whether obj and Session["IC"] have values, otherwise they will all be in the Login view.
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
I mean that, after i insert the correct username and password, I wasn't been able to login into the website which will later on been redirect into the loggedIn view page
And if I removed the code
if(ModelState.IsValid)
I was able to login into the website, It doesn't like a good way to solve it.
It turns out the Model State is returning false and here is model class for the User
[Key]
public int IC { get; set; }
[Display(Name = "Username")]
[Required(AllowEmptyStrings = false, ErrorMessage = "Username required")]
public string Username { get; set; }
[Display(Name = "First Name")]
[Required(AllowEmptyStrings = false, ErrorMessage = "First name required")]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
[Required(AllowEmptyStrings = false, ErrorMessage = "Last name required")]
public string LastName { get; set; }
[Display(Name = "Email Address")]
[Required(AllowEmptyStrings = false, ErrorMessage = "Email Address required")]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[Display(Name = "Date of birth")]
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime DateofBirth { get; set; }
[Required(AllowEmptyStrings = false, ErrorMessage = "Password is required")]
[DataType(DataType.Password)]
[MinLength(6, ErrorMessage = "Minimum 6 characters required")]
public string Password { get; set; }
[Display(Name = "Confirm Password")]
[DataType(DataType.Password)]
[Compare("Password", ErrorMessage = "Confirm password and password do not match")]
public string LoginErrorMessage { get; set; }
According to the model you provided, I found that you used data annotations for model validation.In the
User model,Username, FirstName,
LastName, Email, and Password
must have values and also need to meet some of your custom
rules. In addition,
LoginErrorMessagemust have the same value as Password.
The value of ModelState.IsValid is trueonly after the above mentioned fields
comply with the rules.
Because when MVC creates the model state for the submitted attributes, it traverses
each attribute in the Model and uses the attributes associated with it to verify the attribute. If any errors are found, they are added to the Errors collection in the ModelState of the property.
It seems that you want to use Username and Password to log in.
I suggest you create a new ViewModel called
LoginViewModel for login.
The custom ViewModel class can be used to pass data from the controller to the view for rendering, or to help
process the form data returned to the controller's action method.
This ViewModel will not affect the User model.
Below is the example I gave, you can refer to it.
The part of the code not shown is the same as the code you provided before.
Client side validation is used in the example given.You need to add two files,
jquery.validate.min.js and
jquery.validate.unobtrusive.min.js.
In the example given, because the Login view
uses the Layout view, these two files are directly added to the
Layout view.
In addition, you need to note that these two files
must be loaded after the
jquery file.
Model
public class LoginViewModel
{
[Display(Name = "Username")]
[Required(AllowEmptyStrings = false, ErrorMessage = "Username required")]
public string Username { get; set; }
[Required(AllowEmptyStrings = false, ErrorMessage = "Password is required")]
[DataType(DataType.Password)]
[MinLength(6, ErrorMessage = "Minimum 6 characters required")]
public string Password { get; set; }
}
Controller
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginViewModel user)
{
if (ModelState.IsValid)
{
using (MVCTestContext db = new MVCTestContext())
{
var obj = db.Users.Where(u => u.Username.Equals(user.Username) && u.Password.Equals(user.Password)).FirstOrDefault();
if (obj != null)
{
Session["IC"] = obj.IC.ToString();
Session["Username"] = obj.Username.ToString();
return RedirectToAction("LoggedIn");
}
}
}
return View(user);
}
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
None
0 Points
2 Posts
Login page - unable to login using EF Code first
Aug 18, 2020 01:46 AM|DepressionCoder|LINK
My problem is this i followed complete on this tutorial video on youtube but at the end the login function he was teaching, I ended up can't using it i tried multiple times and it still stuck on the login page and can go to the logged In page.
Here is the link to the youtube video
https://www.youtube.com/watch?v=tbWWkJYDR7Q&fbclid=IwAR0raDsQqpWwNkk00IFvEndhZHdYfF8Ha_NuHXxTEYKjFbtnmg_qzmEOMgo
And here is my code for Login page
And here is the Login controller
Contributor
2770 Points
789 Posts
Re: Login page - unable to login using EF Code first
Aug 18, 2020 09:35 AM|YihuiSun|LINK
Hi DepressionCoder,
Did you spell it wrong here? Do you mean it stays in the Login view after logging in?If this is the problem, you can check your code according to the suggestions I give below.
Remarks:This is a tutorial to help you get started with Entity Framework 6 Code First using MVC 5, you can refer to it.
Here is the result.
Best Regards,
YihuiSun
None
0 Points
2 Posts
Re: Login page - unable to login using EF Code first
Aug 19, 2020 12:32 PM|DepressionCoder|LINK
No, I think i used the wrong sentences.
I mean that, after i insert the correct username and password, I wasn't been able to login into the website which will later on been redirect into the loggedIn view page
And if I removed the code
I was able to login into the website, It doesn't like a good way to solve it.
It turns out the Model State is returning false and here is model class for the User
Contributor
2770 Points
789 Posts
Re: Login page - unable to login using EF Code first
Aug 20, 2020 03:06 AM|YihuiSun|LINK
Hi DepressionCoder,
Model
Controller
[HttpPost] [ValidateAntiForgeryToken] public ActionResult Login(LoginViewModel user) { if (ModelState.IsValid) { using (MVCTestContext db = new MVCTestContext()) { var obj = db.Users.Where(u => u.Username.Equals(user.Username) && u.Password.Equals(user.Password)).FirstOrDefault(); if (obj != null) { Session["IC"] = obj.IC.ToString(); Session["Username"] = obj.Username.ToString(); return RedirectToAction("LoggedIn"); } } } return View(user); }
_Layout
Here is the result.
Best Regards,
YihuiSun