title: How to consume a secure Rest MVC web api
body: <p>I'm just a beginner on the .NET world and I've created a web api (.NET 4.5.2) and I'm using the annotation [Authorize] above my controllers like shown below:</p>
  
  <pre><code>[Authorize]
  public class PhasesController : ApiController
  {
      private TestReportEntities db = new TestReportEntities();
  
      // GET: api/Phases
      public IQueryable&lt;Phase&gt; GetPhase()
      {
          return db.Phase;
      }
  }
  </code></pre>
  
  <p>I've already created my DB and I'm using the default tables that the web.api uses to manage the access, as you can see on this image:</p>
  
  <p><a href="http://i.stack.imgur.com/45Cc5.png" rel="nofollow">My tables</a></p>
  
  <p>I've already done a method to request to my web api, in another project/solution, it's working fine when I remove the annotation [Authorize] from my web api controllers.</p>
  
  <p>this is an example about how I'm requesting my api:</p>
  
  <pre><code>public int GetCurrentIdPhase(int idProject)
      {
          int phaseId = -1;
  
          WebRequest request = WebRequest.Create(string.Concat(URL, string.Format("api/phases/?idProject={0}", idProject)));
  
          using (var resp = (HttpWebResponse)request.GetResponse())
          {
              using (var reader = new StreamReader(resp.GetResponseStream()))
              {
                  string objText = reader.ReadToEnd();
                  var phase = JsonConvert.DeserializeObject&lt;List&lt;Phase&gt;&gt;(objText);
                  phaseId = phase[0].id;
              }
          }
  
          if (phaseId != -1)
          {
              return phaseId;
          }
          else
          {
              throw new Exception("Phase not found");
          }
      }
  </code></pre>
  
  <p>At the end of the day my questions are: </p>
  
  <ol>
  <li>How can I request a token to my api (POST - www.myApi/token) using the example above?</li>
  <li>How can I use the token, once I've got it, on every request to my API?</li>
  </ol>
  
  <p>if you can help me I would really appreciate it.</p>
  
  <p>Thanks.</p>