Question

I'm building a helpdesk/asset tracking web application using Windows Authentication, ASP.NET MVC5, and C#, with a SQL back-end.

In the database, I'm thinking of linking support tickets and assets to users by way of their Active Directory SID, since this value will not change over time. Their SID, along with some other pieces of information, would go into a table called "Users".

Since I'm new to MVC, what would be the most efficient way of getting the user's SID into the database upon their first visit to the site? I thought about building something into Global.asax, but that seems like it goes against the MVC paradigm.

Alternatively, does anyone have suggestions for a better approach?

Was it helpful?

Solution

The idea is quite common.

For most sites (for example StackOverflow) you have to register. You take a common login service (StackExchange) and upon first login, a 'profile' is created.

Authentication (and maybe authorization) still happens through the original source (in your case AD), while the information you really need is saved in the database.

In the AccountController.Login action, you call ActiveDirectory yourself.

using (PrincipalContext context = new PrincipalContext(ContextType.Domain)) 
{
     using (UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(context, model.UserName))
     {
         // read back the user from database
         // if non-existent: add it using a stored procedure or something like that
     }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top