Generate Access Token From C# MVC RRS feed

  • Question

  • Hi All,

    Could any one help me out to Generate Access Token in C# MVC.

    i Tried the below Code in Console Application and it worked Fine but it does not work in MVC

    Working Code in Console Application But not working in MVC c# am getting Error as "URL Could not be Resolved"

    static void Main(string[] args)
                Console.WriteLine("Starting ...");


     private static async Task<string> DoIt()
                // Get the Access Token.
                accessToken = await GetAccessToken();

        return accessToken;

            private static async Task<string> GetAccessToken()
                using (var client = new HttpClient())
                    client.BaseAddress = new Uri(baseUrl);

                    // We want the response to be JSON.
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    // Build up the data to POST.
                    List<KeyValuePair<string, string>> postData = new List<KeyValuePair<string, string>>();
                    postData.Add(new KeyValuePair<string, string>("grant_type", "client_credentials"));
                    postData.Add(new KeyValuePair<string, string>("client_id", "clientId"));
                    postData.Add(new KeyValuePair<string, string>("client_secret", "clientSecret"));
                    postData.Add(new KeyValuePair<string, string>("access_token_url", "Access Token URL"));

                    FormUrlEncodedContent content = new FormUrlEncodedContent(postData);

                    // Post to the Server and parse the response.
                    HttpResponseMessage response = client.PostAsync("Token", content).Result;
                    string jsonString = await response.Content.ReadAsStringAsync();
                    object responseData = JsonConvert.DeserializeObject(jsonString);

                    // return the Access Token.
                    return ((dynamic)responseData).access_token;

    • Moved by CoolDadTx Tuesday, May 22, 2018 1:52 PM ASP.NET related
    Tuesday, May 22, 2018 7:28 AM

All replies

  • Could any one help me out to Generate Access Token in C# MVC.


    Tuesday, May 22, 2018 8:18 AM