First, this answer is not strictly an MVC answer, but an ASP.NET answer. The fact that your MVC site is not relevant to solving the problem, in this case.
Hm. I'm not very sure how you handle your users on your system, but it looks like you are using the (very malicious) asp.net membership provider
, which comes out of the box with .net. It hints at what you said
- aspnet_Users.UserID
- UserID is a unique identifier (read: GUID).
When using the default authentication system, which uses FormsIdentity by default, it has only one property called Name (as you correctly noted). This means that it has only one value, where you can place some unique user information. In your case, you put Name / UserName / DisplayName in the Name
property. I assume this name is the display name, and it is unique. No matter what value you put in this place, it MUST BE UNIQUE.
From this you can grab a custom pointer.
Check this.
using System.Web.Security; .... // NOTE: This is a static method .. which makes things easier to use. MembershipUser user = Membership.GetUser(User.Identity.Name); if (user == null) { throw new InvalidOperationException("User [" + User.Identity.Name + " ] not found."); } // Do whatever u want with the unique identifier. Guid guid = (Guid)user.ProviderUserKey;
So, every time you want to get information about a user, you need to take it from the database using the static method above.
Read all about the membership class and the MembershipUser class on MSDN.
Bonus Response / Offer
That way I would do CACHE, so you don't need to keep getting into the database.
... cont from above.... Guid guid = (Guid)user.ProviderUserKey; Cache.Add(User.Identity.Name, user.UserID);
Otherwise, you can create your own Identity class (which inherits from IIdentity ) and add your own custom properties, such as UserID
. Then, whenever you authenticate (as well as for each request), you can set this value. Anyway, this is a solid decision, so let's go with caching right now.
NTN