如何使用标识以编程方式在asp.net 5中创建用户

时间:2015-09-15 11:11:26

标签: c# asp.net asp.net-mvc

我试图在种子方法中创建一些用户,但没有运气。

我曾使用过这个例子:How to add role and Users in asp.net mvc 5?UserManager接缝要以不同的方式实例化。

查看AccountController,此实例将加载到构造函数中:

    public AccountController(
        UserManager<ApplicationUser> userManager,
        SignInManager<ApplicationUser> signInManager,
        IEmailSender emailSender,
        ISmsSender smsSender,
        BennerCliContext applicationDbContext)
    {
        _userManager = userManager;
        _signInManager = signInManager;
        _emailSender = emailSender;
        _smsSender = smsSender;
        _applicationDbContext = applicationDbContext;
    }

但是我的种子方法取决于MyDbContext类。

如何从那里访问UserManager实例?

还有其他方法可以在vnext中创建用户吗?

更新

在@Vladislav Karamfilov建议之后:

enter image description here

2 个答案:

答案 0 :(得分:5)

由于用户管理员已经在ASP.NET5的服务提供商中注册,您可以通过调用以下方法轻松获取用户管理器的实例:

 var userManager=new HttpContextAccessor()
     .HttpContext.ApplicationServices.GetService(typeof(UserManager<ApplicationUser>));

答案 1 :(得分:0)

您应该创建<ul> <li> first item </li> <li> second item very long content </li> <li> third item </li> <li> This is 200px wide</li> </ul>的实例,然后使用UserManager<ApplicationUser>方法创建CreateIdentity对象。但是,如果您将管理员角色分配给创建的用户并且您的代码是公开的,请务必小心不要公开您网站的安全性