Skip to content

MVC 5 login and sign page taking the value from dataabase

Shahbaz Kaware

Shahbaz Kaware

Asked Apr 15, 2017, 9:49 AM EDT10 answers
Share
LinkedInX
in login and registration page how do i store the value in database and take the login value from database in mvc 5

10 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Hi,
     
    you can retrive with help of entity framework and compare it with user inputs, you can create a dbcontext in mvc controller itself,
     
    if its a valid user then you can add it in form authentication,so that you can get logged in user details,otherwise you can throw the erroes using viewbag 
     
    my advice is keep it as service,dont integrate directly into mvc, you can use webapi as service.its a rest based service. 
    0
  • Shahbaz Kaware
    0
    C#
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
    if (!ModelState.IsValid)
    {
    return View(model);
    }
    
    // Require the user to have a confirmed email before they can log on.
    var user = await UserManager.FindByNameAsync(model.Email);
    if (user != null)
    {
    if (!await UserManager.IsEmailConfirmedAsync(user.Id))
    {
    string callbackUrl = await SendEmailConfirmationTokenAsync(user.Id, "Confirm your account-Resend");
    ViewBag.errorMessage = "You must have a confirmed email to log on.";
    return View("Error");
    }
    }
    
    // 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.Email, model.Password, model.RememberMe, shouldLockout: true);
    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);
    }
    this is my login i want to take the values from my database for logn here is the ado.net code
    Java
    public bool Login(LoginViewModel login)
    {
    
    string constr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
    SqlConnection con = new SqlConnection(constr);
    SqlCommand cmd = new SqlCommand("Login", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@Email", login.Email);
    cmd.Parameters.AddWithValue("@Password", login.Password);
    con.Open();
    int i = cmd.ExecuteNonQuery();
    con.Close();
    if (i >= 1)
    {
    return true;
    }
    else
    {
    return false;
    }
    }
    how do i integrate this code to get the login from my database plz help me
    0
  • 0
    TypeScript
    public async Task Login(LoginViewModel model, string returnUrl)
    {
    if (!ModelState.IsValid)
    {
    return View(model);
    }
    using(YourDBContext=new  YourDBContext())
    {
     var LoggedInUserDetails=YourDBContext.Users.FirstOrDefualt(user=>user.Emai.Equals(model.Email) && user.Password.Equals(model.Password));
    
    if( LoggedInUserDetails!=null)
    {
     //redirect to your home page
    }
    else
    
    ModelState.AddModelError("", "Invalid login attempt.");
    }
    return View(model);
    }
    
    try this.
    0
  • Shahbaz Kaware
    0
    using(YourDBContext=new YourDBContext())
     
    throwing me the error how should i improve this 
    0
  • Shahbaz Kaware
    0
    its throws me the error i have send to u a screen shot of this and can u plz pass me ur mobile no
    0
  • 0
    JavaScript
    using(var YourDBConext=new YourDBContext())
    {
    }
    
    try like this
    0
  • Shahbaz Kaware
    0
    plz chek its throwing me error on user
    0
  • 0
    you must have user table in your database and update your edmx and try again
    0
  • Shahbaz Kaware
    0
    i have LoginDetails table already in my database
    0