1

我想要綁定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。

感謝

+0

基於表單的認證?一旦認證成功,您只能訪問身份信息? –

+0

是它的形式的基礎。我嘗試在應用程序啓動時訪問它,但收效甚微 –

回答

0

這裏是一個示例顯示引導之後和在認證

public class MvcApplication : System.Web.HttpApplication 
{ 
    protected void Application_Start() 
    { 
     FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
     RouteConfig.RegisterRoutes(RouteTable.Routes); 
     BundleConfig.RegisterBundles(BundleTable.Bundles); 
     //WebApiConfig.Register(GlobalConfiguration.Configuration); 
     //BundleMobileConfig.RegisterBundles(BundleTable.Bundles); 
    } 

    private bool _isBootStrapped; 
    private bool _isBootStrappedAuthenticated; 

    public override void Init() 
    { 
     base.Init(); 
     // handlers managed by ASP.Net during Forms authentication 
     BeginRequest += new EventHandler(BeginRequestHandler); 
     PostAuthorizeRequest += new EventHandler(PostAuthHandler); 
     EndRequest += new EventHandler(EndRequestHandler); 
    } 

    public void EndRequestHandler(object sender, EventArgs e) 
    { 
    } 

    public void BeginRequestHandler(object sender, EventArgs e) 
    { 
     BootStrapUnauthentiated(); 
    } 

    public void PostAuthHandler(object sender, EventArgs e) 
    { 
     if (_isBootStrappedAuthenticated) 
     { 
      return; // nuff done... 
     } 
     BootStrapAuthenticated(); 
     BootStrapUnauthentiated(); 
    } 

    private void BootStrapAuthenticated() 
    { 
     if (Request.IsAuthenticated) 
     { 

      BootStrapHttp(Context); 
      BootStrapper.RegisterInfrastureAdapters(); 

      _isBootStrapped = true; 
      _isBootStrappedAuthenticated = true; 
     } 
    } 

    private void BootStrapUnauthentiated() 
    { 
     if (!_isBootStrapped) 
     { // minimal bootstrap for launch but user not yet known, eg logon screen 

      BootStrapHttp(Context); 
      BootStrapper.RegisterInfrastureAdapters(); 
      _isBootStrapped = true; // just a connection, if no persisted cookie, the may not be authenticated yet 
     } 
    } 
}