public async Task<HttpResponseMessage> Post([FromBody] User user) { if (user == null || string.IsNullOrEmpty(user.UserName) || string.IsNullOrEmpty(user.Password)) return new HttpResponseMessage(HttpStatusCode.BadRequest); var appUser = new ApplicationUser { UserName = user.UserName }; var result = userManager.Create(appUser, user.Password); if (result.Succeeded) { var result2 = await userManager.AddUserToRolesAsync(appUser.Id, user.Roles); if (result2.Succeeded) { return Request.CreateResponse(HttpStatusCode.Created); } return ErrorResponse(result2.Errors.FirstOrDefault()); } return ErrorResponse(result.Errors.FirstOrDefault()); }
static void Main(string[] args) { if (args == null || args.Length < 3) { string codeBase = Assembly.GetExecutingAssembly().CodeBase; string exeName = Path.GetFileName(codeBase); Console.WriteLine("usage: {0} <username> <password> <role>", exeName); return; } var username = args[0]; var password = args[1]; var role = args[2]; var mongoContext = new MongoContext(); var identityContext = new ApplicationIdentityContext(mongoContext); var store = new UserStore<ApplicationUser>(identityContext); var manager = new ApplicationUserManager(store); var user = new ApplicationUser { UserName = username }; var result = manager.Create(user, password); if (result.Succeeded) { var roles = new[] { role }; var roleResult = manager.AddUserToRolesAsync(user.Id, roles); roleResult.Wait(); Console.WriteLine("user created!"); Console.ReadKey(); return; } foreach (var error in result.Errors) { Console.WriteLine(error); } Console.ReadKey(); }