<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Aydanur Akça on Medium]]></title>
        <description><![CDATA[Stories by Aydanur Akça on Medium]]></description>
        <link>https://medium.com/@aydanurakca?source=rss-22db4ac70433------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Fz3DYYVw5mA8-O8qZgn7kw.jpeg</url>
            <title>Stories by Aydanur Akça on Medium</title>
            <link>https://medium.com/@aydanurakca?source=rss-22db4ac70433------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 13:48:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@aydanurakca/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Authentication with JSON Web Tokens using Refresh Tokens on an ASP.Net Core MVC Application]]></title>
            <link>https://medium.com/@aydanurakca/authentication-with-json-web-tokens-using-refresh-tokens-on-an-asp-net-core-mvc-application-47770b65eb5c?source=rss-22db4ac70433------2</link>
            <guid isPermaLink="false">https://medium.com/p/47770b65eb5c</guid>
            <category><![CDATA[authentication]]></category>
            <category><![CDATA[mvc]]></category>
            <category><![CDATA[net-core]]></category>
            <category><![CDATA[json-web-token]]></category>
            <category><![CDATA[refresh-token]]></category>
            <dc:creator><![CDATA[Aydanur Akça]]></dc:creator>
            <pubDate>Tue, 01 Sep 2020 10:49:41 GMT</pubDate>
            <atom:updated>2020-09-01T10:49:41.274Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*IOYCnC3N0cJqj8wSZ88t6Q.png" /></figure><p>Hello Everyone! In this article, I will mention about authentication with JSON Web Tokens using Refresh Tokens and show you an implementation on ASP.Net Core 3.1 MVC application.</p><p><strong>Why do we need token authentication?</strong></p><p>Token-based authentication has become popular for web and mobile applications. The main reason for using token-based authentication is simple. The users log in with entering the username and the password. While accessing a page that requires authentication, the users have to log in again. That’s because the web service is stateless and this means, we have to get some credentials with each request. Instead of entering the username and the password every single time, they could be stored on browser and the information can be grabbed from there. But storing the password is insecure. That’s why we need tokens for authentication. Tokens store information about the user. Of course, that information does not include the password!!</p><p><strong>What is JWT?</strong></p><p>JSON Web Token is a standard that defines a way to transmit information between parties securely as a JSON object. The information is always trusted because it is signed using a secret key with a hashing algorithm. The key is only known by the server.</p><p>The most common usage of JWTs is authorization.</p><p>The JWTs consist of three parts: the header, the payload and the signature. These parts are separated with a dot (.). A typical JWT looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/647/1*b306AG63rBN_lpnqiwNcMQ.png" /></figure><p><strong>Header</strong></p><p>The header includes the token type and the hashing algorithm information. It is encoded to Base64Url. The most common and recommended JWT signing algorithms are:</p><p>· HMAC + SHA256</p><p>· RSASSA-PKCS1-v1_5 + SHA256</p><p>· ECDSA + P-256 + SHA256</p><p>The hashing algorithm used here is HS256 (HMAC — SHA256).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/718/1*8Akb5K8vbUdk4POuLtJg0g.png" /></figure><p><strong>Payload</strong></p><p>The payload is the second part of the JWT, and it contains claims. The claims include information about the user. There are three types of claims which are public, private, and registered claims. It is encoded to Base64Url.</p><p>The most common claims are:</p><p>· <strong>“iss”</strong> Issuer → identifies the principal that issued the JWT.</p><p>· <strong>“sub”</strong> Subject → identifies the principal that is the subject of the JWT.</p><p>· <strong>“aud”</strong> Audience → identifies the recipients that the JWT is intended for.</p><p>· <strong>“exp”</strong> Expiration time → identifies the expiration time after which the JWT must not be accepted for processing.</p><p>· <strong>“nbf”</strong> Not before → identifies the time before which the JWT must not be accepted for processing.</p><p>· <strong>“iat”</strong> Issued at → identifies the time at which the JWT was issued.</p><p>· <strong>“jti”</strong> JWT ID → provides a unique identifier for the JWT.</p><p>Use of these claims is optional.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/717/1*MsFPbUw8U-MshZc-NR2sug.png" /></figure><p><strong>Signature</strong></p><p>The encoded header and payload are signed with the secret key according to the algorithm specified in the header and the signature part of the JWT is created. This part is used to verify that the JWT hasn’t changed and has transmitted securely.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/718/1*4hlA9WLs0WhROXu7lAzBgA.png" /></figure><p><strong>What about security?</strong></p><p>JWTs have to be stored on the client-side. The most secure way to store JWT tokens is storing them as an HttpOnly Cookie. But of course, JWT has a risk of being stolen. To minimize the risk, the expiration time of each JWT must be decided logically. After a JWT is expired the user has to log in again with the credentials. What if the expiration time is less than 5 minutes? Does the user has to log in every single time the token is expired? To prevent this issue, the refresh tokens can be used.</p><p><em>Note that, according to GDPR (General Data Protection Regulation), it is not allowed to set the cookies without asking permission from the user.</em></p><p><strong>How to use the Refresh Tokens?</strong></p><p>When a user logs in, the refresh token is also generated with the access token (JWT). The expiration time of a refresh token must also be short, but longer than the access token. When the access token is expired, and the refresh token is still valid, new access and refresh tokens are generated. When the user left the app and the refresh token is expired, the user has to log in again.</p><p>After the brief information about the JWTs and the Refresh Tokens, let’s move on to the implementation.</p><p><strong>Implementation of JWTs and Refresh Tokens to an ASP.Net Core MVC project</strong></p><p>In appsettings.json, we must enter a secret key which will be used for signing the signature part of the JWT. Make sure that your secret key has at least 16 bits.</p><p>Note that, the secret key could not be stored in code, it is not secure.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/36d79abf3b05f1f835a0fd67b75954d6/href">https://medium.com/media/36d79abf3b05f1f835a0fd67b75954d6/href</a></iframe><p>To get the secret key from appsettings.json file, we must create a class called AppSettings.cs and the key must be an attribute in that class.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ce73576478aa0f451fb27a794bbea641/href">https://medium.com/media/ce73576478aa0f451fb27a794bbea641/href</a></iframe><p>This part must be added to the Startup.cs to make the connection between appsettings.json and AppSettings.cs. Now we can take the secret key with a get call from an object of AppSetting.cs.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e2a7d6356043ce38a32d799bb555b1a6/href">https://medium.com/media/e2a7d6356043ce38a32d799bb555b1a6/href</a></iframe><p>These are the packages that you must add to your application. To install these, you have to enter Manage NuGet Packets and browse the package names.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/939/1*uYttcjUJ0I9sLzW9aXhDRA.png" /></figure><p>Firstly, when the user logs in with their credentials such as username and password, the server authenticates the user. If the user exists and authenticated, the JWT and the refresh token are generated.</p><p>This is an example function for generating JWT tokens. In the generated JWT, the payload part includes the id of the user. The expiration time is set to a minute. SymmetricSecurityKey is our secret key from appsettings.json. And the security algorithm is HS256 (HMAC-SHA256).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1386b2671ea250f882ac9809518c1832/href">https://medium.com/media/1386b2671ea250f882ac9809518c1832/href</a></iframe><p>The function below is used for generating the refresh tokens. The expiration time is set to 2 minutes.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c280a83693480cb7e4389907742b2e55/href">https://medium.com/media/c280a83693480cb7e4389907742b2e55/href</a></iframe><p>Here’s the Refresh token entity which has some attributes such as isExpired, isActive, Token, Expires, Created etc. The generated refresh token is in this type.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e7e429d1d438d508c4759fa396e65320/href">https://medium.com/media/e7e429d1d438d508c4759fa396e65320/href</a></iframe><p>After the JWT and the refresh token are generated, they are stored as cookies on the browser. The tokens are not stored anywhere on the server-side. They are only stored on the client-side. The function below is a helper function for storing the tokens as HttpOnly and Secure cookies. The expiration time is set to a day for cookies. This part can be done in the controller.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a90b78813b141010e6d56770a5177258/href">https://medium.com/media/a90b78813b141010e6d56770a5177258/href</a></iframe><p>When the user wants to call a function or access to a page that requires authentication, the JWT is controlled. The token is taken from the cookie and checked whether it is valid or not. Here, we can think of JWT like a ticket. If your ticket is valid, you can pass and access what you want, else you must bring a valid ticket with you to pass.</p><p>That function below is created for validating the JWT token. If the access token is expired or changed, the function returns null. It returns the user id in other conditions. If that function returns null that means the validation fails and tokens need to be refreshed.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c2445ca6debd38f1ba4dbb41b33c618a/href">https://medium.com/media/c2445ca6debd38f1ba4dbb41b33c618a/href</a></iframe><p>When the validation fails, the function below is called. It checks whether the refresh token is valid or not. If the refresh token is still valid, that function regenerates the JWT and the refresh token. Then these tokens are sent to the controller and cookies are set with the new tokens. The user can access the page because the JWT token is valid now.</p><p>If the refresh token is also not valid, that function returns null and the cookies do not change. This does not let the user access to the page which requires authentication. The user must log in with their username and password and be authenticated again.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/df705f587f54ce940a08656ac8f308cf/href">https://medium.com/media/df705f587f54ce940a08656ac8f308cf/href</a></iframe><p>The AuthenticateResponse ViewModel is created to connect the user and the tokens. User entity does not have an access token attribute. Because we cannot store the tokens in the database or on the server-side. This ViewModel is used to sent tokens with the user to the controller.</p><p>For each request from the user, the JWT validation is done and if the validation fails the tokens are refreshed. When the user logs out, the cookies are removed. If a user logs in from a different browser, it generates tokens for that browser, too. Each access and refresh tokens are valid unless their expiration time has come.</p><p>As a conclusion, in this article, we learnt what JWTs are, how to implement and store them securely, and have seen some important points about implementation on an MVC project.</p><p>Thank you for reading. Good luck on implementing JWT tokens!</p><p>References:</p><p>1. Dev.to. 2020. <em>Authentication With JSON Web Tokens In .NET Core 3.1 — DEV</em>. [online] Available at: &lt;https://dev.to/_patrickgod/authentication-with-json-web-tokens-in-net-core-3-1-29bd&gt; [Accessed 28 August 2020].</p><p>2. Dev.to. 2020. <em>How To Securely Store JWT Tokens. — DEV</em>. [online] Available at: &lt;https://dev.to/gkoniaris/how-to-securely-store-jwt-tokens-51cf&gt; [Accessed 28 August 2020].</p><p>3. Jwt.io. 2020. <em>JWT.IO — JSON Web Tokens Introduction</em>. [online] Available at: &lt;https://jwt.io/introduction/&gt; [Accessed 28 August 2020].</p><p>4. GitHub. 2020. <em>Can We Create Multiple JWT’s Against One User? · Issue #231 · Firebase/Php-Jwt</em>. [online] Available at: &lt;https://github.com/firebase/php-jwt/issues/231&gt; [Accessed 28 August 2020].</p><p>5. GitHub. 2020. <em>Cornflourblue/Aspnet-Core-3-Jwt-Authentication-Api</em>. [online] Available at: &lt;https://github.com/cornflourblue/aspnet-core-3-jwt-authentication-api&gt; [Accessed 28 August 2020].</p><p>6. GitHub. 2020. <em>Cornflourblue/Aspnet-Core-3-Jwt-Refresh-Tokens-Api</em>. [online] Available at: &lt;https://github.com/cornflourblue/aspnet-core-3-jwt-refresh-tokens-api&gt; [Accessed 28 August 2020].</p><p>7. Tools.ietf.org. 2020. <em>RFC 7519 — JSON Web Token (JWT)</em>. [online] Available at: &lt;https://tools.ietf.org/html/rfc7519#section-4.1&gt; [Accessed 1 September 2020].</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=47770b65eb5c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>