dotConnect for PostgreSQL Documentation
Using ADO.NET Implementation of ASP.NET Identity 1 for PostgreSQL
support@devart.com

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 or Visual Studio 2013 Update 1 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 ADO.NET implementation of ASP.NET Identity.

To complete this tutorial you need Visual Studio 2013 or Visual Studio 2013 Update 1 installed. For Visual Studio 2013 Update 2 or higher see Using ADO.NET Implementation of ASP.NET Identity 2 for PostgreSQL.

In order to create an ASP.NET MVC 5 application using dotConnect for PostgreSQL for storing identity information, perform the following steps:

  1. In the Add New Project dialog box expand Visual C# on the left, then Web, and then select ASP.NET Web Application. Name your project, for example, "AspNet_Identity_Application" and then click OK.

    Add New Project dialog box
  2. 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.

    New ASP.NET Project dialog box

  3. In Solution Explorer, right-click your project and select Manage NuGet Packages from the shortcut menu. In the search text box dialog, type "Identity.EntityFramework". Select this package in the list of results and click Uninstall. You will be prompted to uninstall the dependency package EntityFramework. Click Yes since we will no longer this package on this application.

  4. Add references to the necessary dotConnect for PostgreSQL assemblies:

    • Devart.Data.dll
    • Devart.Data.PostgreSql.dll
    • Devart.Data.PostgreSql.Web.Identity.dll

    For ASP.NET Identity 1 you need to add the reference to the Devart.Data.PostgreSql.Web.Identity.dll, having the revision number 0, and installed to the Program Files\Devart\dotConnect\PostgreSQL\Web\ASP.NET Identity 1 folder

When creating an ASP.NET application, using ASP.NET Identity, you can use the existing ASP.NET Identity functionality as is or customize this functionality. For example, you may add storing some custom information about the user. In our tutorial we will demonstrate both options.

- Using Existing ASP.NET Identity Functionality

First, you need to create a schema that will store information about users. To do it, execute the Install_identity_tables.sql script, which goes with dotConnect for PostgreSQL. By default, this script is in the Program Files\Devart\dotConnect\PostgreSQL\Web\ASP.NET Identity 1 folder. You can also copy this script from the Identity Database Script chapter of this topic. After this perform the following steps:

  1. In Visual Studio remove the IdentityModels.cs (or IdentityModels.vb for Visual Basic) file that is in the Models folder, from the project.
  2. In the Controllers project folder open the AccountController.cs (or AccountController.vb for Visual Basic) file.

    Replace

    C#csharpCopy Code
    using Microsoft.AspNet.Identity.EntityFramework;
    
    Visual BasicCopy Code
    Imports Microsoft.AspNet.Identity.EntityFramework
    

    with:

    C#csharpCopy Code
    using Devart.Common.Web.Identity;
    using Devart.Data.PostgreSql.Web.Identity;
    using ApplicationUser = Devart.Common.Web.Identity.IdentityUser;
    
    Visual BasicCopy Code
    Imports Devart.Common.Web.Identity
    Imports Devart.Data.PostgreSql.Web.Identity
    Imports ApplicationUser = Devart.Common.Web.Identity.IdentityUser
    

    Replace the constructor code:

    C#csharpCopy Code
            public AccountController()
                : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
            {
            }
    
    Visual BasicCopy Code
    	Public Sub New()
    		Me.New(New UserManager(Of ApplicationUser)(New UserStore(Of ApplicationUser)(New ApplicationDbContext())))
    	End Sub
    

    with

    C#csharpCopy Code
    	public AccountController()
    		: this(new UserManager<ApplicationUser>(new PgSqlUserStore()))        
    	{
    	}
    
    Visual BasicCopy Code
    	Public Sub New()
    		Me.New(New UserManager(Of ApplicationUser)(New PgSqlUserStore()))
    	End Sub
    
  3. 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" />
    	

+ Extending ASP.NET Identity Functionality

- Checking Application

Now we can run our application and check if everything works correctly.

  1. Run the application by pressing CTRL+F5.

    Getting started page
  2. Switch to the Register tab on the top of the page.

    Registration page

    For an example with extended ASP.NET Identity functionality the page looks like the following:

    Registration page (with e-mail)
  3. Enter a new user name and password (and email - if you have created an example with extended ASP.NET Identity functionality) to the corresponding boxes and click the Register button.
  4. The new user is now registered and logged in.

    User is registered
  5. After this you may use an appropriate database tool to connect to the database where the user data is stored and verify that the data is stored correctly.

+ Identity Database Script

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 2 for PostgreSQL  | Using Entity Framework Core Implementation of ASP.NET Core Identity for PostgreSQL