我想要綁定IIdentity從HttpContext.Current.User.Identity到自定義IPrincipal但從我可以收集,IIdentity在用戶身份驗證之前爲空。綁定自定義IPrincipal和IIdentity與Ninject
示例代碼:
public interface ICustomPrincipal
{
int UserID { get; set; }
}
public class CustomPrincipal : ICustomPrincipal
{
private readonly IIdentity _identity;
private readonly IUserRepository _userRepository;
public CustomPrincipal(IIdentity identity, IUserRepository repository)
{
_identity = identity;
_repository = repository;
}
}
然後
protected void Application_AcquireRequestState(object sender, EventArgs e)
{
if (Request.IsAuthenticated && !Request.Url.AbsoluteUri.Contains(".axd"))
{
HttpContext.Current.User as CustomPrincipal;
}
}
我可以綁定IUserRepository沒有問題,但我不知道如何正確地綁定的IIdentity。
我試圖在Application_Start的CreateKernel()中綁定HttpContext.Current.User.Identity,但問題是,IIdentity爲null。
我也嘗試使用GlobalFilters和Ninject.BindFilter方法來設置CustomPrincipal,但問題仍然回到IIdentity爲null。
我不想調用CustomPrincipal的構造函數,因爲IUserRepository也涉及構造函數注入。
我不確定我是否沒有正確地綁定,或者我的實現方法不對,任何想法或建議將不勝感激。
我最終想實現的目標是將ICustomPrincipal向下傳遞到數據庫級別以記錄事務中的UserID。
感謝
基於表單的認證?一旦認證成功,您只能訪問身份信息? –
是它的形式的基礎。我嘗試在應用程序啓動時訪問它,但收效甚微 –