【发布时间】:2024-06-15 19:50:01
【问题描述】:
我已经为 ASP.Net 身份实现了一个自定义原则,但是当我尝试从 HttpContext.Current.User 获取我的自定义原则时,我收到以下异常:
无法将“System.Security.Principal.GenericPrincipal”类型的对象转换为“VenuePortal.Business.ICustomPrincipal”类型。
我的实现是:
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);
}
}
此 Ninject 绑定中引发错误:
kernel.Bind<ICustomPrincipal>().ToMethod(context => (ICustomPrincipal)HttpContext.Current.User).InRequestScope();
我在另一个(较旧的)项目中也有同样的解决方案,所以我猜有某种框架更改会影响这个? HttpContext.Current.User 似乎仍然返回一个 IPrinciple 所以这一切不应该有效吗?
非常感谢任何帮助。
【问题讨论】:
标签: asp.net asp.net-identity iprincipal