{"id":1304,"date":"2013-09-20T02:00:00","date_gmt":"2013-09-20T02:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/webdev\/2013\/09\/20\/understanding-security-features-in-the-spa-template-for-vs2013-rc\/"},"modified":"2022-08-09T05:22:12","modified_gmt":"2022-08-09T12:22:12","slug":"understanding-security-features-in-the-spa-template-for-vs2013-rc","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/understanding-security-features-in-the-spa-template-for-vs2013-rc\/","title":{"rendered":"Understanding Security Features in the SPA Template for VS2013 RC"},"content":{"rendered":"<p>The blog is based on <a href=\"http:\/\/blogs.msdn.com\/b\/webdev\/archive\/2013\/09\/09\/announcing-release-of-asp-net-and-web-tools-for-visual-studio-2013-rc.aspx\">Visual Studio 2013 RC release<\/a>.<\/p>\n<p>We have completely rewritten the SPA template from the previous version( MVC 4). Here are some of the changes we made:<\/p>\n<ul>\n<li>The authentication story has completely changed. Instead of using cookie (which was used in the last release), we are using OAuth with a bearer token and <a href=\"http:\/\/www.asp.net\/aspnet\/overview\/owin-and-katana\/an-overview-of-project-katana\">OWIN<\/a>, which is more correct in Web API world. <\/li>\n<li>A real single page app. We converted all the app pages into one web page and control the visibility of them by knockout. <\/li>\n<li>Account controller are now pure Web API controller <\/li>\n<li>Removed the <em>todo<\/em> samples and made the template generate boilerplate code. <\/li>\n<li>Updated to <a href=\"http:\/\/getbootstrap.com\/\">bootstrap<\/a> to modernize and simplify the CSS story.<\/li>\n<li>Updated to the new Identity API.<\/li>\n<li>We use attribute routes as default route scenario.<\/li>\n<\/ul>\n<p>Note that SPA VB template is still not available in this RC release. We will ship it with RTM release.<\/p>\n<p>In this blog, I will focus on the security features in SPA template.<\/p>\n<ul>\n<li>Bearer token authentication with Web API.<\/li>\n<li>How the bearer token is created:      \n<ul>\n<li>Password login flow <\/li>\n<li><a href=\"http:\/\/www.asp.net\/mvc\/tutorials\/mvc-5\/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on\">External\/Social login flow<\/a> (Microsoft, Facebook, Twitter and Google) <\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>Bearer Token Authentication with Web API<\/h2>\n<p>This is one of the most asked for features in Web API. In the SPA template, we implemented this feature with <a href=\"http:\/\/www.asp.net\/aspnet\/overview\/owin-and-katana\/an-overview-of-project-katana\">OWIN<\/a> security middleware. In order to use OWIN bearer token middleware, you need to make sure this package is installed:<\/p>\n<ul>\n<li>Microsoft.Owin.Security.OAuth <\/li>\n<\/ul>\n<p>Here is the code to enable Bearer Token middleware:<\/p>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:85883374-6ad1-45f5-b4e6-be3506669564\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px;padding: 0px;float: none\">\n<div style=\"border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt\">\n<div style=\"background: #000080;color: #fff;font-family: Verdana, Tahoma, Arial, sans-serif;font-weight: bold;padding: 2px 5px\">Code Snippet<\/div>\n<div style=\"background: #ddd;max-height: 500px;overflow: auto\">\n<ol start=\"1\" style=\"background: #ffffff;margin: 0 0 0 2.5em;padding: 0 0 0 5px\">\n<li><span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">static<\/span><span style=\"background:#ffffff;color:#000000\"> Startup()<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background:#ffffff;color:#000000\">{<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">OAuthOptions = <\/span><span style=\"background:#ffffff;color:#0000ff\">new<\/span><span style=\"background:#ffffff;color:#000000\"> OAuthAuthorizationServerOptions<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">{<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">TokenEndpointPath = <\/span><span style=\"background:#ffffff;color:#a31515\">&quot;\/Token&quot;<\/span><span style=\"background:#ffffff;color:#000000\">,<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">AuthorizeEndpointPath = <\/span><span style=\"background:#ffffff;color:#a31515\">&quot;\/api\/Account\/ExternalLogin&quot;<\/span><span style=\"background:#ffffff;color:#000000\">,<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">Provider = <\/span><span style=\"background:#ffffff;color:#0000ff\">new<\/span><span style=\"background:#ffffff;color:#000000\"> ApplicationOAuthProvider(PublicClientId, IdentityManagerFactory, CookieOptions)<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">};<\/span><\/li>\n<li><span style=\"background:#ffffff;color:#000000\">}<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;<\/li>\n<li><span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#008000\">\/\/ For more information on configuring authentication, please visit http:\/\/go.microsoft.com\/fwlink\/?LinkId=301864<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">public<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#0000ff\">void<\/span><span style=\"background:#ffffff;color:#000000\"> ConfigureAuth(IAppBuilder app)<\/span><\/li>\n<li><span style=\"background:#ffffff;color:#000000\">{<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#008000\">\/\/ Enable the application to use bearer tokens to authenticate users<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">app.UseOAuthBearerTokens(OAuthOptions, ExternalOAuthAuthenticationType);<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background:#ffffff;color:#000000\">}<\/span><\/li>\n<\/ol><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<p>The <em>UseOAuthBearerTokens<\/em> extension method actually enables 3 OWIN middleware components:<\/p>\n<ol>\n<li>Authorization Server middleware. <\/li>\n<li>Application bearer token middleware.<\/li>\n<li>External bearer token middleware.<\/li>\n<\/ol>\n<p>Here is the pseudo-code for the extension method:<\/p>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:49765701-763f-4d48-8f3c-ab26206e74cc\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px;padding: 0px;float: none\">\n<div style=\"border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt\">\n<div style=\"background: #000080;color: #fff;font-family: Verdana, Tahoma, Arial, sans-serif;font-weight: bold;padding: 2px 5px\">Code Snippet<\/div>\n<div style=\"background: #ddd;max-height: 300px;overflow: auto\">\n<ol start=\"1\" style=\"background: #ffffff;margin: 0 0 0 2.5em;padding: 0 0 0 5px\">\n<li><span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">public<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#0000ff\">static<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#0000ff\">void<\/span><span style=\"background:#ffffff;color:#000000\"> UseOAuthBearerTokens(<\/span><span style=\"background:#ffffff;color:#0000ff\">this<\/span><span style=\"background:#ffffff;color:#000000\"> IAppBuilder app, OAuthAuthorizationServerOptions options, <\/span><span style=\"background:#ffffff;color:#0000ff\">string<\/span><span style=\"background:#ffffff;color:#000000\"> externalAuthenticationType)<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background:#ffffff;color:#000000\">{<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">app.UseOAuthAuthorizationServer(options);<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">app.UseOAuthBearerAuthentication(<\/span><span style=\"background:#ffffff;color:#0000ff\">new<\/span><span style=\"background:#ffffff;color:#000000\"> OAuthBearerAuthenticationOptions<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">{<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">AccessTokenFormat = options.AccessTokenFormat,<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">AccessTokenProvider = options.AccessTokenProvider,<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">AuthenticationMode = options.AuthenticationMode,<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">AuthenticationType = options.AuthenticationType,<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">Description = options.Description,<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">Provider = <\/span><span style=\"background:#ffffff;color:#0000ff\">new<\/span><span style=\"background:#ffffff;color:#000000\"> AppBuilderExtensions.ApplicationOAuthBearerProvider(),<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">SystemClock = options.SystemClock<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">});<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">app.UseOAuthBearerAuthentication(<\/span><span style=\"background:#ffffff;color:#0000ff\">new<\/span><span style=\"background:#ffffff;color:#000000\"> OAuthBearerAuthenticationOptions<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">{<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">AccessTokenFormat = options.AccessTokenFormat,<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">AccessTokenProvider = options.AccessTokenProvider,<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">AuthenticationMode = AuthenticationMode.Passive,<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">AuthenticationType = externalAuthenticationType,<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">Description = options.Description,<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">Provider = <\/span><span style=\"background:#ffffff;color:#0000ff\">new<\/span><span style=\"background:#ffffff;color:#000000\"> AppBuilderExtensions.ExternalOAuthBearerProvider(),<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">SystemClock = options.SystemClock<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">});<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background:#ffffff;color:#000000\">}<\/span><\/li>\n<\/ol><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<p>&#160;&#160;&#160; <\/p>\n<p>Authorization server will be responsible to create the bearer token and external bearer token is only used with external logins (Such as Facebook, Google, etc). I will explain them later in the subsequent sections.<\/p>\n<p>After enabling bearer token authentication in active mode, OWIN middleware will authenticate every request with the \u201cAuthorization: Bearer\u201d header.&#160; By default, the application bearer token middleware is active.<\/p>\n<p>However, there is still a problem with Web API. What happens if your application enables other authentications?&#160; For example, the SPA template enables application cookie middleware as active mode as well in order to enable other scenarios like MVC authentication. So Web API will still be authenticated if the request has session cookie but without a bearer token. That\u2019s probably not what you want as you would be venerable to CSRF attacks for your APIs. Another negative impact is that if request is unauthorized, both middleware components will apply challenges to it. The cookie middleware will alter the 401 response to a 302 to redirect to the login page. That is also not what you want in a Web API request.<\/p>\n<p>The SPA template demonstrates how to solve this problem in Web API layer. Here is the code snippet from WebApiConfig.Register method:<\/p>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:89dd8f73-0b7d-4f40-b6a3-2ca42dfd5e3c\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px;padding: 0px;float: none\">\n<div style=\"border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt\">\n<div style=\"background: #000080;color: #fff;font-family: Verdana, Tahoma, Arial, sans-serif;font-weight: bold;padding: 2px 5px\">Code Snippet<\/div>\n<div style=\"background: #ddd;max-height: 300px;overflow: auto\">\n<ol start=\"1\" style=\"background: #ffffff;margin: 0 0 0 2em;padding: 0 0 0 5px\">\n<li><span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#008000\">\/\/ Web API configuration and services<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#008000\">\/\/ Configure Web API to use only bearer token authentication.<\/span><\/li>\n<li><span style=\"background:#ffffff;color:#000000\">config.SuppressDefaultHostAuthentication();<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background:#ffffff;color:#000000\">config.Filters.Add(<\/span><span style=\"background:#ffffff;color:#0000ff\">new<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#2b91af\">HostAuthenticationFilter<\/span><span style=\"background:#ffffff;color:#000000\">(<\/span><span style=\"background:#ffffff;color:#2b91af\">Startup<\/span><span style=\"background:#ffffff;color:#000000\">.OAuthOptions.AuthenticationType));<\/span><\/li>\n<\/ol><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<p>&#160;<\/p>\n<p><strong>SuppressDefaultHostAuthentication<\/strong> will register a message handler and set current principal to anonymous, so no host principal will get passed to Web API. It will also suppress default challenges from OWIN middleware.<\/p>\n<p><strong>HostAuthenticationFilter<\/strong> behavior is the opposite. It will set the principal from specified OWIN authentication middleware. In this case, it is the bearer token middleware, it will also send a challenge to specified middleware when it sees a 401 response. Since this authentication filter is set as global filter, it will apply to all Web APIs. So the result is that Web API will only see the authentication principal from the bearer token middleware and any 401 response from Web API will add a bearer challenge.<\/p>\n<p><em><strong>Note: the authentication filter is a new filter type in Web API v2. It happens after the message handler but before the AuthorizationAttribute filter. It makes it possible to let you specify different authentication methods at the action level.<\/strong><\/em><\/p>\n<p>After configuring the OWIN bearer token middleware and Web API host authentication settings, you can easily protect your Web API resources with the AuthorizeAttribute.<\/p>\n<p>Your next question is probably how the bearer token is created in the SPA template. In order to conform with OAuth 2.0 spec, we try to use the OWIN Authorization server to create and send bearer tokens to the client in every scenario.&#160; We plan to have a blog post for OWIN Authorization server to explain how it supports all OAuth 2.0 flows. Here I will only explain the authentication flows that are used in SPA template: <a href=\"http:\/\/tools.ietf.org\/html\/rfc6749#section-4.3\"><strong>Resource Owner Password Credentials Grant<\/strong><\/a><strong> and <\/strong><a href=\"http:\/\/tools.ietf.org\/html\/rfc6749#section-4.2\"><strong>Implicit Grant<\/strong><\/a><strong>.<\/strong><\/p>\n<h2>Password Login Flow<\/h2>\n<p>This flow happens when the user logs in by typing her user name and password in login form ( This is the most common authentication scenario). <\/p>\n<p>In the SPA template, we use <a href=\"http:\/\/tools.ietf.org\/html\/rfc6749#section-4.3\">OAuth 2.0\u2019s Resource Owner Password Credentials Grant<\/a> flow for this scenario and implement it in the OWIN Authorization server. <\/p>\n<p>In this flow, the browser sends a POST request with grant type, user name and password and server returns back an access token. For example, the browser sends:<\/p>\n<blockquote>\n<p>POST <a href=\"http:\/\/localhost:47948\/Token\">http:\/\/localhost:47948\/Token<\/a> HTTP\/1.1       <br \/>Content-Type: application\/x-www-form-urlencoded; charset=UTF-8       <br \/>Content-Length: 46       <br \/>Host: localhost:47948<\/p>\n<p>grant_type=password&amp;username=a&amp;password=aaaaaa<\/p>\n<\/blockquote>\n<p>The server responds:<\/p>\n<blockquote>\n<p>HTTP\/1.1 200 OK      <br \/>Content-Length: 671       <br \/>Content-Type: application\/json;charset=UTF-8       <br \/>Set-Cookie: .AspNet.Cookies=Ud0iQNZazLq-K8C; path=\/; HttpOnly <\/p>\n<p>{      <br \/>&quot;access_token&quot;:&quot;YPo047a0sqJUmle6tkeKmIaRUS&quot;,       <br \/>&quot;token_type&quot;:&quot;bearer&quot;,       <br \/>&quot;expires_in&quot;:1200,       <br \/>&quot;userName&quot;:&quot;a&quot;,       <br \/>&quot;.issued&quot;:&quot;Thu, 19 Sep 2013 05:56:32 GMT&quot;,       <br \/>&quot;.expires&quot;:&quot;Thu, 19 Sep 2013 06:16:32 GMT&quot;       <br \/>}<\/p>\n<\/blockquote>\n<p>In order to support the above password login flow, the&#160; SPA template calls <strong>ApplicationOAuthProvider<\/strong> and exposes the token endpoint as \u201c\/Token\u201d. (See OAuthAuthorizationServerOptions in the <em>Startup.Auth.cs<\/em> file). <\/p>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:eb0355b2-e170-49f9-8ce4-c87b49324edf\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px;padding: 0px;float: none\">\n<div style=\"border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt\">\n<div style=\"background: #000080;color: #fff;font-family: Verdana, Tahoma, Arial, sans-serif;font-weight: bold;padding: 2px 5px\">Code Snippet<\/div>\n<div style=\"background: #ddd;max-height: 400px;overflow: auto\">\n<ol start=\"1\" style=\"background: #ffffff;margin: 0 0 0 2.5em;padding: 0 0 0 5px\">\n<li><span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">public<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#0000ff\">override<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#0000ff\">async<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#2b91af\">Task<\/span><span style=\"background:#ffffff;color:#000000\"> GrantResourceOwnerCredentials(<\/span><span style=\"background:#ffffff;color:#2b91af\">OAuthGrantResourceOwnerCredentialsContext<\/span><span style=\"background:#ffffff;color:#000000\"> context)<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background:#ffffff;color:#000000\">{<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">using<\/span><span style=\"background:#ffffff;color:#000000\"> (<\/span><span style=\"background:#ffffff;color:#2b91af\">IdentityManager<\/span><span style=\"background:#ffffff;color:#000000\"> identityManager = _identityManagerFactory.CreateStoreManager())<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">{<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">if<\/span><span style=\"background:#ffffff;color:#000000\"> (!<\/span><span style=\"background:#ffffff;color:#0000ff\">await<\/span><span style=\"background:#ffffff;color:#000000\"> identityManager.Passwords.CheckPasswordAsync(context.UserName, context.Password))<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">{<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">context.SetError(<\/span><span style=\"background:#ffffff;color:#a31515\">&quot;invalid_grant&quot;<\/span><span style=\"background:#ffffff;color:#000000\">, <\/span><span style=\"background:#ffffff;color:#a31515\">&quot;The user name or password is incorrect.&quot;<\/span><span style=\"background:#ffffff;color:#000000\">);<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">return<\/span><span style=\"background:#ffffff;color:#000000\">;<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">}<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">string<\/span><span style=\"background:#ffffff;color:#000000\"> userId = <\/span><span style=\"background:#ffffff;color:#0000ff\">await<\/span><span style=\"background:#ffffff;color:#000000\"> identityManager.Logins.GetUserIdForLocalLoginAsync(context.UserName);<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#2b91af\">IEnumerable<\/span><span style=\"background:#ffffff;color:#000000\">&lt;<\/span><span style=\"background:#ffffff;color:#2b91af\">Claim<\/span><span style=\"background:#ffffff;color:#000000\">&gt; claims = <\/span><span style=\"background:#ffffff;color:#0000ff\">await<\/span><span style=\"background:#ffffff;color:#000000\"> GetClaimsAsync(identityManager, userId);<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#2b91af\">ClaimsIdentity<\/span><span style=\"background:#ffffff;color:#000000\"> oAuthIdentity = CreateIdentity(identityManager, claims,<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">context.Options.AuthenticationType);<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#2b91af\">ClaimsIdentity<\/span><span style=\"background:#ffffff;color:#000000\"> cookiesIdentity = CreateIdentity(identityManager, claims,<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">_cookieOptions.AuthenticationType);<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#2b91af\">AuthenticationProperties<\/span><span style=\"background:#ffffff;color:#000000\"> properties = <\/span><span style=\"background:#ffffff;color:#0000ff\">await<\/span><span style=\"background:#ffffff;color:#000000\"> CreatePropertiesAsync(identityManager, userId);<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#2b91af\">AuthenticationTicket<\/span><span style=\"background:#ffffff;color:#000000\"> ticket = <\/span><span style=\"background:#ffffff;color:#0000ff\">new<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#2b91af\">AuthenticationTicket<\/span><span style=\"background:#ffffff;color:#000000\">(oAuthIdentity, properties);<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">context.Validated(ticket);<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">context.Request.Context.Authentication.SignIn(cookiesIdentity);<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">}<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background:#ffffff;color:#000000\">}<\/span><\/li>\n<\/ol><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<p>What the code above does is quite straightforward:<\/p>\n<ol>\n<li>Checks the password with the Identity API .<\/li>\n<li>Create a user identity for the bearer token.<\/li>\n<li>Creates a user identity for the cookie.<\/li>\n<li>Calls the context.Validated(ticket) to tell the OAuthZ server to protect the ticket as an access token and send it out in JSON payload. <\/li>\n<li>Signs the cookie identity so it can send the authentication cookie.<\/li>\n<\/ol>\n<p>&#160;<\/p>\n<h2>External login flow (Microsoft, Facebook, Twitter and Google)<\/h2>\n<p>This flow is a little bit complicated compared with password flow.&#160; The SPA template uses OAuth 2.0 implicit flow to convert an external sign in cookie to an access token and send it back to the browser by URL fragment. Here is a simplified flow diagram for a Facebook login:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2013\/09\/1651.image_thumb_5AC83C99.png\"><img decoding=\"async\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2013\/09\/1651.image_thumb_5AC83C99.png\" width=\"694\" height=\"533\" \/><\/a><\/p>\n<ol>\n<li>The User clicks the Facebook Login button and it will trigger a browser redirect to the authorization endpoint with a parameter response_type as token and redirect_uri as application URL. For example: <a title=\"http:\/\/localhost:47948\/api\/Account\/ExternalLogin?provider=Facebook&amp;response_type=token&amp;client_id=self&amp;redirect_uri=%2F&amp;state=E2VRvHyopw%2BdYR5uKHbHAyYFmK06Pd%2Fw5LRWO243Tdk%3D\" href=\"http:\/\/localhost:47948\/api\/Account\/ExternalLogin?provider=Facebook&amp;response_type=token&amp;client_id=self&amp;redirect_uri=%2F&amp;state=E2VRvHyopw%2BdYR5uKHbHAyYFmK06Pd%2Fw5LRWO243Tdk%3D\">http:\/\/localhost:47948\/api\/Account\/ExternalLogin?provider=Facebook&amp;response_type=token&amp;client_id=self&amp;redirect_uri=%2F&amp;state=E2VRvHyopw%2BdYR5uKHbHAyYFmK06Pd%2Fw5LRWO243Tdk%3D<\/a> <\/li>\n<li>The application knows that user wants to login as Facebook so it sends a challenge to the Facebook authentication middleware to modify the response as 302 and redirect browser to facebook.com. To enable an external login service, see <a href=\"http:\/\/www.asp.net\/mvc\/tutorials\/mvc-5\/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on\">Create an ASP.NET MVC 5 App with Facebook and Google OAuth2 and OpenID Sign-on<\/a> or&#160; <a href=\"http:\/\/www.asp.net\/web-api\/overview\/security\/external-authentication-services\">External Authentication Service<\/a>. <\/li>\n<li>The user signs in with her facebook account and which grants access to this application.<\/li>\n<li>Facebook sends a HTTP 302 back and redirects the browser back to Facebook callback URL on server, by default it\u2019s \u201c\/signin-facebook\u201d. You can change it with Facebook authentication options. <\/li>\n<li>The Facebook authentication middleware asks for access token and user data from facebook.com. <\/li>\n<li>The Facebook authentication middleware redirects the browser back to the authorization endpoint and converts the facebook data into claims and sets external sign in cookie. <\/li>\n<li>The user agents redirects to the authorization endpoint with the external cookie in the request. <\/li>\n<li>The authorization endpoint checks the external sign in cookie principal and finds the associated application user, then signs in the user as Bearer authentication type into the authorization server middleware. Since the authorization server sees that the request parameter response_type is token (in step 1), it will trigger implicit flow, which will create access the token and append it to the redirect_uri (step 1) as URL fragment. For example:      \n<blockquote>\n<p>HTTP\/1.1 302 Found          <br \/>Cache-Control: no-cache           <br \/>Pragma: no-cache           <br \/>Expires: -1           <br \/>Location: <strong>\/#access_token=asd2342SDIUKJdsfjk3234&amp;token_type=bearer&amp;expires_in=1200&amp;state=06hwltIjvnTn44hc<\/strong>           <br \/>Set-Cookie: .AspNet.External=; path=\/; expires=Thu, 01-Jan-1970 00:00:00 GMT           <br \/>Set-Cookie: .AspNet.Cookies=WJgdyZQs9N8TG20EWnik-j0_; path=\/; HttpOnly           <br \/>Content-Length: 0<\/p>\n<\/blockquote>\n<\/li>\n<\/ol>\n<p>&#160;<\/p>\n<p>The key logic here is in the authorization endpoint, which is \u201capi\/Account\/ExternalLogin\u201d in the SPA template (See <strong>OAuthAuthorizationServerOptions<\/strong> in the <em>Startup.Auth.cs<\/em> file). <\/p>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e6f60889-3b2e-48f5-b61b-9738b8ee923e\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px;padding: 0px;float: none\">\n<div style=\"border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt\">\n<div style=\"background: #000080;color: #fff;font-family: Verdana, Tahoma, Arial, sans-serif;font-weight: bold;padding: 2px 5px\">Code Snippet<\/div>\n<div style=\"background: #ddd;max-height: 300px;overflow: auto\">\n<ol start=\"1\" style=\"background: #ffffff;margin: 0 0 0 2.5em;padding: 0 0 0 5px\">\n<li><span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#008000\">\/\/ GET api\/Account\/ExternalLogin<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background:#ffffff;color:#000000\">[<\/span><span style=\"background:#ffffff;color:#2b91af\">OverrideAuthentication<\/span><span style=\"background:#ffffff;color:#000000\">]<\/span><\/li>\n<li><span style=\"background:#ffffff;color:#000000\">[<\/span><span style=\"background:#ffffff;color:#2b91af\">HostAuthentication<\/span><span style=\"background:#ffffff;color:#000000\">(<\/span><span style=\"background:#ffffff;color:#2b91af\">Startup<\/span><span style=\"background:#ffffff;color:#000000\">.ExternalCookieAuthenticationType)]<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background:#ffffff;color:#000000\">[<\/span><span style=\"background:#ffffff;color:#2b91af\">AllowAnonymous<\/span><span style=\"background:#ffffff;color:#000000\">]<\/span><\/li>\n<li><span style=\"background:#ffffff;color:#000000\">[<\/span><span style=\"background:#ffffff;color:#2b91af\">HttpGet<\/span><span style=\"background:#ffffff;color:#000000\">(<\/span><span style=\"background:#ffffff;color:#a31515\">&quot;ExternalLogin&quot;<\/span><span style=\"background:#ffffff;color:#000000\">, RouteName = <\/span><span style=\"background:#ffffff;color:#a31515\">&quot;ExternalLogin&quot;<\/span><span style=\"background:#ffffff;color:#000000\">)]<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">public<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#0000ff\">async<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#2b91af\">Task<\/span><span style=\"background:#ffffff;color:#000000\">&lt;<\/span><span style=\"background:#ffffff;color:#2b91af\">IHttpActionResult<\/span><span style=\"background:#ffffff;color:#000000\">&gt; ExternalLogin(<\/span><span style=\"background:#ffffff;color:#0000ff\">string<\/span><span style=\"background:#ffffff;color:#000000\"> provider)<\/span><\/li>\n<li><span style=\"background:#ffffff;color:#000000\">{<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">if<\/span><span style=\"background:#ffffff;color:#000000\"> (!User.Identity.IsAuthenticated)<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">{<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">return<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#0000ff\">new<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#2b91af\">ChallengeResult<\/span><span style=\"background:#ffffff;color:#000000\">(provider, <\/span><span style=\"background:#ffffff;color:#0000ff\">this<\/span><span style=\"background:#ffffff;color:#000000\">);<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">}<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#2b91af\">ExternalLoginData<\/span><span style=\"background:#ffffff;color:#000000\"> externalLogin = <\/span><span style=\"background:#ffffff;color:#2b91af\">ExternalLoginData<\/span><span style=\"background:#ffffff;color:#000000\">.FromIdentity(User.Identity <\/span><span style=\"background:#ffffff;color:#0000ff\">as<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#2b91af\">ClaimsIdentity<\/span><span style=\"background:#ffffff;color:#000000\">);<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">if<\/span><span style=\"background:#ffffff;color:#000000\"> (externalLogin == <\/span><span style=\"background:#ffffff;color:#0000ff\">null<\/span><span style=\"background:#ffffff;color:#000000\">)<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">{<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">return<\/span><span style=\"background:#ffffff;color:#000000\"> InternalServerError();<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">}<\/span><\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">string<\/span><span style=\"background:#ffffff;color:#000000\"> userId = <\/span><span style=\"background:#ffffff;color:#0000ff\">await<\/span><span style=\"background:#ffffff;color:#000000\"> IdentityManager.Logins.GetUserIdForLoginAsync(externalLogin.LoginProvider,<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">externalLogin.ProviderKey);<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">bool<\/span><span style=\"background:#ffffff;color:#000000\"> hasRegistered = userId != <\/span><span style=\"background:#ffffff;color:#0000ff\">null<\/span><span style=\"background:#ffffff;color:#000000\">;<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">if<\/span><span style=\"background:#ffffff;color:#000000\"> (hasRegistered)<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">{<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">Authentication.SignOut(<\/span><span style=\"background:#ffffff;color:#2b91af\">Startup<\/span><span style=\"background:#ffffff;color:#000000\">.ExternalCookieAuthenticationType);<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#2b91af\">IEnumerable<\/span><span style=\"background:#ffffff;color:#000000\">&lt;<\/span><span style=\"background:#ffffff;color:#2b91af\">Claim<\/span><span style=\"background:#ffffff;color:#000000\">&gt; claims = <\/span><span style=\"background:#ffffff;color:#0000ff\">await<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#2b91af\">ApplicationOAuthProvider<\/span><span style=\"background:#ffffff;color:#000000\">.GetClaimsAsync(IdentityManager, userId);<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#2b91af\">ClaimsIdentity<\/span><span style=\"background:#ffffff;color:#000000\"> oAuthIdentity = <\/span><span style=\"background:#ffffff;color:#2b91af\">ApplicationOAuthProvider<\/span><span style=\"background:#ffffff;color:#000000\">.CreateIdentity(IdentityManager, claims,<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">OAuthOptions.AuthenticationType);<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#2b91af\">ClaimsIdentity<\/span><span style=\"background:#ffffff;color:#000000\"> cookieIdentity = <\/span><span style=\"background:#ffffff;color:#2b91af\">ApplicationOAuthProvider<\/span><span style=\"background:#ffffff;color:#000000\">.CreateIdentity(IdentityManager, claims,<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">CookieOptions.AuthenticationType);<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#2b91af\">AuthenticationProperties<\/span><span style=\"background:#ffffff;color:#000000\"> properties = <\/span><span style=\"background:#ffffff;color:#0000ff\">await<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#2b91af\">ApplicationOAuthProvider<\/span><span style=\"background:#ffffff;color:#000000\">.CreatePropertiesAsync(<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">IdentityManager, userId);<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">Authentication.SignIn(properties, oAuthIdentity, cookieIdentity);<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">}<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">else<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">{<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#2b91af\">IEnumerable<\/span><span style=\"background:#ffffff;color:#000000\">&lt;<\/span><span style=\"background:#ffffff;color:#2b91af\">Claim<\/span><span style=\"background:#ffffff;color:#000000\">&gt; claims = externalLogin.GetClaims();<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#2b91af\">ClaimsIdentity<\/span><span style=\"background:#ffffff;color:#000000\"> identity = <\/span><span style=\"background:#ffffff;color:#2b91af\">ApplicationOAuthProvider<\/span><span style=\"background:#ffffff;color:#000000\">.CreateIdentity(IdentityManager, claims,<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">OAuthOptions.AuthenticationType);<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">Authentication.SignIn(identity);<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">}<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">return<\/span><span style=\"background:#ffffff;color:#000000\"> Ok();<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background:#ffffff;color:#000000\">}<\/span><\/li>\n<\/ol><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<p>The <strong>OverrideAuthentication<\/strong> attribute is used to suppress global authentication filters. It suppresses the application bearer token host authentication filter in the SPA template.<\/p>\n<p>This action enables <strong>ExternalCookieAuthenticationType<\/strong> host authentication, which represents the user\u2019s external sign in state. With this setting, the User.Identity will be set as the external login identity, for example, Facebook identity.<\/p>\n<p><strong>AllowAnonymous<\/strong> enables a user to reach this endpoint without an external sign in state. It will trigger an external sign in challenge when the user is anonymous. That\u2019s the scenario when the unauthorized user clicks the Facebook button to trigger a redirection to facebook.com.<\/p>\n<p>After the browser redirects back from facebook.com and gets the external sign in cookie from the Facebook authentication middleware, this action will check if the external login data has already been associated with existing user. <\/p>\n<p>If it has, it will sign in with both the application bearer token identity and the application cookie identity. As described in the step 8 of external login flow above, it will trigger a redirection and add an access token in URL fragment.<\/p>\n<p>If not, it will sign in with the external bearer token identity and it will also be sent to the client by implicit flow. The client code will check if the user is registered by the code and show up the register external user page as needed. After the user is registered, the client code will trigger the external login flow again to get the application bearer token.<\/p>\n<h2>Acknowledgements<\/h2>\n<p>I\u2019d like to thank Rick Anderson: (twitter <a href=\"https:\/\/twitter.com\/RickAndMSFT\">@RickAndMSFT<\/a> ) for the help on the blog. Rick&#160; is a senior programming writer for Microsoft focusing on Azure and MVC.<\/p>\n<p>&#160;<\/p>\n<p>Thanks,<\/p>\n<p>Hongye Sun (ASP.NET MVC and Web API team)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The blog is based on Visual Studio 2013 RC release. We have completely rewritten the SPA template from the previous version( MVC 4). Here are some of the changes we made: The authentication story has completely changed. Instead of using cookie (which was used in the last release), we are using OAuth with a bearer [&hellip;]<\/p>\n","protected":false},"author":438,"featured_media":58792,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[197],"tags":[34,7446,7439,7425],"class_list":["post-1304","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aspnet","tag-asp-net-web-api","tag-katana","tag-mvc-5","tag-spa"],"acf":[],"blog_post_summary":"<p>The blog is based on Visual Studio 2013 RC release. We have completely rewritten the SPA template from the previous version( MVC 4). Here are some of the changes we made: The authentication story has completely changed. Instead of using cookie (which was used in the last release), we are using OAuth with a bearer [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/1304","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/438"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=1304"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/1304\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/58792"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=1304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=1304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=1304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}