将自定义IPrincipal和IIdentity绑定到Ninject

时间:2013-06-02 22:54:16

标签: c# entity-framework asp.net-mvc-4 dependency-injection ninject

我正在尝试将IIdentity从HttpContext.Current.User.Identity绑定到自定义IPrincipal,但是根据我可以收集的内容,在用户通过身份验证之前,IIdentity为null。

示例代码:

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为空。

我不想调用CustomPrincipal的构造函数,因为IUserRepository也涉及构造函数注入。

我不确定我是否没有正确绑定,或者我的实施方法不对,我们将不胜感激任何想法或建议。

我最终想要实现的是将ICustomPrincipal传递到数据库级别以记录事务上的UserID。

由于

1 个答案:

答案 0 :(得分:0)

这是一个示例,显示引导后AFTER和身份验证之前

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
        }
    }
}