.NET 5.0, ORACLE, ASP.NET Identity with N-tier architecture-Part 03

If you have not read first two parts, you can read them here.

In our last part we will explain how to use ASP.NET Identity with ORACLE database and how to do scaffolding of needed tables, indexes and other objects. Also we will explain how to use claim based authorization when claims are coming from database and not from Active Directory.

For us to be able to use ASP.NET Identity 3 we need to scaffold needed objects (Tables, Indexes), then create migration and update our database from created migration. We will do everything through Package Manager Console commands. I assume that you have istalled EF Core CLI.

First, we need to check if we have installed next nuget packages in our Dll project:

dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.AspNetCore.Identity.EntityFrameworkCore
dotnet add package Microsoft.AspNetCore.Identity.UI
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.EntityFrameworkCore.Tools

We have already installed next package in our part 2 of tutorial series:

dotnet add package Oracle.EntityFrameworkCore

Also take a look at version of these packages. They are adjusted to work with .NET 5.0 and not 6.0 version of .NET.

Next, we will change our context class to be able to work with Identity. As we have mentioned, we will load our claims from database so first thing is that we need to add class that will inherit IdentityUser class, so we can add our custom fields to inherited class. Lets name our class ApplicationUser and add two fields FirstName and LastName.

Next we will need to change our EXAMPLE_SCHEMA_Context, that will inherit IdentityDbContext<ApplicationUser> instead of DbContext.
public partial class EXAMPLE_SCHEMA_Context :

IdentityDbContext<ApplicationUser>

So, for us to be able to scaffold Identity with codegenerator, lets execute next package manager commands. First position ourselves in Web project.

cd .\NTierOracleIdentityExample.Web

Next execute command:

dotnet aspnet-codegenerator identity -h

Output will be next:

Arguments:
generator Name of the generator. Check available generators below.
Options:
-p| — project Path to .csproj file in the project.
-n| — nuget-package-dir
-c| — configuration Configuration for the project (Possible values: Debug/ Release)
-tfm| — target-framework Target Framework to use. (Short folder name of the tfm. eg. net46)
-b| — build-base-path
— no-build
Selected Code Generator: identityGenerator Options:
— dbContext|-dc : Name of the DbContext to use, or generate (if it does not exist).
— files|-fi : List of semicolon separated files to scaffold. Use the — listFiles option to see the available options.
— listFiles|-lf : Lists the files that can be scaffolded by using the ‘ — files’ option.
— userClass|-u : Name of the User class to generate.
— useSqLite|-sqlite : Flag to specify if DbContext should use SQLite instead of SQL Server.
— force|-f : Use this option to overwrite existing files.
— useDefaultUI|-udui : Use this option to setup identity and to use Default UI.
— layout|-l : Specify a custom layout file to use.
— generateLayout|-gl : Use this option to generate a new _Layout.cshtml
— bootstrapVersion|-b : Specify the bootstrap version. Valid values: ‘3’, ‘4’. Default is 4.

This means that we have selected identity as generator and -h to list generator options.

For our project, we will setup identity from existing DbContext, so we run next command where we specify path of our DbContext class in Dll project. As we need to overwrite at least one file from UI because we are using existing DbContext, we will overwrite Account.Register, otherwise we will get
an error.

Also we cannot use useDefaultUI|-udui command, as we are using existing context. Also we do not want to scaffold whole UI as we are using windows authentication and our custom views.

dotnet aspnet-codegenerator identity — dbContext NTierOracleIdentityExample.Dll.Context.EXAMPLE_SCHEMA_Context — files “Account.Register;”

We can delete Pages as we will not need it for our example. So next step is to create our migration and update our database.

cd .\NTierOracleIdentityExample.Dll
dotnet ef migrations add EXAMPLE_IDENTITY — output-dir Migrations — startup-project “../NTierOracleIdentityExample.Web/NTierOracleIdentityExample.Web.csproj”

And last lets update our database.

dotnet ef database update EXAMPLE_IDENTITY — startup-project “../NTierOracleIdentityExample.Web/NTierOracleIdentityExample.Web.csproj”

This done lets check our oracle database for newly created tables, indexes.

So we are left with setting up our Startup.cs class to add services like ClaimsTransformation and also setting up ClaimsTransformation itself.

So first lets add our ClaimsTransformer to services.

Next lets add configure the identity system for the specified User and Role types. For User type we will use inherited class ApplicationUser and for Role type we will use default IdentityRole.

Here we will encounter one problem. If we run our app we will see that our claims transformer will not run. AddIdentity in prevoius code block, sets the DefaultAuthenticateScheme (Identity.Application) which takes priority for the Auth middleware so claims transformation doesn’t run.
Setting DefaultAuthenticateScheme to IISDefaults.AuthenticationScheme after AddIdentity fixes this issue.

Last, we will add two policies that will be used in policy base authorization later in our controllers.

Next we need to add ClaimsTransformer class that will fetch our user from database and add custom claims as FirstName and LastName.

Do not forget to insert initial user in database and assign role that we have created previously.

Now we can run our application and we get this.

I will not get into details of creation UI, but you can find more info here:

Code is also available on GitHub.

If you want more content, clap for it and hit that follow button.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kenan Begić

Hello there, I’ Kenan. Software Engineer, Fullstack developer, mobile hobbyist. Support my content at: buymeacoffee.com/kenanbegic