I am creating ASP.NET MVC web application. I have data model User:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Knihovna.Models
{
public class User
{
public int UserId { get; set; }
public string Name { get; set; }
public string Login { get; set; }
public string Password { get; set; }
public List<Book> Books { get; set; }
}
}
and I need to create user registration and user login. Application needs to know If user is logged in.
Is there some best practises how to do it? Save logged in user in session?
I would use the ASP.NET membership and role provider model. If you would like to do it with your custom tables you can create a class that inherits from Membership Provider. There are a number of methods you can implement to support things like changing passwords, forgot password etc… but the one for logging in would be ValidateUser
You will also need to create a role provider if you would like there to be different levels of users. To do so you will inherit from the RoleProvider class.
To authorize certain areas of your application you would use the Authorize attribute.
Finally there is some configuration in the web.config you have to do to get it to use your providers.
You can find more information about the memberhsip and role providers on MSDN