tôi đã phát triển một đơn giản IIdentity
và IPrincipal
cho dự án MVC của tôi và tôi muốn ghi đè lên User
và User.Identity
để trả về giá trị với đúng loạiOverride tài sản của tài
Dưới đây là danh tính tùy chỉnh của tôi:
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; }
}
đây là Principal tùy chỉnh của tôi:
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
}
đây là tùy chỉnh của tôi điều khiển, tôi được cập nhật thành công User
tài sản để trả lại loại chính tùy chỉnh của tôi:
public abstract class BaseController : Controller
{
protected new virtual MyPrincipal User
{
get { return HttpContext == null ? null : HttpContext.User as MyPrincipal; }
}
}
Làm thế nào tôi có thể làm điều đó một cách tương tự cho User.Identity
trở lại loại sắc tùy chỉnh của tôi?
bạn đặt hiệu trưởng tùy chỉnh của mình trên HttpContext ở đâu? –
Trong phương thức global.asax.cs Application_AuthenticateRequest của tôi – Swell