{"id":570,"date":"2016-09-23T13:08:32","date_gmt":"2016-09-23T11:08:32","guid":{"rendered":"https:\/\/zaven.co\/blog\/?p=570"},"modified":"2025-04-08T19:55:19","modified_gmt":"2025-04-08T17:55:19","slug":"user-authentication-asp-net-web-api-2-rsa-jwt-tokens-part-4","status":"publish","type":"post","link":"https:\/\/zaven.co\/blog\/user-authentication-asp-net-web-api-2-rsa-jwt-tokens-part-4\/","title":{"rendered":"User Authentication in ASP.NET WEB API 2 with RSA-signed JWT Tokens (part 4)"},"content":{"rendered":"<p>After creating, signing and verifying the <a href=\"https:\/\/zaven.co\/blog\/user-authentication-asp-net-web-api-jwt-tokens\/\">JWT Token<\/a>, we can move on to <strong>programming the login controller and testing our application<\/strong>.<!--more--><\/p>\n<h2>Login controller: MembershipController<\/h2>\n<p><strong>In this part we\u2019ll create the <code>MembershipController <\/code>with one <code>GET<\/code> method<\/strong>, which will return a JWT token as a compact string after receiving user\u2019s login data.<\/p>\n<p>Our endpoint URL is:<\/p>\n<pre><code class=\"language-http\" style=\"white-space: pre-wrap; word-wrap: break-word;\">\n\/api\/Membership\/Authenticate?username=admin93&amp;password=password<\/code><\/pre>\n<pre><code class=\"language-csharp\">\nusing System;\nusing System.Web.Http;\nusing System.Threading.Tasks;\nusing Zaven.Practices.Auth.JWT.Services.Interfaces;\n...\npublic class MembershipController : ApiController\n    {\n        private readonly IAuthService _authService;\n        \n        public MembershipController(IAuthService authService)\n        {\n            _authService = authService;\n        }\n\n        \/\/ GET: Membership\n        [HttpGet]\n        public async Task Authenticate(String username, String password)\n        {\n            string Token = await _authService.GenerateJwtTokenAsync(username, password);\n            return Token;\n        }\n    }\n<\/code><\/pre>\n<p>After receiving the token our web app should store it in browser data and place it in the header of every request.<\/p>\n<p><code>Authorization: Bearer<\/code><\/p>\n<h2>Exemplary controller: ValuesController<\/h2>\n<p>To check if the authentication process works, let\u2019s create a simple <code>ApiController ValuesController<\/code>, which methods were applied with filter as a <code>TokenAuthenticate<\/code> attribute.<\/p>\n<pre><code class=\"language-csharp\">\nusing JWTAuth.Filters;\nusing System.Collections.Generic;\nusing System.Web.Http;\n...\n[TokenAuthenticate]\n    public class ValuesController : ApiController\n    {\n\t...\n        \/\/ GET api\/values\/5 \n        public string Get(int id)\n        {\n            return \"You have access to this data:\\n- Very important data number 5\";\n        }\n\t... \n    }\n<\/code><\/pre>\n<h2>TokenAuthentication filter<\/h2>\n<p>Finally we have to implement the <code>TokenAuthenticate<\/code> filter.<\/p>\n<pre><code class=\"language-csharp\">\nusing System;\nusing System.Net.Http;\nusing System.Net.Http.Headers;\nusing System.Threading;\nusing System.Threading.Tasks;\nusing System.Web.Http.Filters;\nusing Zaven.Practices.Auth.JWT.Services.Interfaces;\n...\npublic class TokenAuthenticate : Attribute, IAuthenticationFilter\n    {\n       \n        public bool AllowMultiple\n        {\n            get\n            {\n                return false;\n            }\n        }\n\n        public async Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)\n        {\n            try\n            {\n                IAuthService _authService = context.ActionContext.ControllerContext.Configuration\n                                  .DependencyResolver.GetService(typeof(IAuthService)) as IAuthService;\n\n                HttpRequestMessage request = context.Request;\n                AuthenticationHeaderValue authorization = request.Headers.Authorization;\n                \n                if (authorization == null)\n                {\n                    context.ErrorResult = new AuthenticationFailureResult(\"Missing autorization header\", request);\n                    return;\n                }\n                if (authorization.Scheme != \"Bearer\")\n                {\n                    context.ErrorResult = new AuthenticationFailureResult(\"Invalid autorization scheme\", request);\n                    return;\n                }                   \n                if (String.IsNullOrEmpty(authorization.Parameter))\n                {\n                    context.ErrorResult = new AuthenticationFailureResult(\"Missing Token\", request);\n                    return;\n                }\n\n                Boolean correctToken = await _authService.ValidateTokenAsync(authorization.Parameter);\n                if(!correctToken)\n                    context.ErrorResult = new AuthenticationFailureResult(\"Invalid Token\", request);                \n            }\n            catch (Exception ex)\n            {\n                context.ErrorResult = new AuthenticationFailureResult(\"Exception: \\n\" + ex.Message, context.Request);\n            }            \n        }\n...\n    }\n<\/code><\/pre>\n<p>The most interesting is the <code>AuthenticateAsync<\/code> which, in this case, is responsible for reading the token from the HTTP request header and validating it (calling the <code>ValidateTokenAsync()<\/code> from <code>AuthService<\/code>). Depending on the result, the are three outcomes: a positive authentication, moving on to controllers method or sending the response with a 401 code.<\/p>\n<h2>401 response: AuthenticationFailureResult<\/h2>\n<p>In case the HTTP request header lacks the <code>\u201cAuthorization: Bearer\u201d<\/code> part (or basically any other kind of error), <code>context.ErrorResult<\/code> will filter it out and set the <code>IHttpActionResult<\/code>. This part is responsible for providing error response codes (e.g. 401 if unauthorized) as a type of <code>AuthenticationFailureResult<\/code>. Below, you can see the code in action.<\/p>\n<pre><code class=\"language-csharp\">\nusing System.Net;\nusing System.Net.Http;\nusing System.Threading;\nusing System.Threading.Tasks;\nusing System.Web.Http;\n...\npublic class AuthenticationFailureResult : IHttpActionResult\n    {\n        public string ReasonPhrase { get; private set; }\n        public HttpRequestMessage Request { get; private set; }\n\n        public AuthenticationFailureResult(string reasonPhrase, HttpRequestMessage request)\n        {\n            ReasonPhrase = reasonPhrase;\n            Request = request;\n        }\n\n        public Task ExecuteAsync(CancellationToken cancellationToken)\n        {\n            return Task.FromResult(Execute());\n        }\n\n        private HttpResponseMessage Execute()\n        {\n            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.Unauthorized);\n            response.RequestMessage = Request;\n            response.ReasonPhrase = ReasonPhrase;\n            return response;\n        }\n    }\n<\/code><\/pre>\n<h3>Application test<\/h3>\n<p>When running our app in the console, you should see the initial request being sent. In response you should get a JWT token, which is then added to the HTTP headers of any further requests. If the token is accepted, we get access to our protected data.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-574\" src=\"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2016\/09\/6-730x220.png\" alt=\"login controller\"   srcset=\"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2016\/09\/6-730x220.png 730w, https:\/\/zaven.co\/blog\/wp-content\/uploads\/2016\/09\/6.png 961w\" sizes=\"auto, (max-width: 730px) 100vw, 730px\" \/><\/p>\n<h3>Summary<\/h3>\n<p><strong>JWT token is a quick and easy way to authenticate access to server resources.<\/strong> The transmitted data can be signed and thanks to that they\u2019re safe and resistant to man-in-the-middle attacks. RSA signng is fast, it doesn\u2019t slow down your application and the token itself doesn\u2019t have to be stored in the database.<\/p>\n<p>Check out other <a href=\"https:\/\/zaven.co\/blog\/category\/tutorials\/\" target=\"_blank\" rel=\"noopener noreferrer\">tutorials<\/a>&nbsp;on our blog.<\/p>\n<p><span style=\"color: #808080;\">Sources: <\/span><\/p>\n<ul>\n<li><a href=\"https:\/\/jwt.io\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Official JWT website<\/a><\/li>\n<li><a href=\"http:\/\/dotnetcodes.com\/\" target=\"_blank\" rel=\"nofollow oopener noopener noreferrer\">.NET Codes website<\/a><\/li>\n<li><a href=\"https:\/\/msdn.microsoft.com\/pl-pl\/default.aspx\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Microsoft Developer Network<\/a><\/li>\n<li><a href=\"http:\/\/www.asp.net\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">The ASP.NET Site<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>After creating, signing and verifying the JWT Token, we can move on to programming the login controller and testing our application.<\/p>\n","protected":false},"author":12,"featured_media":585,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[56,57,5],"tags":[48,47,40,8,49,41],"class_list":["post-570","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android-development","category-ios-development","category-tutorials","tag-json-tokens","tag-jwt","tag-mobile-app","tag-tutorial","tag-user-authentication","tag-web-app"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.8.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Web Api 2 User Authentication. Login controller | Zaven Blog<\/title>\n<meta name=\"description\" content=\"After creating, signing and verifying the JWT Token, we can move on to programming the login controller and testing our application. Read more on our blog!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/zaven.co\/blog\/user-authentication-asp-net-web-api-2-rsa-jwt-tokens-part-4\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Web Api 2 User Authentication. Login controller | Zaven Blog\" \/>\n<meta property=\"og:description\" content=\"After creating, signing and verifying the JWT Token, we can move on to programming the login controller and testing our application. Read more on our blog!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/zaven.co\/blog\/user-authentication-asp-net-web-api-2-rsa-jwt-tokens-part-4\/\" \/>\n<meta property=\"og:site_name\" content=\"Zaven Blog\" \/>\n<meta property=\"article:published_time\" content=\"2016-09-23T11:08:32+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-04-08T17:55:19+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2016\/09\/klucze_3-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1500\" \/>\n\t<meta property=\"og:image:height\" content=\"679\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Micha\u0142 Zawadzki\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Micha\u0142 Zawadzki\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/zaven.co\/blog\/user-authentication-asp-net-web-api-2-rsa-jwt-tokens-part-4\/\",\"url\":\"https:\/\/zaven.co\/blog\/user-authentication-asp-net-web-api-2-rsa-jwt-tokens-part-4\/\",\"name\":\"Web Api 2 User Authentication. Login controller | Zaven Blog\",\"isPartOf\":{\"@id\":\"https:\/\/zaven.co\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/zaven.co\/blog\/user-authentication-asp-net-web-api-2-rsa-jwt-tokens-part-4\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/zaven.co\/blog\/user-authentication-asp-net-web-api-2-rsa-jwt-tokens-part-4\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2016\/09\/klucze_3-1.jpg\",\"datePublished\":\"2016-09-23T11:08:32+00:00\",\"dateModified\":\"2025-04-08T17:55:19+00:00\",\"author\":{\"@id\":\"https:\/\/zaven.co\/blog\/#\/schema\/person\/7398fa7d171618b07d568aea38f1d17f\"},\"description\":\"After creating, signing and verifying the JWT Token, we can move on to programming the login controller and testing our application. Read more on our blog!\",\"breadcrumb\":{\"@id\":\"https:\/\/zaven.co\/blog\/user-authentication-asp-net-web-api-2-rsa-jwt-tokens-part-4\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/zaven.co\/blog\/user-authentication-asp-net-web-api-2-rsa-jwt-tokens-part-4\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/zaven.co\/blog\/user-authentication-asp-net-web-api-2-rsa-jwt-tokens-part-4\/#primaryimage\",\"url\":\"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2016\/09\/klucze_3-1.jpg\",\"contentUrl\":\"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2016\/09\/klucze_3-1.jpg\",\"width\":1500,\"height\":679,\"caption\":\"User Authentication in ASP.NET WEB API 2 with RSA-encrypted JWT Tokens\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/zaven.co\/blog\/user-authentication-asp-net-web-api-2-rsa-jwt-tokens-part-4\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/zaven.co\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"User Authentication in ASP.NET WEB API 2 with RSA-signed JWT Tokens (part 4)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/zaven.co\/blog\/#website\",\"url\":\"https:\/\/zaven.co\/blog\/\",\"name\":\"Zaven Blog\",\"description\":\"Software development blog. Generative AI, web &amp; mobile applications.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/zaven.co\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/zaven.co\/blog\/#\/schema\/person\/7398fa7d171618b07d568aea38f1d17f\",\"name\":\"Micha\u0142 Zawadzki\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/zaven.co\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/ca67b3acf11d373f6677081d08548407?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/ca67b3acf11d373f6677081d08548407?s=96&d=mm&r=g\",\"caption\":\"Micha\u0142 Zawadzki\"},\"description\":\"Micha\u0142 is at the Back-end site of our software team, specializing in .NET web apps. Apart from being an excellent table tennis player, he\u2019s very much into sci-fi literature and computer games.\",\"sameAs\":[\"https:\/\/pl.linkedin.com\/in\/micha\u0142-zawadzki-003428126\"],\"url\":\"https:\/\/zaven.co\/blog\/author\/michal\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Web Api 2 User Authentication. Login controller | Zaven Blog","description":"After creating, signing and verifying the JWT Token, we can move on to programming the login controller and testing our application. Read more on our blog!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/zaven.co\/blog\/user-authentication-asp-net-web-api-2-rsa-jwt-tokens-part-4\/","og_locale":"en_US","og_type":"article","og_title":"Web Api 2 User Authentication. Login controller | Zaven Blog","og_description":"After creating, signing and verifying the JWT Token, we can move on to programming the login controller and testing our application. Read more on our blog!","og_url":"https:\/\/zaven.co\/blog\/user-authentication-asp-net-web-api-2-rsa-jwt-tokens-part-4\/","og_site_name":"Zaven Blog","article_published_time":"2016-09-23T11:08:32+00:00","article_modified_time":"2025-04-08T17:55:19+00:00","og_image":[{"width":1500,"height":679,"url":"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2016\/09\/klucze_3-1.jpg","type":"image\/jpeg"}],"author":"Micha\u0142 Zawadzki","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Micha\u0142 Zawadzki","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/zaven.co\/blog\/user-authentication-asp-net-web-api-2-rsa-jwt-tokens-part-4\/","url":"https:\/\/zaven.co\/blog\/user-authentication-asp-net-web-api-2-rsa-jwt-tokens-part-4\/","name":"Web Api 2 User Authentication. Login controller | Zaven Blog","isPartOf":{"@id":"https:\/\/zaven.co\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/zaven.co\/blog\/user-authentication-asp-net-web-api-2-rsa-jwt-tokens-part-4\/#primaryimage"},"image":{"@id":"https:\/\/zaven.co\/blog\/user-authentication-asp-net-web-api-2-rsa-jwt-tokens-part-4\/#primaryimage"},"thumbnailUrl":"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2016\/09\/klucze_3-1.jpg","datePublished":"2016-09-23T11:08:32+00:00","dateModified":"2025-04-08T17:55:19+00:00","author":{"@id":"https:\/\/zaven.co\/blog\/#\/schema\/person\/7398fa7d171618b07d568aea38f1d17f"},"description":"After creating, signing and verifying the JWT Token, we can move on to programming the login controller and testing our application. Read more on our blog!","breadcrumb":{"@id":"https:\/\/zaven.co\/blog\/user-authentication-asp-net-web-api-2-rsa-jwt-tokens-part-4\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/zaven.co\/blog\/user-authentication-asp-net-web-api-2-rsa-jwt-tokens-part-4\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/zaven.co\/blog\/user-authentication-asp-net-web-api-2-rsa-jwt-tokens-part-4\/#primaryimage","url":"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2016\/09\/klucze_3-1.jpg","contentUrl":"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2016\/09\/klucze_3-1.jpg","width":1500,"height":679,"caption":"User Authentication in ASP.NET WEB API 2 with RSA-encrypted JWT Tokens"},{"@type":"BreadcrumbList","@id":"https:\/\/zaven.co\/blog\/user-authentication-asp-net-web-api-2-rsa-jwt-tokens-part-4\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/zaven.co\/blog\/"},{"@type":"ListItem","position":2,"name":"User Authentication in ASP.NET WEB API 2 with RSA-signed JWT Tokens (part 4)"}]},{"@type":"WebSite","@id":"https:\/\/zaven.co\/blog\/#website","url":"https:\/\/zaven.co\/blog\/","name":"Zaven Blog","description":"Software development blog. Generative AI, web &amp; mobile applications.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/zaven.co\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/zaven.co\/blog\/#\/schema\/person\/7398fa7d171618b07d568aea38f1d17f","name":"Micha\u0142 Zawadzki","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/zaven.co\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/ca67b3acf11d373f6677081d08548407?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ca67b3acf11d373f6677081d08548407?s=96&d=mm&r=g","caption":"Micha\u0142 Zawadzki"},"description":"Micha\u0142 is at the Back-end site of our software team, specializing in .NET web apps. Apart from being an excellent table tennis player, he\u2019s very much into sci-fi literature and computer games.","sameAs":["https:\/\/pl.linkedin.com\/in\/micha\u0142-zawadzki-003428126"],"url":"https:\/\/zaven.co\/blog\/author\/michal\/"}]}},"_links":{"self":[{"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/posts\/570","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/comments?post=570"}],"version-history":[{"count":10,"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/posts\/570\/revisions"}],"predecessor-version":[{"id":69779,"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/posts\/570\/revisions\/69779"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/media\/585"}],"wp:attachment":[{"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/media?parent=570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/categories?post=570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/tags?post=570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}