//
        // GET: /TodoList/
        public async Task<ActionResult> Index()
        {
            AuthenticationResult result = null;
            List<TodoItem> itemList = new List<TodoItem>();

            try
            {
                string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
                AuthenticationContext authContext = new AuthenticationContext(Startup.Authority, new NaiveSessionCache(userObjectID));
                ClientCredential credential = new ClientCredential(clientId, appKey);
                result = authContext.AcquireTokenSilent(todoListResourceId, credential, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));

                //
                // Retrieve the user's To Do List.
                //
                HttpClient client = new HttpClient();
                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, todoListBaseAddress + "/api/todolist");
                request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
                HttpResponseMessage response = await client.SendAsync(request);

                //
                // Return the To Do List in the view.
                //
                if (response.IsSuccessStatusCode)
                {
                    List<Dictionary<String, String>> responseElements = new List<Dictionary<String, String>>();
                    JsonSerializerSettings settings = new JsonSerializerSettings();
                    String responseString = await response.Content.ReadAsStringAsync();
                    responseElements = JsonConvert.DeserializeObject<List<Dictionary<String, String>>>(responseString, settings);
                    foreach (Dictionary<String, String> responseElement in responseElements)
                    {
                        TodoItem newItem = new TodoItem();
                        newItem.Title = responseElement["Title"];
                        newItem.Owner = responseElement["Owner"];
                        itemList.Add(newItem);
                    }

                    return View(itemList);
                }
                else
                {
                    //
                    // If the call failed with access denied, then drop the current access token from the cache, 
                    //     and show the user an error indicating they might need to sign-in again.
                    //
                    if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                    {
                        var todoTokens = authContext.TokenCache.ReadItems().Where(a => a.Resource == todoListResourceId);
                        foreach (TokenCacheItem tci in todoTokens)
                            authContext.TokenCache.DeleteItem(tci);                            

                        ViewBag.ErrorMessage = "UnexpectedError";
                        TodoItem newItem = new TodoItem();
                        newItem.Title = "(No items in list)";
                        itemList.Add(newItem);
                        return View(itemList);
                    }
                }
            }
            catch (Exception ee)
            {
                if (Request.QueryString["reauth"] == "True")
                {
                    //
                    // Send an OpenID Connect sign-in request to get a new set of tokens.
                    // If the user still has a valid session with Azure AD, they will not be prompted for their credentials.
                    // The OpenID Connect middleware will return to this controller after the sign-in response has been handled.
                    //
                    HttpContext.GetOwinContext().Authentication.Challenge(OpenIdConnectAuthenticationDefaults.AuthenticationType);
                }

                //
                // The user needs to re-authorize.  Show them a message to that effect.
                //
                TodoItem newItem = new TodoItem();
                newItem.Title = "(Sign-in required to view to do list.)";
                itemList.Add(newItem);
                ViewBag.ErrorMessage = "AuthorizationRequired";
                return View(itemList);
            }


            //
            // If the call failed for any other reason, show the user an error.
            //
            return View("Error");

        }
        //
        // GET: /TodoList/
        public async Task<ActionResult> Index()
        {
            List<TodoItem> itemList = new List<TodoItem>();

            //
            // Get a token to call the To Do list.
            // This sample uses the web app's identity to call the service, not the user's identity.
            // The web app has special privilege in the service to view and update any user's To Do list.
            // Since the web app is trusted to sign the user in, it is a trusted sub-system.
            //

            //
            // Get an access token from Azure AD using client credentials.
            // If the attempt to get a token fails because the server is unavailable, retry twice after 3 seconds each.
            //
            AuthenticationResult result = null;
            int retryCount = 0;
            bool retry = false;

            do
            {
                retry = false;
                try
                {
                    // ADAL includes an in memory cache, so this call will only send a message to the server if the cached token is expired.
                    result = authContext.AcquireToken(todoListResourceId, clientCredential);
                }
                catch (AdalException ex)
                {
                    if (ex.ErrorCode == "temporarily_unavailable")
                    {
                        retry = true;
                        retryCount++;
                        Thread.Sleep(3000);
                    }
                }

            } while ((retry == true) && (retryCount < 3));

            if (result == null)
            {
                // Handle unexpected errors.
                ViewBag.ErrorMessage = "UnexpectedError";
                TodoItem newItem = new TodoItem();
                newItem.Title = "(No items in list)";
                itemList.Add(newItem);
                return View(itemList);
            }

            // Retrieve the user's Object Identifier claim, which is used as the key to the To Do list.
            string ownerId = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;

            //
            // Retrieve the user's To Do List.
            //
            HttpClient client = new HttpClient();
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, todoListBaseAddress + "/api/todolist?ownerid=" + ownerId);
            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
            HttpResponseMessage response = await client.SendAsync(request);

            //
            // Return the To Do List in the view.
            //
            if (response.IsSuccessStatusCode)
            {
                List<Dictionary<String, String>> responseElements = new List<Dictionary<String, String>>();
                JsonSerializerSettings settings = new JsonSerializerSettings();
                String responseString = await response.Content.ReadAsStringAsync();
                responseElements = JsonConvert.DeserializeObject<List<Dictionary<String, String>>>(responseString, settings);
                foreach (Dictionary<String, String> responseElement in responseElements)
                {
                    TodoItem newItem = new TodoItem();
                    newItem.Title = responseElement["Title"];
                    newItem.Owner = responseElement["Owner"];
                    itemList.Add(newItem);
                }

                return View(itemList);
            }
            else
            {
                // If the response is Unauthorized, drop the token cache to force getting a new token on the next try.
                if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                {
                    authContext.TokenCache.Clear();
                }

                // Handle unexpected errors.
                ViewBag.ErrorMessage = "UnexpectedError";
                TodoItem newItem = new TodoItem();
                newItem.Title = "(No items in list)";
                itemList.Add(newItem);
                return View(itemList);
            }

        }
        public async Task<ActionResult> Index(string item)
        {
            if (ModelState.IsValid)
            {
                //
                // Retrieve the user's tenantID and access token since they are parameters used to call the To Do service.
                //
                AuthenticationResult result = null;
                List<TodoItem> itemList = new List<TodoItem>();

                try
                {
                    string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
                    AuthenticationContext authContext = new AuthenticationContext(Startup.Authority, new NaiveSessionCache(userObjectID));
                    ClientCredential credential = new ClientCredential(clientId, appKey);
                    result = authContext.AcquireTokenSilent(todoListResourceId, credential, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));



                    // Forms encode todo item, to POST to the todo list web api.
                    HttpContent content = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("Title", item) });

                    //
                    // Add the item to user's To Do List.
                    //
                    HttpClient client = new HttpClient();
                    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, todoListBaseAddress + "/api/todolist");
                    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
                    request.Content = content;
                    HttpResponseMessage response = await client.SendAsync(request);

                    //
                    // Return the To Do List in the view.
                    //
                    if (response.IsSuccessStatusCode)
                    {
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        //
                        // If the call failed with access denied, then drop the current access token from the cache, 
                        //     and show the user an error indicating they might need to sign-in again.
                        //
                        if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                        {
                            var todoTokens = authContext.TokenCache.ReadItems().Where(a => a.Resource == todoListResourceId);
                            foreach (TokenCacheItem tci in todoTokens)
                                authContext.TokenCache.DeleteItem(tci);  

                            ViewBag.ErrorMessage = "UnexpectedError";
                            TodoItem newItem = new TodoItem();
                            newItem.Title = "(No items in list)";
                            itemList.Add(newItem);
                            return View(newItem);
                        }
                    }

                }
                catch (Exception ee)// TODO: verify that the exception is 'silentauth failed'
                {
                    //
                    // The user needs to re-authorize.  Show them a message to that effect.
                    //
                    TodoItem newItem = new TodoItem();
                    newItem.Title = "(No items in list)";
                    itemList.Add(newItem);
                    ViewBag.ErrorMessage = "AuthorizationRequired";
                    return View(itemList);

                }
                //
                // If the call failed for any other reason, show the user an error.
                //
                return View("Error");

            }

            return View("Error");
        }
        public async Task<ActionResult> Index(string item)
        {
            if (ModelState.IsValid)
            {
                List<TodoItem> itemList = new List<TodoItem>();

                //
                // Get an access token from Azure AD using client credentials.
                // If the attempt to get a token fails because the server is unavailable, retry twice after 3 seconds each.
                //
                AuthenticationResult result = null;
                int retryCount = 0;
                bool retry = false;

                do
                {
                    retry = false;
                    try
                    {
                        // ADAL includes an in memory cache, so this call will only send a message to the server if the cached token is expired.
                        result = authContext.AcquireToken(todoListResourceId, clientCredential);
                    }
                    catch (AdalException ex)
                    {
                        if (ex.ErrorCode == "temporarily_unavailable")
                        {
                            retry = true;
                            retryCount++;
                            Thread.Sleep(3000);
                        }
                    }

                } while ((retry == true) && (retryCount < 3));

                if (result == null)
                {
                    // Handle unexpected errors.
                    ViewBag.ErrorMessage = "UnexpectedError";
                    TodoItem newItem = new TodoItem();
                    newItem.Title = "(No items in list)";
                    itemList.Add(newItem);
                    return View(itemList);
                }

                // Retrieve the user's Object Identifier claim, which is used as the key to the To Do list.
                string ownerId = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;

                // Forms encode todo item, to POST to the todo list web api.
                HttpContent content = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("Title", item), new KeyValuePair<string, string>("Owner", ownerId) });

                //
                // Add the item to user's To Do List.
                //
                HttpClient client = new HttpClient();
                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, todoListBaseAddress + "/api/todolist");
                request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
                request.Content = content;
                HttpResponseMessage response = await client.SendAsync(request);

                //
                // Return the To Do List in the view.
                //
                if (response.IsSuccessStatusCode)
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    // If the response is Unauthorized, drop the token cache to force getting a new token on the next try.
                    if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                    {
                        authContext.TokenCache.Clear();
                    }

                    // Handle unexpected errors.
                    ViewBag.ErrorMessage = "UnexpectedError";
                    TodoItem newItem = new TodoItem();
                    newItem.Title = "(No items in list)";
                    itemList.Add(newItem);
                    return View(itemList);
                }

            }

            return View("Error");
        }
        //
        // GET: /TodoList/
        public async Task<ActionResult> Index()
        {
            AuthenticationResult result = null;
            List<TodoItem> itemList = new List<TodoItem>();
            AuthenticationContext authContext = null;
            if (useService)
            {
                try
                {
                    string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
                    authContext = new AuthenticationContext(Startup.Authority, new NaiveSessionCache(userObjectID));
                    ClientCredential credential = new ClientCredential(clientId, appKey);
                    result = authContext.AcquireTokenSilent(todoListResourceId, credential, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));

                    //
                    // Retrieve the user's To Do List.
                    //
                    HttpClient client = new HttpClient();
                    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, todoListBaseAddress + "/api/todolist");
                    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
                    HttpResponseMessage response = await client.SendAsync(request);

                    //
                    // Return the To Do List in the view.
                    //
                    if (response.IsSuccessStatusCode)
                    {
                        List<Dictionary<String, String>> responseElements = new List<Dictionary<String, String>>();
                        JsonSerializerSettings settings = new JsonSerializerSettings();
                        String responseString = await response.Content.ReadAsStringAsync();
                        responseElements = JsonConvert.DeserializeObject<List<Dictionary<String, String>>>(responseString, settings);
                        foreach (Dictionary<String, String> responseElement in responseElements)
                        {
                            TodoItem newItem = new TodoItem();
                            newItem.Title = responseElement["Title"];
                            newItem.Owner = responseElement["Owner"];
                            itemList.Add(newItem);
                        }

                        return View(itemList);
                    }
                    else
                    {
                        //
                        // If the call failed with access denied, then drop the current access token from the cache, 
                        //     and show the user an error indicating they might need to sign-in again.
                        //
                        if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                        {
                            var todoTokens = authContext.TokenCache.ReadItems().Where(a => a.Resource == todoListResourceId);
                            foreach (TokenCacheItem tci in todoTokens)
                                authContext.TokenCache.DeleteItem(tci);

                            ViewBag.ErrorMessage = "UnexpectedError";
                            TodoItem newItem = new TodoItem();
                            newItem.Title = "(No items in list)";
                            itemList.Add(newItem);
                            return View(itemList);
                        }
                    }
                }
                catch (Exception ee)
                {
                    if (Request.QueryString["reauth"] == "True")
                    {
                        //
                        // Send an OpenID Connect sign-in request to get a new set of tokens.
                        // If the user still has a valid session with Azure AD, they will not be prompted for their credentials.
                        // The OpenID Connect middleware will return to this controller after the sign-in response has been handled.
                        //
                        HttpContext.GetOwinContext().Authentication.Challenge(OpenIdConnectAuthenticationDefaults.AuthenticationType);
                    }

                    //
                    // The user needs to re-authorize.  Show them a message to that effect.
                    //
                    TodoItem newItem = new TodoItem();
                    newItem.Title = "(Sign-in required to view to do list.)";
                    itemList.Add(newItem);
                    ViewBag.ErrorMessage = "AuthorizationRequired";
                    return View(itemList);
                }
            }
            else
            {
                try
                {
                    string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
                    authContext = new AuthenticationContext(Startup.Authority, new NaiveSessionCache(userObjectID));
                    ClientCredential credential = new ClientCredential(clientId, appKey);
                    result = authContext.AcquireTokenSilent(azureSqlResourceId, credential, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));

                    // A user's To Do list is keyed off of the NameIdentifier claim, which contains an immutable, unique identifier for the user.
                    Claim subject = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier);

                    using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ToDoList"].ConnectionString))
                    {
                        if (!conn.ConnectionString.ToUpper().Contains("USER ID"))
                        {
                            conn.AccessToken = result.AccessToken;
                        }
                        conn.Open();

                        using (SqlCommand cmd = new SqlCommand("SELECT ID, Title, Owner FROM ToDoItems WHERE Owner = @Owner", conn))
                        {
                            cmd.CommandType = System.Data.CommandType.Text;
                            cmd.Parameters.AddWithValue("@Owner", subject.Value);

                            SqlDataReader rdr = cmd.ExecuteReader();
                            while (rdr.Read())
                            {
                                itemList.Add(new TodoItem
                                {
                                    Title = (string)rdr["Title"],
                                    Owner = (string)rdr["Owner"]
                                });
                            }

                            return View(itemList);
                        }

                    }

                }
                catch (AdalSilentTokenAcquisitionException ee)
                {
                    //
                    // The user needs to re-authorize.  Show them a message to that effect.
                    //
                    TodoItem newItem = new TodoItem();
                    newItem.Title = ee.Message;
                    itemList.Add(newItem);
                    ViewBag.ErrorMessage = "AuthorizationRequired";
                    return View(itemList);
                }
                catch (Exception ee)
                {
                    //
                    // An unexpected error occurred.  Show them a message to that effect.
                    //
                    TodoItem newItem = new TodoItem();
                    newItem.Title = ee.Message;
                    itemList.Add(newItem);
                    ViewBag.ErrorMessage = "UnexpectedError";
                    return View(itemList);
                }
            }

            //
            // If the call failed for any other reason, show the user an error.
            //
            return View("Error");

        }
        public async Task<ActionResult> Index(string item)
        {
            if (ModelState.IsValid)
            {
                //
                // Retrieve the user's tenantID and access token since they are parameters used to call the To Do service.
                //
                AuthenticationResult result = null;
                List<TodoItem> itemList = new List<TodoItem>();
                AuthenticationContext authContext = null;
                if (useService)
                {
                    try
                    {
                        string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
                        authContext = new AuthenticationContext(Startup.Authority, new NaiveSessionCache(userObjectID));
                        ClientCredential credential = new ClientCredential(clientId, appKey);
                        result = authContext.AcquireTokenSilent(todoListResourceId, credential, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));



                        // Forms encode todo item, to POST to the todo list web api.
                        HttpContent content = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("Title", item) });

                        //
                        // Add the item to user's To Do List.
                        //
                        HttpClient client = new HttpClient();
                        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, todoListBaseAddress + "/api/todolist");
                        request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
                        request.Content = content;
                        HttpResponseMessage response = await client.SendAsync(request);

                        //
                        // Return the To Do List in the view.
                        //
                        if (response.IsSuccessStatusCode)
                        {
                            return RedirectToAction("Index");
                        }
                        else
                        {
                            //
                            // If the call failed with access denied, then drop the current access token from the cache, 
                            //     and show the user an error indicating they might need to sign-in again.
                            //
                            if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                            {
                                var todoTokens = authContext.TokenCache.ReadItems().Where(a => a.Resource == todoListResourceId);
                                foreach (TokenCacheItem tci in todoTokens)
                                    authContext.TokenCache.DeleteItem(tci);

                                ViewBag.ErrorMessage = "UnexpectedError";
                                TodoItem newItem = new TodoItem();
                                newItem.Title = "(No items in list)";
                                itemList.Add(newItem);
                                return View(newItem);
                            }
                        }

                    }
                    catch (Exception ee)// TODO: verify that the exception is 'silentauth failed'
                    {
                        //
                        // The user needs to re-authorize.  Show them a message to that effect.
                        //
                        TodoItem newItem = new TodoItem();
                        newItem.Title = "(No items in list)";
                        itemList.Add(newItem);
                        ViewBag.ErrorMessage = "AuthorizationRequired";
                        return View(itemList);

                    }
                    //
                    // If the call failed for any other reason, show the user an error.
                    //
                    return View("Error");
                }
                else
                {
                    try
                    {
                        string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
                        authContext = new AuthenticationContext(Startup.Authority, new NaiveSessionCache(userObjectID));
                        ClientCredential credential = new ClientCredential(clientId, appKey);
                        result = authContext.AcquireTokenSilent(azureSqlResourceId, credential, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));

                        // A user's To Do list is keyed off of the NameIdentifier claim, which contains an immutable, unique identifier for the user.
                        Claim subject = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier);

                        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ToDoList"].ConnectionString))
                        {
                            if (!conn.ConnectionString.ToUpper().Contains("USER ID"))
                            {
                                conn.AccessToken = result.AccessToken;
                            }
                            conn.Open();

                            using (SqlCommand cmd = new SqlCommand("INSERT INTO ToDoItems (Title, Owner) VALUES (@Title, @Owner)", conn))
                            {
                                cmd.CommandType = System.Data.CommandType.Text;
                                cmd.Parameters.AddWithValue("@Title", item);
                                cmd.Parameters.AddWithValue("@Owner", subject.Value);

                                cmd.ExecuteNonQuery();
                            }

                            return RedirectToAction("Index");
                        }

                    }
                    catch (AdalSilentTokenAcquisitionException ee)
                    {
                        //
                        // The user needs to re-authorize.  Show them a message to that effect.
                        //
                        TodoItem newItem = new TodoItem();
                        newItem.Title = ee.Message;
                        itemList.Add(newItem);
                        ViewBag.ErrorMessage = "AuthorizationRequired";
                        return View(itemList);
                    }
                    catch (Exception ee)
                    {
                        //
                        // An unexpected error occurred.  Show them a message to that effect.
                        //
                        TodoItem newItem = new TodoItem();
                        newItem.Title = ee.Message;
                        itemList.Add(newItem);
                        ViewBag.ErrorMessage = "UnexpectedError";
                        return View(itemList);
                    }
                }

            }

            return View("Error");
        }
        //
        // GET: /TodoList/
        public async Task<ActionResult> Index()
        {
            //
            // Retrieve the user's tenantID and access token since they are parameters used to call the To Do service.
            //
            string tenantId = ClaimsPrincipal.Current.FindFirst(TenantIdClaimType).Value;
            string accessToken = TokenCacheUtils.GetAccessTokenFromCacheOrRefreshToken(tenantId, todoListResourceId);
            List<TodoItem> itemList = new List<TodoItem>();

            //
            // If the user doesn't have an access token, they need to re-authorize.
            //
            if (accessToken == null)
            {
                //
                // If refresh is set to true, the user has clicked the link to be sent to be authorized again.
                //
                if (Request.QueryString["reauth"] == "True")
                {
                    //
                    // Send an OpenID Connect sign-in request to get a new set of tokens.
                    // If the user still has a valid session with Azure AD, they will not be prompted for their credentials.
                    // The OpenID Connect middleware will return to this controller after the sign-in response has been handled.
                    //
                    HttpContext.GetOwinContext().Authentication.Challenge(OpenIdConnectAuthenticationDefaults.AuthenticationType);
                }

                //
                // The user needs to re-authorize.  Show them a message to that effect.
                //
                TodoItem newItem = new TodoItem();
                newItem.Title = "(Sign-in required to view to do list.)";
                itemList.Add(newItem);
                ViewBag.ErrorMessage = "AuthorizationRequired";
                return View(itemList);
            }

            //
            // Retrieve the user's To Do List.
            //
            HttpClient client = new HttpClient();
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, todoListBaseAddress + "/api/todolist");
            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
            HttpResponseMessage response = await client.SendAsync(request);

            //
            // Return the To Do List in the view.
            //
            if (response.IsSuccessStatusCode)
            {
                List<Dictionary<String, String>> responseElements = new List<Dictionary<String, String>>();
                JsonSerializerSettings settings = new JsonSerializerSettings();
                String responseString = await response.Content.ReadAsStringAsync();
                responseElements = JsonConvert.DeserializeObject<List<Dictionary<String, String>>>(responseString, settings);
                foreach (Dictionary<String, String> responseElement in responseElements)
                {
                    TodoItem newItem = new TodoItem();
                    newItem.Title = responseElement["Title"];
                    newItem.Owner = responseElement["Owner"];
                    itemList.Add(newItem);
                }

                return View(itemList);
            }
            else
            {
                //
                // If the call failed with access denied, then drop the current access token from the cache, 
                //     and show the user an error indicating they might need to sign-in again.
                //
                if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                {
                    TokenCacheUtils.RemoveAccessTokenFromCache(todoListResourceId);

                    ViewBag.ErrorMessage = "UnexpectedError";
                    TodoItem newItem = new TodoItem();
                    newItem.Title = "(No items in list)";
                    itemList.Add(newItem);
                    return View(itemList);
                }
            }

            //
            // If the call failed for any other reason, show the user an error.
            //
            return View("Error");

        }
        public async Task<ActionResult> Index(string item)
        {
            if (ModelState.IsValid)
            {
                //
                // Retrieve the user's tenantID and access token since they are parameters used to call the To Do service.
                //
                string tenantId = ClaimsPrincipal.Current.FindFirst(TenantIdClaimType).Value;
                string accessToken = TokenCacheUtils.GetAccessTokenFromCacheOrRefreshToken(tenantId, todoListResourceId);
                List<TodoItem> itemList = new List<TodoItem>();

                //
                // If the user doesn't have an access token, they need to re-authorize.
                //
                if (accessToken == null)
                {
                    //
                    // The user needs to re-authorize.  Show them a message to that effect.
                    //
                    TodoItem newItem = new TodoItem();
                    newItem.Title = "(No items in list)";
                    itemList.Add(newItem);
                    ViewBag.ErrorMessage = "AuthorizationRequired";
                    return View(itemList);
                }

                // Forms encode todo item, to POST to the todo list web api.
                HttpContent content = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("Title", item) });

                //
                // Add the item to user's To Do List.
                //
                HttpClient client = new HttpClient();
                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, todoListBaseAddress + "/api/todolist");
                request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
                request.Content = content;
                HttpResponseMessage response = await client.SendAsync(request);

                //
                // Return the To Do List in the view.
                //
                if (response.IsSuccessStatusCode)
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    //
                    // If the call failed with access denied, then drop the current access token from the cache, 
                    //     and show the user an error indicating they might need to sign-in again.
                    //
                    if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                    {
                        TokenCacheUtils.RemoveAccessTokenFromCache(todoListResourceId);

                        ViewBag.ErrorMessage = "UnexpectedError";
                        TodoItem newItem = new TodoItem();
                        newItem.Title = "(No items in list)";
                        itemList.Add(newItem);
                        return View(newItem);
                    }
                }

                //
                // If the call failed for any other reason, show the user an error.
                //
                return View("Error");

            }

            return View("Error");
        }