2017-02-13 31 views
0

我有一個web api如下。當我調用這個API時,它只是在UserManager.CreateAsync行上出錯。 MVC Web API的默認/註冊方法使用相同的代碼。UserManager.CreateAsync不工作,但從另一個代碼片段

[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),但註冊API也從Python代碼調用,並且執行得很好。

任何想法?

回答

0

您可以使用Errors屬性檢查錯誤並修復它。