无法从System.Security.Claims.ClaimsPrincipal继承



MVC5控制器具有一个用户属性,该属性是System.Security.Claims.ClaimsPrincipal

我已经创建了一个从中继承的基本控制器。此基本控制器的User属性被继承自System.Security.Claims.ClaimsPrincipalmycustomPrinciple覆盖

 public abstract partial class BaseController : Controller
    {
        public new mycustomPrinciple User
        {
            get
            {
                return base.User as mycustomPrinciple;  //returns null here
            }
        }
     }
public class mycustomPrinciple : System.Security.Claims.ClaimsPrincipal
    {
        private int _ID;
        public int ID
        {
            get { return _ID; }
            set { _ID = value; }
        }
    }

正如你在上面看到的,它在铸造上失败了。我不确定为什么会这样,因为我在MVC4中做了类似的事情,但继承的原理是System.Security.Principal.IPrincipal,因为这就是控制器User属性的一种类型。

您错过了ClaimsPrincipal的要点——Claims集合是您保存应用程序特定身份数据的地方。然后我建议添加扩展方法来提取特定的声明,比如你的"ID"。

最新更新