0

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.

itsmatt
  • 31,265
  • 10
  • 100
  • 164

1 Answers1

2

I have used the same technique in my mvc application and its working fine for last one year. However, there is another approach that you may consider if you haven't gone that far in development process. you can consider using ProfileProvider for storing user specific information than rolling you own system. plz have a look at following links
http://odetocode.com/articles/440.aspx
http://www.vbforums.com/showthread.php?p=3352498
Implementing Profile Provider in ASP.NET MVC

Community
  • 1
  • 1
Muhammad Adeel Zahid
  • 17,474
  • 14
  • 90
  • 155