1

我正在尝试将 HttpContext.Current.User.Identity 中的 IIdentity 绑定到自定义 IPrincipal 但据我所知,在用户通过身份验证之前,IIdentity 为空。

示例代码:

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

我也尝试过使用 GlobalFilters 和 Ninject.BindFilter 方法来设置 CustomPrincipal 但问题又回到了 IIdentity 为空。

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

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

我最终要实现的是将 ICustomPrincipal 向下传递到数据库级别以记录事务中的用户 ID。

谢谢

4

1 回答 1

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
        }
    }
}
于 2013-06-03T23:08:01.337 回答