
Secure ASP.NET core microservices with IdentityServer4 and Ocelot API Gateway using OAuth 2.0 and OpenID Connect to issue JWTs and enforce claims-based access.
Outline prerequisites for this course. Expect OAuth 2.0, OpenID Connect, and IdentityServer4 to be covered later, while leveraging C#, ASP.NET MVC, and REST API knowledge with Visual Studio and Postman.
Explore how JSON Web Tokens (JWT) enable authentication and authorization across services, explain RFC 7519 as the industry standard, and describe retrieving a token to access protected resources.
Demonstrates a jwt example scenario where a user logs in, the authentication server creates a token with a secret key, and clients pass the token in api request headers.
Explore the structure of JSON Web Tokens, detailing header, payload, and signature parts encoded in base64, and how claims, scopes like movieAPI, and data integrity are ensured.
OAuth2 is an open authorization protocol that delegates user authorization to a service, enabling third-party apps to access data across applications, not for authentication.
Explore OAuth2 authorization types and flows, including authorization code grant and client credential grant, and their roles in the client, resource owner, authorization server, and resource server.
Explore the three OpenID Connect endpoints—authorization endpoint, token endpoint, and userinfo endpoint—and learn how they handle authentication, consent, token exchange, and claim requests.
this lecture explains OpenID Connect authentication flows, covering the authorization code flow, implicit flow, and hybrid flow as extensions of OAuth2, defined by the response_type parameter.
Discover how IdentityServer4, an open source .NET Core framework, implements OpenID Connect and OAuth2 to provide authentication, access control, and token-based security for web apps and APIs.
Explore IdentityServer4 terminologies, including client applications, data source resources, and IdentityServer, and learn how clients register to request OpenId and OAuth2 tokens for authentication, authorization, and single sign-on.
Explore how IdentityServer4 centralizes authentication and authorization in a microservices world, issuing tokens to clients and securing protected APIs via the Ocelot API gateway.
Learn to build an Asp.Net Web API as an API resource in a microservice architecture, starting with a Movies API performing CRUD on in-memory relational data protected by Identity Server.
Open Visual Studio and create a new blank solution named SecureMicroservices, then add a project and prepare the application for development.
Create a movies api by scaffolding an api controller with actions using Entity Framework Core, wiring the movies api context and the connection string in startup to perform CRUD operations.
Configure an in-memory database for the entity framework core MoviesAPIContext to replace the real database, enabling quick testing and avoiding sql exceptions during movie retrieval.
Seed an in-memory database with entity framework core by updating the movie entity and creating a seed class and context, then seed initial data at startup via program.cs.
Test the movies API using Postman by performing get, post, put, and delete operations. Save the Postman collection for future testing.
Build Identity Server4 authentication microservices for a centralized, standalone authentication layer in a .NET core architecture, integrating the movies.api project and implementing OpenId Connect and OAuth2.
Create an IdentityServer4 ASP.NET web application from scratch by starting with an empty ASP.NET Core project, configuring launch settings, and running on port 5005.
Install the IdentityServer4 NuGet package and configure startup.cs with dependency injection and middleware to turn the app into an identity server, noting early configuration errors.
Configure IdentityServer4 with in-memory clients, resources, scopes, and test users, then run the server and verify the OpenID Connect discovery document and endpoints.
Explain how IdentityServer4 uses clients, API resources, API scopes, and test users with in-memory configurations and the role of identity resources and developer signing credentials.
Protect Movies.API resources with IdentityServer4 and JWT tokens using OAuth 2.0. Generate a JWT token via client_credentials and use it to secure Movies.API resources.
Learn how to obtain an access token from Identity Server using the client credentials grant type, configure a movie client, and test token responses via Postman.
Secure the movies API with jwt bearer token authentication by configuring AddAuthentication, token validation, and IdentityServer authority, then enable authentication and authorization middleware and apply the authorize attribute.
Demonstrates claim-based authentication with client ID restriction by inspecting user claims via an identity controller, obtaining bearer tokens, and enforcing a client ID policy in a .NET microservices API.
Build the MVC client application for consuming an API resource, the Movies.Client ASP.NET MVC project, and align its architecture to consume the protected Movies.API alongside Identity Server.
Create an ASP.NET Core MVC web client for the movies client microservice to consume a protected web API, configure HTTPS and port settings, and run the app.
Add a movie model class to the MVC client by creating it in the models folder, copying properties from the API project, and aligning the namespaces.
Refactor the movies client MVC app to consume the movies.api via a new API services layer. Implement IMovieApiService and MovieApiService with mock data and wire them in startup.
Learn to add an interactive IdentityServer4 UI for user credential entry within the authorization server, explore the architecture for client login operations, and set the stage for coding login experience.
Register MVC controllers and views, enable static files, and configure IdentityServer4 pipelines with routing and endpoints that map the home index to the default controller, including authorization middleware.
Update the identity server configuration by adding an interactive MVC client, define identity resources, and register test users to enable login via OpenID Connect with OpenID and profile scopes.
Integrate IdentityServer4 with the Movies.Client MVC app using OpenID Connect to authorize the movie page, redirect login to IdentityServer UI, and return a token to continue app operations.
Configure an OpenID Connect authentication layer for the movies mvc client, enabling login via IdentityServer and protecting the movies page with cookie-based authentication and OpenID Connect.
Protect the movies page by adding authorize to the movie controller and enforcing login via OpenID Connect with identity server, then run the app to retrieve and log the token.
Run a multi-project setup to discover OpenID Connect from an MVC client, opening the identity server login window and initiating the authorization flow, and review tokens and claims.
Add a logout button to the client layout, enabling sign out from identityserver4 via openid connect and cookie authentication, with the button shown only when the user is authenticated.
Consume protected Movie API resources from the Movies.Client project using HttpClientFactory, obtain a token from Identity Server with client credentials, and call the protected API validated by the server.
Obtain a token from the identity server using the identity model, then call the protected movies API with a bearer token and deserialize the response into a movie list.
Develop a delegating http handler to obtain a token from identity server with client credentials, attach it as a bearer token using http client factory, and call protected movies api.
Develop and integrate full crud operations for the movies api by consuming http calls with token authentication, extending the mvc app to create, update, delete, and get by id.
Learn how hybrid flow blends front-channel and back-channel token delivery in OpenId Connect, using authorization code, implicit, and hybrid flows via the authorization and token endpoints.
Implement hybrid flow in identity server to obtain a single token by combining OpenID Connect scopes, enabling the MVC interactive client to access Movie API resources with one token.
Refactor token retrieval to reuse the OpenID Connect token via IHttpContextAccessor, eliminating extra identity server requests and using GetTokenAsync to access the movies API and OpenID Connect token.
Log in with OpenID Connect on hybrid flow and extend claim-based authorization, verifying access tokens and client IDs to access the protected movies API.
Learn claim-based authorization with Identity Server to secure the Movies.Client MVC and Movies.API using OpenID Connect and OAuth, exploring hybrid flow and role-based access control.
Configure predefined test users with email and address claims in identity server to test authentication in secure .NET microservices, using Alice and Bob as example users.
Create role-based claims to enable role-based authorization by assigning Alice as user and Bob as admin, defining a role identity resource, and configuring client scopes to expose role claims.
Extend the identity server by adding a roles scope and mapping role claims to the MVC client for role-based authorization, and verify tokens via login and token inspection.
Develop an admin page that retrieves user info from identity server using an access token, maps claims to a view model, and renders them in a naive HTML view.
Develop an access denied page as an assignment for securing IdentityServer4-based microservices, implementing role-based access for unauthorized requests, and submit your GitHub link for review.
I will plan to implement adding EF.Core implementation into IS4.
Please follow this article series, this will provide to store login information in sql server with using ef.core. ->
https://identityserver4.readthedocs.io/en/latest/quickstarts/5_entityframework.html#
You will learn how to secure microservices with using standalone Identity Server 4 and backing with Ocelot API Gateway. We’re going to protect our ASP.NET Web MVC and API applications with using OAuth 2 and OpenID Connect in IdentityServer4. Securing your web application and API with tokens, working with claims, authentication and authorization middlewares and applying policies, and so on.
This course will led you get started securing your ASP.NET based microservices applications with IdentityServer4 using OAuth 2 and OpenID Connect on distributed microservices architecture. And Also you’ll learn how to secure protected APIs backing with Ocelot API Gateway in a microservices architecture.
Check the overall picture
You can see that we will have 4 Aspnet core microservices project which we are going to develop one by one and together.
Movies.API
First of all, we are going to develop Movies.API project and protect this API resources with IdentityServer4 OAuth 2.0 implementation. Generate JWT Token with client_credentials from IdentityServer4 and will use this token for securing Movies.API protected resources.
Movies.MVC
After that, we are going to develop Movies.MVC Asp.Net project for Interactive Client of our application. This Interactive Movies.MVC Client application will be secured with OpenID Connect in IdentityServer4. Our client application pass credentials with logging to an Identity Server and receive back a JSON Web Token (JWT).
Identity Server
Also, we are going to develop centralized standalone Authentication Server and Identity Provider with implementing IdentityServer4 package and the name of microservice is Identity Server.
Identity Server4 is an open source framework which implements OpenId Connect and OAuth2 protocols for .Net Core.
With Identity Server, we can provide authentication and access control for our web applications or Web APIs from a single point between applications or on a user basis.
Ocelot API Gateway
Lastly, we are going to develop Ocelot API Gateway and make secure protected API resources over the Ocelot API Gateway with transferring JWT web tokens. Once the client has a bearer token it will call the API endpoint which is fronted by Ocelot. Ocelot is working as a reverse proxy.
After Ocelot re-routes the request to the internal API, it will present the token to Identity Server in the authorization pipeline. If the client is authorized the request will be processed and a list of movies will be sent back to the client.
Also over these overall picture, we have also apply the Claim based authentications.
By the end of this course, you will have a practical understanding of how to secure .Net Microservices with IdentityServer4 using OAuth2, OpenID Connect and Ocelot Api Gateway.
Secure Existing Microservices Architecture
In the last section, we will give an assignment for security operations with identity server integration for an existing microservices reference application. We had developed run-aspnetcore-microservices reference application before this course. We will extend this application with IdentityServer OAuth 2.0 and OpenId Connect features with adding new Identity Server Microservice.
Is this course for you?
This course is very practical, about 90%+ of the lessons will involve you coding along with me on this project. If you are the type of person who gets the most out of learning by doing, then this course is definitely for you.
Tools you need for this course
In this course all the lessons are demonstrated using Visual Studio 2019 as a code editor. You can of course use any code editor you like and any Operating system you like as long as it's Windows or Mac.