How can I make accessing my custom IPrincipal easier in ASP.NET MVC?

前端 未结 6 1230
失恋的感觉
失恋的感觉 2021-02-14 23:18

I\'ve written a custom principal object which contains a few additional fields (email and userid in addition to the username).

In order to access these properties I have

6条回答
  •  终归单人心
    2021-02-15 00:19

    You could also make extension methods for the Email and UserID in the same fashion as John Kalberer's answer:

    public static class CustomPrincipalExtensions
    {
        public static string Email(this CustomPrincipal cUser)
        {
            return cUser != null ? cUser.Email : "Default Email"
        }
    
        public static string UserID(this CustomPrincipal cUser)
        {
            return cUser != null ? cUser.UserID : "Default ID"
        }
    }
    

提交回复
热议问题