3

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.

ridermansb
  • 10,779
  • 24
  • 115
  • 226

2 Answers2

3

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()

Jeff Ogata
  • 56,645
  • 19
  • 114
  • 127
3

You could create a new base type for your views and add to it a property or method that will do the casting. That way you can avoid doing it all the time in your views.

marcind
  • 52,944
  • 13
  • 125
  • 111