Ejemplo n.º 1
0
        public void TestPasswords()
        {
            var email = Email;
            var password = Password;
            var accountModel = new AccountModel { UserName = email, Password = NewPassword };
            var token = GetToken(accountModel);
            var pass = NewPassword;//"test@123B";//
            var client = GetClient();
            client.DefaultRequestHeaders.Add("Authorization", token.TokenType + " " + token.AccessToken);
            var changePassModel = new ChangePasswordModel
            {
                OldPassword = NewPassword,
                NewPassword = pass,
                ConfirmPassword = pass
            };
            var cRes = client.PostAsJsonAsync(routPrefix+"/ChangePassword", changePassModel).Result;
            var content = cRes.Content.ReadAsStringAsync().Result;
            Assert.AreEqual(HttpStatusCode.OK, cRes.StatusCode);

            // If password has been setted, will throw exception
            // {"Message":"请求无效。","ModelState":{"":["已为用户设置了密码。"]}}
            token = GetToken(new AccountModel {UserName = Email, Password = pass});
            client.DefaultRequestHeaders.Remove("Authorization");
            client.DefaultRequestHeaders.Add("Authorization", token.TokenType + " " + token.AccessToken);
            var setPassModel = new SetPasswordModel { NewPassword = password, ConfirmPassword = password };
            var sRes = client.PostAsJsonAsync(routPrefix+"/SetPassword", setPassModel).Result;
            var sContent = sRes.Content.ReadAsStringAsync().Result;
            Assert.AreEqual(HttpStatusCode.BadRequest, sRes.StatusCode);
        }
Ejemplo n.º 2
0
 internal static TokenModel GetToken(AccountModel model)
 {
     using (var client = GetClient())
     {
         var dict = new Dictionary<string, string>
         {
             {"UserName", model.UserName},
             {"Password", model.Password},
             {"grant_type", "password"}
         };
         var m = new FormUrlEncodedContent(dict);
         var res = client.PostAsync("/Token", m);
         res.Wait();
         var obj = res.Result;
         //var list = new List<string>();
         //foreach (var h in obj.Headers)
         //{
         //    list.Add(h.Key + ": " + h.Value);
         //}
         var resContent = obj.Content.ReadAsStringAsync();
         resContent.Wait();
         if (!obj.IsSuccessStatusCode)
         {
             throw new Exception(resContent.Result);
         }
         var token = JsonConvert.DeserializeObject<TokenModel>(resContent.Result);
         return token;
     }
 }