ASP.NET MVC - Set custom IIdentity or IPrincipal

后端 未结 9 1414
忘了有多久
忘了有多久 2020-11-21 23:06

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

9条回答
  •  遥遥无期
    2020-11-21 23:46

    As an addition to LukeP code for Web Forms users (not MVC) if you want to simplify the access in the code behind of your pages, just add the code below to a base page and derive the base page in all your pages:

    Public Overridable Shadows ReadOnly Property User() As CustomPrincipal
        Get
            Return DirectCast(MyBase.User, CustomPrincipal)
        End Get
    End Property
    

    So in your code behind you can simply access:

    User.FirstName or User.LastName
    

    What I'm missing in a Web Form scenario, is how to obtain the same behaviour in code not tied to the page, for example in httpmodules should I always add a cast in each class or is there a smarter way to obtain this?

    Thanks for your answers and thank to LukeP since I used your examples as a base for my custom user (which now has User.Roles, User.Tasks, User.HasPath(int) , User.Settings.Timeout and many other nice things)

提交回复
热议问题