ASP.NET Identity is a new system of user authentication and authorization, that continues the evolution of ASP.NET membership system, and is used in the Visual Studio 2013 project templates for ASP.NET MVC, Web Forms, Web API and SPA. You can read more on ASP.NET Identity in Microsoft documentation.
dotConnect for PostgreSQL enables you to employ an implementation of ASP.NET Identity for PostgreSQL database using either ADO.NET or Entity Framework functionality in your web applications. This allows you to use such ASP.NET Identity benefits as unit-testable user authentication system, social login support, OWIN integration, etc. This tutorial demonstrates creating an Entity Framework implementation of ASP.NET Identity.
To complete this tutorial you need Visual Studio 2013 installed.
In order to create an ASP.NET MVC 5 application using dotConnect for PostgreSQL for storing identity information, perform the following steps:
In the New ASP.NET Project dialog box, select the MVC template with the default options (that includes Individual User Accounts as authentication method) and click OK.
Add references to the necessary dotConnect for PostgreSQL assemblies:
You need to add the last file from the Entity\EF6 subfolder of the dotConnect for PostgreSQL installation folder. Or you may add it from the GAC.
In the web.config file of your project, replace the default connection string with your PostgreSQL one.
<add name="DefaultConnection" connectionString="host=server;database=test;user id=postgres;" providerName="Devart.Data.PostgreSql" />
Register the Entity Framework provider in the <providers> section of the web.config file
Before the registration:
<providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> </providers>
After the registration:
<providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> <provider invariantName="Devart.Data.PostgreSql" type="Devart.Data.PostgreSql.Entity.PgSqlEntityProviderServices, Devart.Data.PostgreSql.Entity.EF6, Version=7.1.26.0, Culture=neutral, PublicKeyToken=09af7300eec23701" /> </providers>
Note: replace "7.1.26.0" with the actual version.
Add the following namespaces to the using list:
Add the new Configuration class and register the dotConnect for PostgreSQL provider SQL generator for Code-First Migrations.
Extend the ApplicationDbContext class code.
We need to add a static constructor setting the database initializer for the context.
After this we also need to add the OnModelCreating method, where we change type for character properties that aren't a part of entity key (they are filtered by their names) from the TEXT data type to VARCHAR(256) via a lightweight convention.
The code before editing:
The code after editing:
Now we can run our application and check if everything works correctly.
Using ADO.NET 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 2 for PostgreSQL | Using Entity Framework Core Implementation of ASP.NET Core Identity for PostgreSQL