I have an ASP.NET MVC5 CRUD application that is using Windows Authentication and the site is secured by an Active Directory group, all members in this group have the same permissions. I am using Entity Framework 6 to access the database.
I have a very basic User
model at present where Login
would be DOMAIN\username
. All my models reference this model via CreatedByUserId
property, I've omitted the relationships to other models below.
public class User
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Index(IsUnique = true)]
[MaxLength(100)]
public string Login { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime CreatedDatetime { get; set; }
}
I want to be able to achieve two things though I am not sure how without repeating code in all my controllers:
- Users that are authenticated via the Active Directory group have a User model created and stored in the database, if it doesn't already exist.
- When new items are created the
CreatedByUserId
is populated with theId
of theUser
performing the action.
I believe the first part could be achieved via a global AuthorizeAttribute
filter that would create a new User
if they didn't already exist and retreive the Id
when they do, but where should this be stored for future reference.
The second part I am not sure on, I believe it is possible to do this without having to keep querying the database based on the current user. Could this value be set via a constructor using the Id
retrieved when the User
accesses the site?