gpt4 book ai didi

c# - 在 AccountController 之外配置 UserManager 和 UserStore?

转载 作者:行者123 更新时间:2023-11-30 16:53:42 24 4
gpt4 key购买 nike

这够好吗?或者我还必须处理 UserStore 吗?如果我确实需要任何建议,我们将不胜感激。我是 ASP.NET Identity 的新手。

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

}
}

我想这样会更好:

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

}
}
}

编辑:很高兴我问了这个问题,尽管我可能已经回答了我最初的问题。感谢 Glenn Ferrie,将检查 ASP.NET 依赖项注入(inject)。

最佳答案

这是使用 VS 2015 RC 创建的新 ASP.NET MVC (.NET 4.6) 的一些代码片段。首先是 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.

然后这是在 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

编码愉快!

关于c# - 在 AccountController 之外配置 UserManager 和 UserStore?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31196830/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com