我为我的 MVC 项目开发了一个简单的IIdentity
andIPrincipal
我想覆盖User
andUser.Identity
以正确的类型返回值
这是我的自定义身份:
public class MyIdentity : IIdentity
{
public MyIdentity(string name, string authenticationType, bool isAuthenticated, Guid userId)
{
Name = name;
AuthenticationType = authenticationType;
IsAuthenticated = isAuthenticated;
UserId = userId;
}
#region IIdentity
public string Name { get; private set; }
public string AuthenticationType { get; private set; }
public bool IsAuthenticated { get; private set; }
#endregion
public Guid UserId { get; private set; }
}
这是我的自定义校长:
public class MyPrincipal : IPrincipal
{
public MyPrincipal(IIdentity identity)
{
Identity = identity;
}
#region IPrincipal
public bool IsInRole(string role)
{
throw new NotImplementedException();
}
public IIdentity Identity { get; private set; }
#endregion
}
这是我的自定义控制器,我成功更新了User
属性以返回我的自定义主体类型:
public abstract class BaseController : Controller
{
protected new virtual MyPrincipal User
{
get { return HttpContext == null ? null : HttpContext.User as MyPrincipal; }
}
}
如何以相同的方式User.Identity
返回我的自定义身份类型?