Ninject每个会话单身?

Ham*_*ein 5 ninject membership-provider ninject.web asp.net-mvc-3

所以我试图将用户的概念引入我的应用程序,并拥有自己的一组自定义登录例程等工作正常.在我的模块中,我将IUserSession绑定到我的实现和InSingletonScope.

现在我怀疑是这种情况,并且已经能够证明这不是正确的事情,如果我尝试使用两个用户登录同一站点,我只获得一组数据.

如果我实现MembershipProvider,我是否应该避免这种限制.我知道,如果我实施会员提供者,我不必注入所有内容,但我的登录不仅仅是用户名/密码,如何使用其他数据登录"?

Bui*_*ted 11

InSingletonScope在整个应用程序中共享,不受每个用户会话的限制.你所做的一切都不会改变这一点.您需要使用像InRequestScope这样的其他东西,但这只是根据实际请求共享的...

试试这个网站:http://iridescence.no/post/Session-Scoped-Bindings-With-Ninject-2.aspx

public static class NinjectSessionScopingExtention {
    public static void InSessionScope<T>(this IBindingInSyntax<T> parent) {
        parent.InScope(SessionScopeCallback);
    }

    private const string _sessionKey = "Ninject Session Scope Sync Root";

    private static object SessionScopeCallback(IContext context) {
        if (HttpContext.Current.Session[_sessionKey] == null) {
            HttpContext.Current.Session[_sessionKey] = new object();
        }

        return HttpContext.Current.Session[_sessionKey];
    }
}
Run Code Online (Sandbox Code Playgroud)