簡體   English   中英

Ninject:綁定身份UserManager

[英]Ninject: Binding Identity UserManager

我的AuthenticationController遇到了問題,為此我使用了Identity 2.0。 任務很簡單,但是我對Ninject及其綁定有些不了解。

我想將UserManager綁定到UserStore和DBContext,但是我不知道該怎么做。 另外,更重要的問題是我必須在哪個范圍內設置UserManager和UserStore的綁定。

另外,我的AuthenticationController中有以下方法:

private void UserValidator(UserManager<User> usermanager)
{
    usermanager.UserValidator = new UserValidator<User>(usermanager)
    {
        AllowOnlyAlphanumericUserNames = true
    };
}

我不想創建一個新的UserValidator並在其他方法(或在構造函數中)中調用此方法,而是在創建該方法時將其與我的UserManager綁定。 如何使用Ninject做到這一點?

UserManager,UserStore和DBContext正在創建依賴鏈,因此您必須輸入類似的內容

kernel.Bind<IDBContext>().To<DBContext>().InRequestScope(); //It's good practice to use interface
kernel.Bind<DBContext>().ToSelf().InRequestScope(); //You can also do it this way
kernel.Bind<IUserStore<User>>().To<UserStore<User>>()
            .InRequestScope()
            .WithConstructorArgument("context", kernel.Get<IDBContext>());
kernel.Bind<UserManager<User>>().ToSelf()
            .InRequestScope();

我認為最好為UserValidator使用單獨的方法,因為它僅在注冊新用戶時使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM