I have implemented a custom Principle for ASP.Net identity, however when I try to get my custom principle from HttpContext.Current.User I receive the following exception:
Unable to cast object of type 'System.Security.Principal.GenericPrincipal' to type 'VenuePortal.Business.ICustomPrincipal'.
My implementation is:
public interface ICustomPrincipal : IPrincipal
{
int UserID { get; set; }
string UserName { get; set; }
string Email { get; set; }
string AuthCode { get; set; }
string Title { get; set; }
}
public class CustomPrincipal : ICustomPrincipal
{
public int UserID { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
public string AuthCode { get; set; }
public string Title { get; set; }
public IIdentity Identity { get; private set; }
public bool IsInRole(string role) { return false; }
public CustomPrincipal(string email)
{
Identity = new GenericIdentity(email);
}
}
The error throws in this Ninject binding:
kernel.Bind<ICustomPrincipal>().ToMethod(context => (ICustomPrincipal)HttpContext.Current.User).InRequestScope();
I have this same solution working in another (older) project so I'm guessing there is some kind of framework change affecting this? HttpContext.Current.User still seems to return an IPrinciple so shouldn't this all work?
Any help much appreciated.