Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a login page with 2 textboxes username and password.I want to check if the username exists in the database based on the user entered username and password in the page.Im using MVC pattern..plz help
Posted
Comments
Keith Barrow 27-Jun-11 9:51am    
Are you using ASP.NET MVC, or have you implemented the pattern yourself?

1 solution

You would do something along these lines

Assuming your page was shown from a call to an action method named Logon, you could code another Logon action method that is limited to HttpPost

C#
[HttpPost]
public ActionResult LogOn(LogOnModel model)
{
    if (ModelState.IsValid)
    {
        if (MembershipService.ValidateUser(model.UserName, model.Password))
        {
            AuthenticationService.SignIn(model.UserName, model.RememberMe);
            return RedirectToAction("Index", "Home");
        }
        else
        {
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
        }
    }
    // If we got this far, something failed - redisplay form with model
    return View(model);
}


The code for LogOnModel would be something like this...

C#
public class LogOnModel
{
    [Required]
    [DisplayName("User name")]
    public string UserName { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [DisplayName("Password")]
    public string Password { get; set; }

    [DisplayName("Remember me?")]
    public bool RememberMe { get; set; }
}


This example is using the membership service to check for the user, but you could easily roll your own here if you wanted
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900