title: OAuth2 + Generate New Authorization Bearer Access Token C# MVC
body: <p>Could any one help me out to Generate Access Token in C# MVC.</p>
  
  <p><a href="https://i.stack.imgur.com/xV2Q0.jpg" rel="nofollow noreferrer">PostMan Access Token Generation</a></p>
  
  <p><strong>Below Code Works fine in Console Application But its not working in MVC c# am getting Error as "URL Could not be Resolved"</strong></p>
  
  <pre><code> static void Main(string[] args)  {
         Console.WriteLine("Starting ...");
          DoIt().Wait();
          Console.ReadLine();
      }
   private static async Task&lt;string&gt; DoIt()     {
  
          accessToken = await GetAccessToken();
  
  return accessToken;
      }
  
      private static async Task&lt;string&gt; GetAccessToken()
      {
          using (var client = new HttpClient())
          {
              client.BaseAddress = new Uri(baseUrl);
  
              // We want the response to be JSON.
              client.DefaultRequestHeaders.Accept.Clear();
              client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  
              // Build up the data to POST.
              List&lt;KeyValuePair&lt;string, string&gt;&gt; postData = new List&lt;KeyValuePair&lt;string, string&gt;&gt;();
              postData.Add(new KeyValuePair&lt;string, string&gt;("grant_type", "client_credentials"));
              postData.Add(new KeyValuePair&lt;string, string&gt;("client_id", "clientId"));
              postData.Add(new KeyValuePair&lt;string, string&gt;("client_secret", "clientSecret"));
              postData.Add(new KeyValuePair&lt;string, string&gt;("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;
          }
      }
  </code></pre>