Question Error retrieving token ASP.NET Web API template

yaza

New member
Joined
Sep 10, 2016
Messages
1
Programming Experience
10+
I?m working with a project that has been created as a ASP.Net Web Application with the ?Web API? template and ?Individual User Accounts? enabled as the authentication option. I have a console application that consumes the web api. But When I want to get the token it gives me an html string with 404 not found in stead of a json array. What am I doing wrong?


This is mij console app code:

C#:
using ConsoleApplication1.Helpers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
{
    const string userName = "user@user.com";
    const string password = "Password01!";
    const string apiBaseUri = "http://localhost/WebAPITest";
    const string apiGetPeoplePath = "/api/people";

    static void Main(string[] args)
    {
        //Get the token
        var token = GetAPIToken(userName, password, apiBaseUri).Result;
        Console.WriteLine("Token: {0}", token);

        //Make the call
        var response = GetRequest(token, apiBaseUri, apiGetPeoplePath).Result;
        Console.WriteLine("response: {0}", response);

        //wait for key press to exit
        Console.ReadKey();
    }

    private static async Task<string> GetAPIToken(string userName, string password, string apiBaseUri)
    {
        using (var client = new HttpClient())
        {
            //setup client
            client.BaseAddress = new Uri(apiBaseUri);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            //setup login data
            var formContent = new FormUrlEncodedContent(new[]
            {
 new KeyValuePair<string, string>("grant_type", "password"),
 new KeyValuePair<string, string>("username", userName),
 new KeyValuePair<string, string>("password", password),
 });

            //send request
            HttpResponseMessage responseMessage = await client.PostAsync("/Token", formContent);

            //get access token from response body
            var responseJson = await responseMessage.Content.ReadAsStringAsync();
            var jObject = JObject.Parse(responseJson);
            return jObject.GetValue("access_token").ToString();
        }
    }

    static async Task<string> GetRequest(string token, string apiBaseUri, string requestPath)
    {
        using (var client = new HttpClient())
        {
            //setup client
            client.BaseAddress = new Uri(apiBaseUri);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);

            //make request
            HttpResponseMessage response = await client.GetAsync(requestPath);
            var responseString = await response.Content.ReadAsStringAsync();
            return responseString;
        }
    }
}

}

Am I missing something?
 
Back
Top Bottom