4

Hy,

I'm very new to MVC 5 (or any other MVC). I want to create my own custom login with registration. Can somebody point me to this?

The login should have a simple email and password textbox. The registration should have additional data like first/lastname, age, etc. which stored in a table (user) and a dropdownbox with roles to select (stored in table "roles"). After successful login/registration should the user be redirected to the dashboard.

Or is there a good tutorial about this for MVC 5 .. I just found one for MVC 4.

Thanks for help :)

Golda
  • 3,823
  • 10
  • 34
  • 67
Robert Jaskowski
  • 823
  • 4
  • 11
  • 21
  • 1
    Your question is kind of broad. Please read [ask]. What do you want this custom login with registration to do? Does it store additional information upon registration? Does it redirect somewhere? Does it dance? – Jack Dec 03 '13 at 01:36
  • @Jack The login should just have an email and password textbox. The registration should have additional information like first/lastname, age, etc. - this data should be stored in a table named "user" (in a MS SQL Server 2012). The registration should have one special data: a role dropdownbox, where the user can choose his role .. this will be stored in a other table (roles). If the user has registered successful he should be redirect to the dashboard. When a user has successful loged in, also a redirect should be done to the dashboard. – Robert Jaskowski Dec 03 '13 at 12:26
  • @Jack I think, that's nothing special .. but I don't know how I should code this. I just only have expience with plain PHP :( – Robert Jaskowski Dec 03 '13 at 12:27

1 Answers1

4

Try this

[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
    ViewBag.ReturnUrl = returnUrl;
    return View();
}

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
    if (ModelState.IsValid)
    {
        var user = MyViewModels.checkUser(model.UserName, model.Password);
        if (user!=null)
        {
            SignInAsync();
            return RedirectToAction("Welcome");
        }
        else
        {
            ModelState.AddModelError("", "Invalid username or password.");
        }
    }
    return View(model);
}

private void SignInAsync()
{
    var claims = new List<Claim>();
    claims.Add(new Claim(ClaimTypes.Name, "UserName"));
    claims.Add(new Claim(ClaimTypes.Email, "User@mail.com"));
    var id = new ClaimsIdentity(claims,
                                DefaultAuthenticationTypes.ApplicationCookie);

    var ctx = Request.GetOwinContext();
    var authenticationManager = ctx.Authentication;
    authenticationManager.SignIn(id);
}

[Authorize]
public ActionResult Welcome()
{
    return View();
}

If you add [Authorize] attribute in the action, then it will redirect only the user name and password is authorize

Function to get user name and password from database

public static UserTable checkUser(string userName, string password)
{
    DemoEntities db = new DemoEntities();
    var query = (from u in db.UserTables
                 where u.UserName == userName && u.Password == password
                 select u).FirstOrDefault();
    if(query!=null)
        return query;
    else
        return null;
}
Golda
  • 3,823
  • 10
  • 34
  • 67
  • The type or namespace name 'UserTable' could not be found (are you missing a using directive or an assembly reference? – WIRN Mar 07 '14 at 14:09
  • @WIRN, UserTable is a table name from the database. This table is accessed from entity frame work – Golda Mar 11 '14 at 04:03