I'm using forms authentication for my first ASP.NET MVC 3 application and so there's a dbo.aspnet_Users table that is used for logging into the site and dbo.aspnet_Roles and dbo.aspnet_UsersInRoles tables which manage the roles and role assignments respectively.
The DB is SQL Server.
Additionally I've got a Users table in my application's database which has a layout something like this:
UserID (PK, int, not null)
Name (varchar(50), not null)
Phone (varchar(45), null)
Location (varchar(45), null)
That UserID PK is used for various other tables as a foreign key and the list of users, say, from a particular Location are retrieved and displayed on my website's pages in dropdownlists, etc.
The disconnect I'm having is that when I log onto the page, I want to display a user-specific list, e.g., my latest recipes created, and while I could choose myself from a dropdownlist to get the UserID, I'd like it to be tied to my log-on ID.
I'm not sure I have a very good solution for this. One idea I had was to create a lookup table in my application's database mapping UserIDs from my application's Users table to the GUIDs in the aspnet_Users table, something like
UserGUID(PK, uniqueidentifier, not null )
UserID(PK, int, not null)
and then retrieve the GUID (not sure how yet), look that up and use the associated UserID to "know" which user I am. I hope that makes sense.
Anyone faced a similar situation? How would you solve this?
Update:
My current approach involves adding a UserGUID column (uniqueidentifier) to my application's Users table and then I created a BaseController that looks something like so:
public abstract class BaseController : Controller
{
public Guid UserGuid { get; set; }
public int UserID { get; set; }
protected BaseController()
{
try
{
UserGuid = (Guid)Membership.GetUser().ProviderUserKey;
var db = new IceCreamEntities();
UserID = db.Users.Where(m => m.UserGUID == UserGuid).First().UserID;
}
catch (NullReferenceException)
{
UserGuid = Guid.Empty;
UserID = 0;
}
}
}
and them I have my various controllers inherit BaseController rather than Controller so they get that functionality as part of the base's constructor. Then in my controller actions where I need to fill in a field with the UserID I can do this:
[HttpPost]
public ActionResult Create(IceCreamFlavorViewModel viewmodel)
{
...
flavor.CreatedBy = this.UserID;
...
}
Seems to work OK so far, but I'm still interested in how others would solve this problem.