コード例 #1
0
        public void ChangePassword()
        {
            Database.SetInitializer(new ManahostManagerInitializer());
            using (ManahostManagerDAL prectx = new ManahostManagerDAL())
            {
                prectx.Database.Delete();
            }
            var passwordModel = new ChangePasswordAccountModel()
            {
                CurrentPassword      = ControllerUtils.password,
                Password             = "******",
                PasswordConfirmation = "JeSuisUnNooB88$$"
            };

            using (var server = TestServer.Create <WebApiApplication>())
            {
                HttpResponseMessage response = server.CreateRequest("/token").And((x) => x.Content = new StringContent(string.Format("grant_type=password&username={0}&password={1}&client_id=UNITTEST&client_secret=BLAHBLAHCAR", ControllerUtils.username, ControllerUtils.password), Encoding.UTF8, "application/x-www-form-urlencoded")).PostAsync().Result;
                TokenAuth           token    = response.Content.ReadAsAsync <TokenAuth>().Result;
                Assert.AreEqual(response.StatusCode, HttpStatusCode.OK, "Status Authentication");

                var result = server.CreateRequest("/api/Account/Password").And(x =>
                {
                    x.Content = new ObjectContent(typeof(ChangePasswordAccountModel), passwordModel, new JilFormatter());
                    x.Content.Headers.ContentType = new MediaTypeHeaderValue(GenericNames.APP_JSON);
                }).AddHeader("Authorization", new AuthenticationHeaderValue("Bearer", token.access_token).ToString()).PostAsync().Result;
                Assert.AreEqual(HttpStatusCode.OK, result.StatusCode, "Status Change Password");

                response = server.CreateRequest("/token").And((x) => x.Content = new StringContent(string.Format("grant_type=password&username={0}&password={1}&client_id=UNITTEST&client_secret=BLAHBLAHCAR", ControllerUtils.username, "JeSuisUnNooB88$$"), Encoding.UTF8, "application/x-www-form-urlencoded")).PostAsync().Result;
                Assert.AreEqual(response.StatusCode, HttpStatusCode.OK, "Status Authentication with new password");
                token = response.Content.ReadAsAsync <TokenAuth>().Result;
            }
        }
コード例 #2
0
        public async Task <IHttpActionResult> PostChangePassword(ChangePasswordAccountModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var client = await Manager.FindByIdAsync(User.Identity.GetUserId <int>());

            if (!await Manager.CheckPasswordAsync(client, model.CurrentPassword))
            {
                ModelState.AddModelError("CurrentPassword", GenericError.INVALID_GIVEN_PARAMETER);
                return(BadRequest(ModelState));
            }
            var result = await Manager.ChangePasswordAsync(client.Id, model.CurrentPassword, model.Password);

            if (result.Succeeded)
            {
                return(Ok());
            }
            ModelState.AddModelError("Password", GenericError.INVALID_GIVEN_PARAMETER);
            return(BadRequest());
        }