我已經開發出一種簡單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