title: AngularJS + ASP.NET Web API + ASP.NET MVC Authentication
body: <p>I am new to AngularJS and trying to evaluate it for my new web application.</p>
  
  <h2>Requirement:</h2>
  
  <p>I will have one ASP.NET Web API which will be consumed from an Android, an iPhone and from a web application (ASP.NET MVC) too. ASP.NET Identity will be implemented in Web API. All three applications will call the login method of Web API to get the auth token.</p>
  
  <h2>Problem:</h2>
  
  <p>My problem is how to get ASP.NET MVC <strong>server side authentication</strong> work (in sync with Web API so I don't have to login for ASP.NET MVC separately) while Angular makes a call to get the HTML template/view, JavaScript files or other resources. I have went through many articles and blogs on AngularJS but still unable to find a security model which fits in my requirement.</p>
  
  <h2>Possible Solution:</h2>
  
  <p>Would it be a good idea to make the login call to ASP.NET MVC application instead of Web API directly, and ASP.NET MVC application would call the Web API to login, and once authenticated, save the auth token in session plus create a <strong>FormsAuthentication cookie</strong> and in cookie data save the encrypted auth token. Moreover set the <strong>auth token in ng-init</strong> somewhere in HTML to have the token in AngularJS scope. Now when AngularJS tries to make a call to ASP.NET MVC application to get HTML, then authenticate/authorize the user by matching the cookie decryted data with auth data in session. Also, AngularJS will send the <strong>auth token in header to call the Web API methods</strong> directly for all the subsequent calls for data manipulation through Web API.</p>