[英]Is OWIN only for ASP.net Identity? Can't we use owin context to query our application db?
[英]Owin Context on Asp .Net Identity
我對如何從asp .net身份模板實現此代碼感到困惑。
private ApplicationSignInManager _signInManager;
private ApplicationUserManager _userManager;
private ApplicationRoleManager _roleManager;
public AuthController() { }
public AuthController(ApplicationUserManager userManager, ApplicationSignInManager signInManager, ApplicationRoleManager roleManager)
{
UserManager = userManager;
SignInManager = signInManager;
RoleManager = roleManager;
}
public ApplicationRoleManager RoleManager
{
get { return _roleManager ?? HttpContext.GetOwinContext().Get<ApplicationRoleManager>(); }
private set { _roleManager = value; }
}
public ApplicationSignInManager SignInManager
{
get { return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>(); }
private set { _signInManager = value; }
}
public ApplicationUserManager UserManager
{
get { return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); }
private set { _userManager = value; }
}
為什么我們的AuthController帶參數? 脫毒注射的目的? 如果沒有注入發生,則調用在Startup.cs中指定的owincontext? 這是否意味着我們可以忽略owincontext而僅使用注入?
當我嘗試調試它的運行方式時,它只能通過無參數構造函數進行,並且會破壞我的代碼。 是否不使用此帶參數的構造函數?
任何想法? 謝謝。
的確,此模板適用於DI容器,沒有一個容器-可以覆蓋所有情況。 如您所說-如果使用DI,則使用帶有參數的構造函數。
如果構造函數中未提供任何內容,則進入OWIN並獲取所需對象的實例。
是的,如果您使用的是DI,則可以使用HttpContext.GetOwinContext()
刪除屬性,而只向構造函數保留參數。
如果使用的是DI,但控制器是通過無參數構造函數創建的,則您沒有為Asp.Net配置DependencyResolver
。 在Global.asax.cs(或任何進行DI配置的地方)中,您需要調用DependencyResolver.SetResolver(myContainerWrapper);
其中myContainerWrapper
是IDependencyResolver
,您需要查看DI容器如何與Asp.Net集成。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.