Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's how I do it.</p> <p>I decided to use IPrincipal instead of IIdentity because it means I don't have to implement both IIdentity and IPrincipal.</p> <ol> <li><p>Create the interface</p> <pre><code>interface ICustomPrincipal : IPrincipal { int Id { get; set; } string FirstName { get; set; } string LastName { get; set; } } </code></pre></li> <li><p>CustomPrincipal</p> <pre><code>public class CustomPrincipal : ICustomPrincipal { public IIdentity Identity { get; private set; } public bool IsInRole(string role) { return false; } public CustomPrincipal(string email) { this.Identity = new GenericIdentity(email); } public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } } </code></pre></li> <li><p>CustomPrincipalSerializeModel - for serializing custom information into userdata field in FormsAuthenticationTicket object.</p> <pre><code>public class CustomPrincipalSerializeModel { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } } </code></pre></li> <li><p>LogIn method - setting up a cookie with custom information</p> <pre><code>if (Membership.ValidateUser(viewModel.Email, viewModel.Password)) { var user = userRepository.Users.Where(u =&gt; u.Email == viewModel.Email).First(); CustomPrincipalSerializeModel serializeModel = new CustomPrincipalSerializeModel(); serializeModel.Id = user.Id; serializeModel.FirstName = user.FirstName; serializeModel.LastName = user.LastName; JavaScriptSerializer serializer = new JavaScriptSerializer(); string userData = serializer.Serialize(serializeModel); FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket( 1, viewModel.Email, DateTime.Now, DateTime.Now.AddMinutes(15), false, userData); string encTicket = FormsAuthentication.Encrypt(authTicket); HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket); Response.Cookies.Add(faCookie); return RedirectToAction("Index", "Home"); } </code></pre></li> <li><p>Global.asax.cs - Reading cookie and replacing HttpContext.User object, this is done by overriding PostAuthenticateRequest</p> <pre><code>protected void Application_PostAuthenticateRequest(Object sender, EventArgs e) { HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName]; if (authCookie != null) { FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value); JavaScriptSerializer serializer = new JavaScriptSerializer(); CustomPrincipalSerializeModel serializeModel = serializer.Deserialize&lt;CustomPrincipalSerializeModel&gt;(authTicket.UserData); CustomPrincipal newUser = new CustomPrincipal(authTicket.Name); newUser.Id = serializeModel.Id; newUser.FirstName = serializeModel.FirstName; newUser.LastName = serializeModel.LastName; HttpContext.Current.User = newUser; } } </code></pre></li> <li><p>Access in Razor views</p> <pre><code>@((User as CustomPrincipal).Id) @((User as CustomPrincipal).FirstName) @((User as CustomPrincipal).LastName) </code></pre></li> </ol> <p>and in code:</p> <pre><code> (User as CustomPrincipal).Id (User as CustomPrincipal).FirstName (User as CustomPrincipal).LastName </code></pre> <p>I think the code is self-explanatory. If it isn't, let me know.</p> <p>Additionally to make the access even easier you can create a base controller and override the returned User object (HttpContext.User):</p> <pre><code>public class BaseController : Controller { protected virtual new CustomPrincipal User { get { return HttpContext.User as CustomPrincipal; } } } </code></pre> <p>and then, for each controller:</p> <pre><code>public class AccountController : BaseController { // ... } </code></pre> <p>which will allow you to access custom fields in code like this:</p> <pre><code>User.Id User.FirstName User.LastName </code></pre> <p>But this will not work inside views. For that you would need to create a custom WebViewPage implementation:</p> <pre><code>public abstract class BaseViewPage : WebViewPage { public virtual new CustomPrincipal User { get { return base.User as CustomPrincipal; } } } public abstract class BaseViewPage&lt;TModel&gt; : WebViewPage&lt;TModel&gt; { public virtual new CustomPrincipal User { get { return base.User as CustomPrincipal; } } } </code></pre> <p>Make it a default page type in Views/web.config:</p> <pre><code>&lt;pages pageBaseType="Your.Namespace.BaseViewPage"&gt; &lt;namespaces&gt; &lt;add namespace="System.Web.Mvc" /&gt; &lt;add namespace="System.Web.Mvc.Ajax" /&gt; &lt;add namespace="System.Web.Mvc.Html" /&gt; &lt;add namespace="System.Web.Routing" /&gt; &lt;/namespaces&gt; &lt;/pages&gt; </code></pre> <p>and in views, you can access it like this:</p> <pre><code>@User.FirstName @User.LastName </code></pre>
 

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