简体   繁体   English

在AccountController之外放置UserManager和UserStore?

[英]Disposing UserManager and UserStore outside of AccountController?

Is this good enough? 这够好吗? Or do i have to dispose UserStore as well? 或者我是否也必须处置UserStore If i do have to any suggestions would be welcome. 如果我有任何建议,欢迎。 I'm new to ASP.NET Identity. 我是ASP.NET身份的新手。

using (var applicationDbContext = new ApplicationDbContext())
{
    using (var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(applicationDbContext)))
    {

    }
}

This would be better i guess: 我想这会更好:

using (var applicationDbContext = new ApplicationDbContext())
{
    using (var userStore = new UserStore<ApplicationUser>(applicationDbContext))
    {
        using (var userManager = new UserManager<ApplicationUser>(userStore))
        {

        }
    }
}

EDIT: I'm glad that i asked this question, although i may have already answered my initial question. 编辑:我很高兴我问这个问题,虽然我可能已经回答了我的初步问题。 Thanks Glenn Ferrie, will check out the ASP.NET dependency injection. 感谢Glenn Ferrie,将检查ASP.NET依赖注入。

This is a few code snippets from a new ASP.NET MVC (.NET 4.6) created with VS 2015 RC. 这是使用VS 2015 RC创建的新ASP.NET MVC(.NET 4.6)的一些代码片段。 First the Startup class: 首先是Startup类:

public partial class Startup
{
    // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
    public void ConfigureAuth(IAppBuilder app)
    {
        // Configure the db context, user manager and signin manager to use a single instance per request
        app.CreatePerOwinContext(ApplicationDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
// rest of implementation ommitted for brevity.

then here is how you access it in a Controller class: 那么这是你在Controller类中访问它的方式:

public class AccountController : Controller
{
    private ApplicationSignInManager _signInManager;
    private ApplicationUserManager _userManager;

    public AccountController()
    {
    }

    // NOTE: ASP.NET will use this contructor and inject the instances
    // of SignInManager and UserManager from the OWIN container
    public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager )
    {
        UserManager = userManager;
        SignInManager = signInManager;
    }
    // there are implementations for the public properties
    // 'UserManager' and 'SignInManager' in the boiler plate code
    //  not shown here

Happy Coding! 快乐的编码!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ninject UserManager和UserStore - Ninject UserManager and UserStore 如何在UserManager和UserStore中使用DI - How to use DI with UserManager and UserStore UserManager.FindAsync不使用UserStore的自定义实现 - UserManager.FindAsync not working with custom implementation of UserStore 结合使用UserManager.FindAsync和自定义UserStore - Using UserManager.FindAsync with a custom UserStore 如何在 DI 中注册自定义 UserStore &amp; UserManager - How to register custom UserStore & UserManager in DI 如何使用用户管理器<identityuser>与用户存储<identityuser> ?</identityuser></identityuser> - How to use UserManager<IdentityUser> with UserStore<IdentityUser>? 与不同商店(UserStore,UserEmailStore,UserClaimStore,UserLockoutStore等)一起使用的UserManager - UserManager used with different stores (UserStore, UserEmailStore, UserClaimStore, UserLockoutStore etc.) ASP.NET Identity 中 UserStore 和 UserManager 的使用有什么区别? - What is the difference in the use of UserStore and UserManager in ASP.NET Identity? 如何为身份服务器自定义用户存储提供程序,并使我的 AccountController 与 IdentityServer 一起工作 - How to customize the userstore provider for identity server and also make my AccountController work together with IdentityServer 依赖注入UserManager正在处理异步调用(ASP.NET CORE) - Dependency Injected UserManager is disposing on async call (ASP.NET CORE)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM