In ASP.NET MVC, you get the default AspNetUsers table that stores all user data.
Now in most projects, you want to refer to the user that created, for example, a post. You also want to keep your database logic separate from your web logic, so you most likely put database logic in a class library.
But then, you encounter some issues: How do I refer to a AspNetUser? Should I move the ApplicationUser to the class Library? Should I even add foreign keys to this table?
I've seen many questions and answers about how to add a foreign key to AspNetUser table and how to move the ApplicationUser part to the Class Library, but I have my doubts in terms of security for the following things:
- Part of the database is going to be exposed through an API, mostly for AngularJS Client-Side calls, but later perhaps for third-party websites. Having a foreign key to the UserIdin the a table will expose theUserIdon a GET call, which I've been told that this should be kept secure... right?
- Isn't the default security logic template created in such a way that modifying it should be avoided? (Moving ApplicationUserto class Library).
- To get the logged in user you have to use System.Web.HttpContext.Current.User.Identity.GetUserId();, which I'm not sure about how secure this is in a class library and if this is any good as it usesSystem.Web, is it common to use this in a class library that should only handle database logic?
- It still requires an correct connection string in web.config, so the database handling still isn't really separated from the MVC application.
- Calling the database correctly for userId should be avoided according to this answer.
So let's just say this is not a good way of creating your project, what are my alternatives? How would I correctly refer to an user?
In another project I used a third table, which contains additional information about the user, where one string is the e-mail address. This e-mail address is sequel (and unique) to the logged in Username (User.Identity.Name). This third table is only exposed through the API in such a way that it hides the e-mail address. It still requires System.Web.HttpContext.Current.User.Identity.Name; though.
My question to you guys is, What is best practice in 2015?
 
     
    