Authorization and Authentications with IdentityServer

Ebubekir Dinc
4 min readDec 9, 2023

This article is part of my Microservices and Cloud-Native Applications series. You can find the other parts of the series below.

  1. Saga Orchestration using MassTransit in .NET
  2. API Gateway with Ocelot
  3. Authorization and Authentications with IdentityServer
  4. Eventual Consistency with Integration Events using RabbitMq
  5. Distributed Logging with ElasticSearch, Kibana, and SeriLog
  6. Resiliency and Fault Tolerance with Polly
  7. Health Check with WatchDogs in a Microservices Architecture
  8. Distributed Tracing with Jaeger and OpenTelemetry in a Microservices Architecture
  9. Metrics to Monitor Microservices with OpenTelemetry and Prometheus

If you want to take a look at the GitHub code, you can access it here: https://github.com/ebubekirdinc/SuuCat

IdentityServer is an open-source software framework that allows developers to add authentication and authorization functionality to their applications. It is built on top of the open standards OAuth 2.0 and OpenID Connect, which provide a secure and standard way to manage user authentication and authorization.

A central authentication and authorization server that can be used to authenticate users across numerous apps or services can be made by developers using IdentityServer. Users may log in once and access different resources without having to re-enter their credentials, resulting in a safer and more seamless user experience.

Because IdentityServer is so flexible and versatile, developers may combine it with other identity management systems or add their own authentication and permission logic. As well as working with tokens, claims, and other security-related features, it offers a comprehensive collection of tools and libraries.

IdentityServer can be installed using the following Docker files. More information about the installation is here: https://github.com/ebubekirdinc/SuuCat/wiki/GettingStarted

docker-compose.yml

https://github.com/ebubekirdinc/SuuCat/blob/master/docker-compose.yml
https://github.com/ebubekirdinc/SuuCat/blob/master/docker-compose.yml

docker-compose.override.yml

https://github.com/ebubekirdinc/SuuCat/blob/master/docker-compose.override.yml
https://github.com/ebubekirdinc/SuuCat/blob/master/docker-compose.override.yml

- IdentityServerURL=http://identity.api

This line should be added to every microservice in order to communicate with IdentityServer.

Client Credentials

OAuth 2.0’s Client Credentials grant type enables clients to request an access token from the authorization server without involving users in order to access protected resources. With the help of its own credentials,
often a client ID and a client secret, the client authenticates itself to the authorization server in this flow and receives an access token which can be used to access protected resources.

The client application sends a token request to the authorization server with its own client ID and secret as authentication credentials in order to use the Client Credentials grant type. Following the client credentials being
verified, the authorization server generates an access token that the client application can use to gain access to restricted resources.

Client Credentials grant type request in Postman
Client Credentials token in jwt.io

In the following code, the ClientCredentials grant type is configured in the Identity project.

https://github.com/ebubekirdinc/SuuCat/blob/master/src/Services/Identity/Config.cs
https://github.com/ebubekirdinc/SuuCat/blob/master/src/Services/Identity/Config.cs

The AllowedScopes property specifies the scopes that the client is allowed to request. If the requested scope is not present in this list, the request will be rejected with a 403 Forbidden.

Resource Owner Password

One of the grant types in OAuth 2.0 and OpenID Connect, ResourceOwnerPassword, enables an application to directly exchange the user’s credentials (username and password) for an access token, enabling user authentication. Under this grant type, the user must directly provide their credentials to the client application, which is trusted to manage them safely.

By making a POST request to IdentityServer’s token endpoint together with the user’s username and password, a client using ROPC can obtain an access token.

Resource Owner grant type request in Postman

Here is the info of the token from jwt.io

Resource Owner token in jwt.io

The following code snippet shows a configuration for a client that uses the Resource Owner Password Credentials grant type in IdentityServer.

https://github.com/ebubekirdinc/SuuCat/blob/master/src/Services/Identity/Config.cs

More info can be found in the IdentityServer docs, and SuuCat GitHub.

--

--