Search code examples
asp.net-mvc-3razorstrong-typingcustom-viewiidentity

Strongly Typed @User.Identity


I customize IIdentity and IPrincipal adding a few more properties in IIdentity.

You can obtain a strongly typed instance @User.Identity for my custom class? Without having to make conversions in cast.

I thought of something like razor customize the View, but do not even know where to start.


Solution

  • You could try creating an extension method on IPrincipal

    public static class PrincipalExtensions
    {
        public static MyIdentity GetMyIdentity (this IPrincipal principal)
        {
            return principal.Identity as MyIdentity;
        }
    }
    

    and then get your identity by calling @User.GetMyIdentity()