Using Entity Framework Core Implementation of ASP.NET Core Identity for PostgreSQL
In This Topic
ASP.NET Core Identity is a modern system for user authentication and authorization in ASP.NET Core. You can find more information about ASP.NET Core Identity in the official Microsoft documentation.
dotConnect for PostgreSQL provides support for implementing ASP.NET Core Identity with PostgreSQL databases by utilizing Entity Framework Core in your web applications. With this integration, you can take advantage of ASP.NET Core Identity features such as a unit-testable authentication system, social login support, and OWIN integration. This tutorial demonstrates how to create an Entity Framework implementation of ASP.NET Core Identity.
Prerequisites
- Visual Studio 2017 or later.
- If using the trial version of dotConnect for PostgreSQL, install the product to obtain the corresponding trial license key file. For more details on technical licensing for .NET Core applications, refer to the Licensing .NET Standard (.NET Core) Projects documentation.
In order to create an ASP.NET Core application using dotConnect for PostgreSQL for storing identity information, perform the following steps based on the Visual Studio version:
Visual Studio 2019, 2022
-
Select C# from the left-hand side of the list, then move to the middle section to choose Windows, and finally select Web on the right. Next, select ASP.NET Core Web Application, proceed by clicking Next, and then select Create.
-
In the next dialog, select the Web Application (Model-View-Controller) template.
Visual Studio 2019
To proceed, click Change under Authentication.
Then select Individual User Accounts as the authentication method and click OK.
-
Execute the following command in the Package Manager Console:
install-package Devart.Data.PostgreSql.EFCore
In the appsettings.json file of your project, replace the default connection string with your PostgreSQL one. For example:
"ConnectionStrings": {
"DefaultConnection": "host=server;database=test;user id=postgres;"
},
If you purchased dotConnect for PostgreSQL, add your license key to the connection string as the License Key connection string parameter.
In the Startup.cs file of the application, replace
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
with
options.UsePostgreSql(Configuration.GetConnectionString("DefaultConnection")));
In the Solution Explorer, open the Data\Migrations folder and delete all its content.
-
Execute the following command in the Package Manager Console:
add-migration CreateIdentitySchema -OutputDir Data\Migrations
-
Execute the following command in the Package Manager Console:
Visual Studio 2017
-
In the New Project dialog, expand Visual C# on the left, click Web, and select ASP.NET Core Web Application (.NET Core). Name your project, for example, "AspNetCore_Identity_Application", and click OK.
-
In the next dialog, select the Web Application (Model-View-Controller) template.
Visual Studio 2017
To proceed, click Change Authentication.
Then select Individual User Accounts as the authentication method and click OK.
-
Execute the following command in the Package Manager Console:
install-package Devart.Data.PostgreSql.EFCore
In the appsettings.json file of your project, replace the default connection string with your PostgreSQL one. For example:
"ConnectionStrings": {
"DefaultConnection": "host=server;database=test;user id=postgres;"
},
If you purchased dotConnect for PostgreSQL, add your license key to the connection string as the License Key connection string parameter.
In the Startup.cs file of the application, replace
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
with
options.UsePostgreSql(Configuration.GetConnectionString("DefaultConnection")));
In the Solution Explorer, open the Data\Migrations folder and delete all its content.
-
Execute the following command in the Package Manager Console:
add-migration CreateIdentitySchema -OutputDir Data\Migrations
-
Execute the following command in the Package Manager Console:
Checking Application
Now, we can run our application and check if everything works correctly.
Run the application by pressing CTRL+F5.
Switch to the Register tab at the top of the page.
Enter your email and password in the designated fields, then click Register.
The new user is registered and logged in, and the ASP.NET Identity tables are created in the PostgreSQL database.
Optional: Use any Oracle client tool to connect to the database where the user data is stored and verify that the information has been saved correctly.
See Also
Using Entity Framework Implementation of ASP.NET Identity 1 for PostgreSQL
| Using Entity Framework Implementation of ASP.NET Identity 2 for PostgreSQL
| Using ADO.NET Implementation of ASP.NET Identity 1 for PostgreSQL
| Using ADO.NET Implementation of ASP.NET Identity 2 for PostgreSQL