UserManager.CreateAsync无法正常工作,但可以使用其他代码段

时间:2017-02-13 03:28:49

标签: asp.net asp.net-mvc asp.net-web-api2

我有一个web api如下。当我调用此API时,它只会在UserManager.CreateAsync行上出错。相同的代码适用于MVC Web API上的default / register方法。

[Route("settestaccounts")]
    public async Task<IHttpActionResult> SetTestAccounts(TestAccounts TestAccount)
    {
        if ((TestAccount.AccountCount == 0) || (TestAccount.AccountCount < 0) || (!TestAccount.SecretKey.Equals("test")))
        {
            return BadRequest("Please send valid data");
        }

        db = new Entities();
        List<string> outLogins = new List<string>();

        do
        {
            string testLogin = null; // string.Empty;
            do
            {
                string login = string.Format("user{0}@domain.net", GetRandomNumber(1, 10000));
                testLogin = (from p in db.AspNetUsers
                             where p.UserName.Equals(login)
                             select p.Id).FirstOrDefault();

                if (string.IsNullOrEmpty(testLogin))
                {
                    outLogins.Add(login);
                }
            } while (testLogin != null);
        } while (outLogins.Count < TestAccount.AccountCount);

        foreach (string email in outLogins)
         {
            var newAccounts = new AccountController();
            var registerAccount = new Models.RegisterBindingModel()
            {
                Email = email,
                FirstName = "test",
                LastName = "test",
                Password = "Wadirere123",
                ConfirmPassword = "Wadirere123",
                ZipCode = Convert.ToString(94111),
                RoleName = "User"
            };

            string confkey = ConfigurationManager.AppSettings["ConfirmEmailValidKey"];
            var user = new ApplicationUser() { UserName = registerAccount.Email, Email = registerAccount.Email, FirstName = registerAccount.FirstName, LastName = registerAccount.LastName, ZipCode = registerAccount.ZipCode, RegistrationDateTime = DateTime.UtcNow };

            var result = await UserManager.CreateAsync(user, registerAccount.Password);

            var c = result.Errors;

            //   var role = await newAccounts.UserManager.AddToRoleAsync(user.Id, registerAccount.RoleName);

            //ApplicationUser user = await newAccounts.UserManager.FindByEmailAsync(email.Trim());
            //string code = await newAccounts.UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
            //IdentityResult result = await newAccounts.UserManager.ConfirmEmailAsync(user.Id, code);
            //newAccounts.UpdateDatabaseAndSendWelcomeNotification(user.Id, null, confkey, result);
        }

        return Ok(Newtonsoft.Json.JsonConvert.SerializeObject(new OutLogins() { Logins = outLogins }));
    }

我也使用API​​监控,在该代码中它显示内部服务器错误500,但没有说明错误是什么?

注意:我从Python客户端调用此API(settestaccounts),但也从Python代码调用注册API,并且执行正常。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您可以使用Errors属性检查错误并修复它。