Reputation: 1637
VS2013, MVC5, VB
The MVC5 template works well to create new users from the Register View while the application is running. But if I want to create users in code, how do I do that?
The following is naïve code, but demonstrates what doesn't work:
Dim user = New ApplicationUser() With {.UserName = "[email protected]", .Email = "[email protected]"}
Dim acct As New AccountController
acct.UserManager.CreateAsync(user, "Temp1.11")
This doesn't work because there is no HttpContext for UserManager.
There's a lot going on here that I don't understand and I'm a little lost. What I set out to do was simply create a list of users using the seed method in Configuration.vb. I thought I could just copy the way the existing AccountController.Register method works, but obviously not.
The answer to my question would ultimately be how to create users in the seed, but I'd like to also understand why my thinking was so wrong about simply trying to use portions of the code from the Register method. I don't quite understand the HttpContext and how that comes into being.
Upvotes: 0
Views: 336
Reputation: 118977
You don't need to use AccountController
to get access to the UserManager
object. Instead just create that directly:
Dim user = New ApplicationUser() With {.UserName = "[email protected]", .Email = "[email protected]"}
Dim myUserManager As New UserManager()
myUserManager.CreateAsync(user, "Temp1.11")
Upvotes: 1