我已经开发出一种简单IIdentity
和IPrincipal
我的MVC项目,我想重写User
和User.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
返回我的自定义身份类型?
你在哪里设置你的自定义主体在HttpContext? –
在我的global.asax.cs Application_AuthenticateRequest方法 – Swell