// ############################################################ // Get access token // LoginItems is a resource model class that holds the user name and password // TokenResponse is a resource model class that matches the shape of the response // from the IA Server token endpoint public async Task <bool> GetAccessToken(LoginItems loginItems) { // Continue? if (loginItems == null) { return(false); } // Clean the incoming data // Packaging alternatives... dictionary or list of key-value pairs // Create a package for the request - dictionary var rd = new Dictionary <string, string>(); rd.Add("grant_type", "password"); // etc. // Create a package for the request - list of key-value pairs var requestData = new List <KeyValuePair <string, string> >(); requestData.Add(new KeyValuePair <string, string>("grant_type", "password")); requestData.Add(new KeyValuePair <string, string>("password", loginItems.password)); requestData.Add(new KeyValuePair <string, string>("username", loginItems.username)); // etc. // Create an HttpContent object var content = new FormUrlEncodedContent(requestData); // Create a request using (var request = CreateRequest()) { // Send the request... POST, to token endpoint, with the form URL encoded content // Make it complete by adding the Result property var response = request.PostAsync("http://localhost:2548/token", content).Result; if (response.IsSuccessStatusCode) { // Extract the token from the response var tokenResponse = await response.Content.ReadAsAsync <TokenResponse>(); // Save the token in session state HttpContext.Current.Session["token"] = tokenResponse.access_token; return(true); } else { return(false); } } }
public ActionResult Login(LoginItems newItem) { // Validate the input if (!ModelState.IsValid) { return(View(newItem)); } // Process the input var addedItem = m.GetAccessToken(newItem); if (addedItem.Result == false) { return(View(newItem)); } else { return(RedirectToAction("index")); } }