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

前端 未结 6 1225
失恋的感觉
失恋的感觉 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:10

    You could create some sort of utility method or add a method to one of your services that checks if it's your custom principal. Maybe something like:

    public class UserService : IUserService
    {
        public CustomPrincipal CurrentUser
        {
            get
            {
                CustomPrincipal user = HttpContext.Current.User as CustomPrincipal;
    
                if (user == null)
                    return GuestUser; // Just some default user object
    
                return user;
            }
        }
    }
    

提交回复
热议问题