public ActionResult Create(CreateViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            try
            {
                _apiService.CreateTodo(User.Identity.GetToken(), model.Name);

                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                mvcasgateway.Api.Client.ApiException apiException = _apiService.TryCastApiException(ex);
                if (apiException != null)
                {
                    if ((HttpStatusCode)apiException.ErrorCode == HttpStatusCode.Unauthorized)
                    {
                        return(RedirectToAction("Logout"));
                    }
                    else if (!AddResponseErrorsToModelState(_apiService.GetErrorMessage(apiException)))
                    {
                        return(View("Error"));
                    }
                }
                else
                {
                    return(View("Error"));
                }
            }

            return(View(model));
        }
Example #2
0
        public ActionResult Register(RegisterViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            try
            {
                _apiService.Register(model.Email, model.FirstName, model.LastName, model.Password, model.ConfirmPassword);

                return(RedirectToAction("Login", "Account"));
            }
            catch (Exception ex)
            {
                mvcasgateway.Api.Client.ApiException apiException = _apiService.TryCastApiException(ex);
                if (apiException != null)
                {
                    if ((HttpStatusCode)apiException.ErrorCode == HttpStatusCode.Unauthorized)
                    {
                        return(RedirectToAction("Logout"));
                    }
                    else if (!AddResponseErrorsToModelState(_apiService.GetErrorMessage(apiException)))
                    {
                        return(View("Error"));
                    }
                }
                else
                {
                    return(View("Error"));
                }
            }

            return(View(model));
        }
        // GET: Todo
        public ActionResult Index()
        {
            var model = new IndexViewModel();

            try
            {
                model.TodoList = _apiService.GetTodos(User.Identity.GetToken()).Todo.Select(_ => new Todo
                {
                    Id   = _.Id.GetValueOrDefault(),
                    Name = _.Name
                });
            }
            catch (Exception ex)
            {
                mvcasgateway.Api.Client.ApiException apiException = _apiService.TryCastApiException(ex);
                if (apiException != null)
                {
                    if ((HttpStatusCode)apiException.ErrorCode == HttpStatusCode.Unauthorized)
                    {
                        return(RedirectToAction("Logout", "Account"));
                    }
                    else if (!AddResponseErrorsToModelState(_apiService.GetErrorMessage(apiException)))
                    {
                        return(View("Error"));
                    }
                }
                else
                {
                    //TODO: Properly return Handle Error page
                    return(View("Error"));
                }
            }

            return(View(model));
        }
Example #4
0
        public ActionResult Login(LoginViewModel model)
        {
            try
            {
                string token     = _apiService.Login(model.Email, model.Password);
                var    principal = JwtTokenHelper.ValidateToken(token) as ClaimsPrincipal;

                int expInSec = 0;
                int.TryParse(principal.Claims.FirstOrDefault(_ => _.Type.Equals("exp"))?.Value, out expInSec);

                AuthenticationProperties options = new AuthenticationProperties();
                options.AllowRefresh = true;
                options.IsPersistent = true;
                options.ExpiresUtc   = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(expInSec);

                var claims = new[]
                {
                    new Claim(ClaimTypes.NameIdentifier, model.Email),
                    new Claim("userid", principal.Claims.FirstOrDefault(_ => _.Type.Equals("userid"))?.Value),
                    new Claim("username", principal.Claims.FirstOrDefault(_ => _.Type.Equals("username"))?.Value),
                    new Claim("firstname", principal.Claims.FirstOrDefault(_ => _.Type.Equals("firstname"))?.Value),
                    new Claim("lastname", principal.Claims.FirstOrDefault(_ => _.Type.Equals("lastname"))?.Value),
                    new Claim("exp", principal.Claims.FirstOrDefault(_ => _.Type.Equals("exp"))?.Value),
                    new Claim("exputc", options.ExpiresUtc.ToString()),
                    new Claim("token", string.Format("Bearer {0}", token)),
                };

                var identity = new ClaimsIdentity(claims, "ApplicationCookie");
                Request.GetOwinContext().Authentication.SignIn(options, identity);

                return(RedirectToAction("Index", "Manage"));
            }
            catch (Exception ex)
            {
                mvcasgateway.Api.Client.ApiException apiException = _apiService.TryCastApiException(ex);
                if (apiException != null)
                {
                    if ((HttpStatusCode)apiException.ErrorCode == HttpStatusCode.Unauthorized)
                    {
                        return(RedirectToAction("Logout"));
                    }
                    else if (!AddResponseErrorsToModelState(_apiService.GetErrorMessage(apiException)))
                    {
                        return(View("Error"));
                    }
                }
                else
                {
                    return(View("Error"));
                }
            }

            return(View(model));
        }
Example #5
0
        public string GetErrorMessage(mvcasgateway.Api.Client.ApiException ex)
        {
            var errorCon  = ex.ErrorContent;
            var jsontoken = JObject.Parse(errorCon);

            if (jsontoken["ModelState"] != null)
            {
                return(jsontoken["ModelState"].ToString());
            }
            else if (jsontoken["Message"] != null)
            {
                return(jsontoken["Message"].ToString());
            }
            else
            {
                return(Convert.ToString(errorCon));
            }
        }
Example #6
0
        public void CreateTodo()
        {
            // Arrange
            var    client = new ApiService();
            string guid   = Guid.NewGuid().ToString().Replace("-", "");
            string token  = null;
            bool   inputValidationTestPassed = true;

            mvcasgateway.Api.Model.GetToDoResponse response = null;
            try { token = client.Login("*****@*****.**", "abcd1234"); } catch (Exception ex) { mvcasgateway.Api.Client.ApiException apiException = (mvcasgateway.Api.Client.ApiException)ex; }

            // Act
            //username length exceed 50
            try { client.CreateTodo("Bearer " + token, "012345678901234567890123456789012345678901234567890123456789"); inputValidationTestPassed = false; } catch (Exception ex) { mvcasgateway.Api.Client.ApiException apiException = (mvcasgateway.Api.Client.ApiException)ex; }
            //valid
            try { client.CreateTodo("Bearer " + token, guid); } catch (Exception ex) { mvcasgateway.Api.Client.ApiException apiException = (mvcasgateway.Api.Client.ApiException)ex; inputValidationTestPassed = false; }

            // Assert
            Assert.IsNotNull(token);
            Assert.IsTrue(inputValidationTestPassed);
        }
Example #7
0
        public void GetTodos()
        {
            // Arrange
            var    client = new ApiService();
            string token  = null;

            mvcasgateway.Api.Model.GetToDoResponse response = null;
            try { token = client.Login("*****@*****.**", "abcd1234"); } catch (Exception ex) { mvcasgateway.Api.Client.ApiException apiException = (mvcasgateway.Api.Client.ApiException)ex; }

            // Act
            try { response = client.GetTodos("Bearer " + token); } catch (Exception ex) { mvcasgateway.Api.Client.ApiException apiException = (mvcasgateway.Api.Client.ApiException)ex; }

            // Assert
            Assert.IsNotNull(token);
            Assert.IsNotNull(response);
        }
Example #8
0
        public void LoginTest()
        {
            // Arrange
            var    client = new ApiService();
            string guid   = Guid.NewGuid().ToString().Replace("-", "");
            bool   inputValidationTestPassed = true;
            string token = null;

            // Act
            //username length exceed 50
            try { client.Login(guid + "@testing12345678910.com", "abcd1234"); inputValidationTestPassed = false; } catch (Exception ex) { mvcasgateway.Api.Client.ApiException apiException = (mvcasgateway.Api.Client.ApiException)ex; }
            //password length exceed 25
            try { client.Login("*****@*****.**", "01234567890123456789123456"); inputValidationTestPassed = false; } catch (Exception ex) { mvcasgateway.Api.Client.ApiException apiException = (mvcasgateway.Api.Client.ApiException)ex; }

            //valid
            try { token = client.Login("*****@*****.**", "abcd1234"); } catch (Exception ex) { mvcasgateway.Api.Client.ApiException apiException = (mvcasgateway.Api.Client.ApiException)ex; }

            // Assert
            Assert.IsTrue(inputValidationTestPassed);
            Assert.IsNotNull(token);
        }
Example #9
0
        public void RegisterTest()
        {
            // Arrange
            var    client = new ApiService();
            string guid   = Guid.NewGuid().ToString().Replace("-", "");
            bool   inputValidationTestPassed         = true;
            bool   existingEmailValidationTestPassed = true;

            // Act
            //invalid email
            try { client.Register(guid, "test", "", "abcd1234", "abcd1234"); inputValidationTestPassed = false; } catch (Exception ex) { mvcasgateway.Api.Client.ApiException apiException = (mvcasgateway.Api.Client.ApiException)ex; }
            //invalid password length
            try { client.Register(guid + "@testing.com", "test", "", "abc", "abc"); inputValidationTestPassed = false; } catch (Exception ex) { mvcasgateway.Api.Client.ApiException apiException = (mvcasgateway.Api.Client.ApiException)ex; }
            //confirm password not same
            try { client.Register(guid + "@testing.com", "test", "", "abcd1234", "abcd12345"); inputValidationTestPassed = false; } catch (Exception ex) { mvcasgateway.Api.Client.ApiException apiException = (mvcasgateway.Api.Client.ApiException)ex; }

            //email length exceed 50
            try { client.Register(guid + "@testing12345678910.com", "test", "", "abcd1234", "abcd1234"); inputValidationTestPassed = false; } catch (Exception ex) { mvcasgateway.Api.Client.ApiException apiException = (mvcasgateway.Api.Client.ApiException)ex; }
            //first name length exceed 25
            try { client.Register(guid + "@testing.com", "01234567890123456789123456", "", "abcd1234", "abcd1234"); inputValidationTestPassed = false; } catch (Exception ex) { mvcasgateway.Api.Client.ApiException apiException = (mvcasgateway.Api.Client.ApiException)ex; }
            //last name length exceed 25
            try { client.Register(guid + "@testing.com", "", "01234567890123456789123456", "abcd1234", "abcd1234"); inputValidationTestPassed = false; } catch (Exception ex) { mvcasgateway.Api.Client.ApiException apiException = (mvcasgateway.Api.Client.ApiException)ex; }
            //password length exceed 25
            try { client.Register(guid + "@testing.com", "test", "", "01234567890123456789123456", "01234567890123456789123456"); inputValidationTestPassed = false; } catch (Exception ex) { mvcasgateway.Api.Client.ApiException apiException = (mvcasgateway.Api.Client.ApiException)ex; }

            //valid
            try { client.Register(guid + "@testing.com", "test", "", "abcd1234", "abcd1234"); } catch (Exception ex) { mvcasgateway.Api.Client.ApiException apiException = (mvcasgateway.Api.Client.ApiException)ex; inputValidationTestPassed = false; }
            //email exists
            try { client.Register(guid + "@testing.com", "test", "", "abcd1234", "abcd1234"); existingEmailValidationTestPassed = false; } catch (Exception ex) { mvcasgateway.Api.Client.ApiException apiException = (mvcasgateway.Api.Client.ApiException)ex; }

            // Assert
            Assert.IsTrue(inputValidationTestPassed);
            Assert.IsTrue(existingEmailValidationTestPassed);
        }