Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Firstly, this answer is not strictly an MVC answer, but an ASP.NET answer. The fact that your site is MVC is irrelevant to solving the problem, in this case.</p> <hr> <p>Hmm. I'm not very sure how you are handling your users in your system but it sounds like you using the (very evil) <code>asp.net membership provider</code> that comes out of the box with .net. This is hinted by the fact that you said</p> <ul> <li>aspnet_Users.UserID</li> <li>UserID is a uniqueidentifier (read: GUID).</li> </ul> <p>With the default forms authentication system, which uses the default <a href="http://msdn.microsoft.com/en-us/library/system.web.security.formsidentity.aspx" rel="noreferrer">FormsIdentity</a>, it only has a single property called <a href="http://msdn.microsoft.com/en-us/library/system.web.security.formsidentity.name.aspx" rel="noreferrer">Name</a> (as you correctly noted). This means it has only one value where to place some unique user information. In your case, you are putting <em>Name/UserName/DisplayName</em>, in the <code>Name</code> property. I'm assuming this name is their Display Name and it is unique. Whatever value you are putting in here, it <em>HAS TO BE UNIQUE</em>.</p> <p>From this, you can grab the user's guid.</p> <p>Check this out.</p> <pre><code>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; </code></pre> <p>So, every time you wish to grab the user information, you need to grab it from the database using the static method above.</p> <p>Read all about the <a href="http://msdn.microsoft.com/en-us/library/system.web.security.membership.aspx" rel="noreferrer">Membership class</a> and <a href="http://msdn.microsoft.com/en-us/library/system.web.security.membershipuser.aspx" rel="noreferrer">MembershipUser class</a> on MSDN.</p> <h3>Bonus Answer / Suggestion</h3> <p>As such, i would CACHE that result so you don't need to keep hitting the database.</p> <pre><code>... cont from above.... Guid guid = (Guid)user.ProviderUserKey; Cache.Add(User.Identity.Name, user.UserID); // Key: Username; Value: Guid. </code></pre> <p>Otherwise, you can create your own Identity class (which inherits from <a href="http://msdn.microsoft.com/en-us/library/system.security.principal.iidentity.aspx" rel="noreferrer">IIdentity</a>) and add your own custom properties, like <code>UserID</code>. Then, whenever you authenticate (and also on every request) you can set this value. Anyway, this is a hard core solution, so go with the caching, right now.</p> <p>HTH</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload