Nemanja Tomic

Cookies and JWTs - Use Cases and Practical Example in C#

October 18, 2025

Welcome to the third and final part of my Identity and Access Management (IAM) trilogy. In the last two parts, we defined what authentication and authorization are. We also discussed two very important protocols for managing authentication and authorization via third-party apps like OAuth 2.0 and OpenID Connect.

This covers most of the key aspects of authentication and authorization. However, there is still one important puzzle piece missing, and that is persisting a session after a user logs in.

To really understand this problem, imagine a user authenticates via the login form and then refreshes the page. Without implementing any mechanism to persist the session after login, the user still can’t authorize herself for anything because the backend requires something that actually proves the user has authenticated themselves at some point. This is where cookies and JSON Web Tokens (JWTs) come in.

Prerequisites

What you should know about cookies

Session cookies are stateful elements. They contain data that the server sends to the browser for temporary use. The authentication data inside a cookie is stored on both the client and server. The server keeps track of active sessions in a database, while the browser holds the identifier to the active session. When a request is made to the server, the session ID is used to look up information such as user roles or privileges for authentication, in order to check if the session is valid.

Advantages of cookies

Consider these key benefits:

The downside of cookies

Some of the disadvantages of cookies include:

Cookies aren’t the only way to store session IDs; other options include URLs and form fields. Cookies are more secure than those two, but how secure are cookies?

While cookies can be made secure by setting the appropriate attributes and following best practices, they can also be made insecure by neglecting these steps.

JSON Web Tokens

Tokens—or JWTs in this context—are stateless in nature, meaning the server doesn’t need to keep a record of the token. Each token is self-contained, holding the information needed for verification and identification on the server.

Advantages of tokens

Here are some specific advantages of tokens:

If you use a browser’s local storage, tokens can’t be accessed by a subdomain. However, they can be accessed and manipulated by any JavaScript code on the webpage, as well as by browser plugins. This isn’t a recommended method: first, it poses a security risk, plus you must manage the storage.

Session storage is another way to store tokens. The drawback is that the token is destroyed when the browser is closed.

Disadvantages of JWT tokens

Here are some downsides of tokens to be aware of:

But what about the security of tokens?

When to Use Cookies or Tokens

In general, the choice between a session cookie and a structured token will depend on your use case. You should use cookies when you need to keep track of user interactions, such as with an e-commerce application or website. You can use tokens when building API services or implementing distributed systems.

Another popular option is to use a token inside a cookie. This is by far the most secure option for persisting user sessions, while still giving you all the flexibility you would have with JWT tokens. The technique is simple. You create your token in the backend, but instead of returning the token as a JSON response, you return a cookie that contains the JWT token.

I won’t go into any more detail on how this works specifically for C# here. You can always read more about it in the official documentation. But to be quite frank with you, once you understand how it works in one language, you should understand it in all other languages as well (at least in theory).

Conclusion

Great, the trilogy is done! And to be honest, I am quite happy with it. Of course, I can always go more in depth, but I think I hit the sweet spot for people who are not as tech savy.