Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC - Set custom IIdentity or IPrincipal
    text
    copied!<p>I need to do something fairly simple: in my ASP.NET MVC application, I want to set a custom IIdentity / IPrincipal. Whichever is easier / more suitable. I want to extend the default so that I can call something like <code>User.Identity.Id</code> and <code>User.Identity.Role</code>. Nothing fancy, just some extra properties.</p> <p>I've read tons of articles and questions but I feel like I'm making it harder than it actually is. I thought it would be easy. If a user logs on, I want to set a custom IIdentity. So I thought, I will implement <code>Application_PostAuthenticateRequest</code> in my global.asax. However, that is called on every request, and I don't want to do a call to the database on every request which would request all the data from the database and put in a custom IPrincipal object. That also seems very unnecessary, slow, and in the wrong place (doing database calls there) but I could be wrong. Or where else would that data come from?</p> <p>So I thought, whenever a user logs in, I can add some necessary variables in my session, which I add to the custom IIdentity in the <code>Application_PostAuthenticateRequest</code> event handler. However, my <code>Context.Session</code> is <code>null</code> there, so that is also not the way to go.</p> <p>I've been working on this for a day now and I feel I'm missing something. This shouldn't be too hard to do, right? I'm also a bit confused by all the (semi)related stuff that comes with this. <code>MembershipProvider</code>, <code>MembershipUser</code>, <code>RoleProvider</code>, <code>ProfileProvider</code>, <code>IPrincipal</code>, <code>IIdentity</code>, <code>FormsAuthentication</code>.... Am I the only one who finds all this very confusing?</p> <p>If someone could tell me a simple, elegant, and efficient solution to store some extra data on a IIdentity without all the extra fuzz.. that would be great! I know there are similar questions on SO but if the answer I need is in there, I must've overlooked.</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