dotConnect for DB2 Documentation
In This Topic
    Using ADO.NET Implementation of ASP.NET Identity 2 for DB2
    In This Topic

    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 Update 2 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 DB2 enables you to employ an implementation of ASP.NET Identity for DB2 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 Update 2 or higher. For Visual Studio 2013 or Visual Studio 2013 Update 1 see Using ADO.NET Implementation of ASP.NET Identity 1 for DB2.

    In order to create an ASP.NET MVC 5 application using dotConnect for DB2 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. For Visual Studio 2013 Update 2 click Yes. For Visual Studio 2013 Update 3 or higher click No.

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

      • Devart.Data.dll
      • Devart.Data.DB2.dll
      • Devart.Data.DB2.Web.Identity.dll

      For ASP.NET Identity 2 you need to add the reference to the Devart.Data.DB2.Web.Identity.dll, having the revision number 2, and installed to the Program Files\Devart\dotConnect\DB2\Web\ASP.NET Identity 2 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 DB2. By default, this script is in the Program Files\Devart\dotConnect\DB2\Web\ASP.NET Identity 2 folder. You can also copy this script from the Identity Database Script chapter of this topic. After this perform the following steps:

    1. In the Models project folder open the IdentityModels.cs (or IdentityModels.vb for Visual Basic) file. that is in the Models folder, from the project.
    2. Replace the file contents with

      using System.Security.Claims;
      using System.Threading.Tasks;
      using Microsoft.AspNet.Identity;
      using Devart.Common.Web.Identity;
      using Devart.Data.DB2.Web.Identity;
      using ApplicationUser = Devart.Common.Web.Identity.IdentityUser;
      
      namespace AspNet_Identity_Application.Models
      {
          public static class ApplicationUserExtensions
          {
              public static async Task<ClaimsIdentity> GenerateUserIdentityAsync(this ApplicationUser user, UserManager<ApplicationUser> manager)
              {
                  // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
                  var userIdentity = await manager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
                  // Add custom user claims here
                  return userIdentity;
              }
          }
      }
      
      
      
      Imports System.Security.Claims
      Imports System.Threading.Tasks
      Imports Microsoft.AspNet.Identity
      Imports Devart.Common.Web.Identity
      Imports Devart.Data.DB2.Web.Identity
      Imports ApplicationUser = Devart.Common.Web.Identity.IdentityUser
      
      Public Module ApplicationUserExtensions
          <System.Runtime.CompilerServices.Extension> _
          Public Async Function GenerateUserIdentityAsync(user As ApplicationUser, manager As UserManager(Of ApplicationUser)) As Task(Of ClaimsIdentity)
              ' Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
              Dim userIdentity = Await manager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie)
              ' Add custom user claims here
              Return userIdentity
          End Function
      End Module
      
      

      Note that if you have specified the name for the application other than "AspNet_Identity_Application", you must use the corresponding namespace name in the code above instead of "AspNet_Identity_Application".

    3. In the Controllers project folder open the AccountController.cs (or AccountController.vb for Visual Basic) file.

      If you use Visual Studio 2013 Update 2, replace

      using Microsoft.AspNet.Identity.EntityFramework;
      
      
      Imports Microsoft.AspNet.Identity.EntityFramework
      
      

      with:

      using Devart.Common.Web.Identity;
      using Devart.Data.DB2.Web.Identity;
      using ApplicationUser = Devart.Common.Web.Identity.IdentityUser;
      
      
      Imports Devart.Common.Web.Identity
      Imports Devart.Data.DB2.Web.Identity
      Imports ApplicationUser = Devart.Common.Web.Identity.IdentityUser
      
      

      If you use Visual Studio 2013 Update 3 or higher, there is no such line, just add the above code to the beginning of the file.

    4. In the App_Start project folder open the IdentityConfig.cs (or IdentityConfig.vb for Visual Basic) file.

      Replace

      using Microsoft.AspNet.Identity.EntityFramework;
      
      
      Imports Microsoft.AspNet.Identity.EntityFramework
      
      

      with:

      using Devart.Common.Web.Identity;
      using Devart.Data.DB2.Web.Identity;
      using ApplicationUser = Devart.Common.Web.Identity.IdentityUser;
      
      
      Imports Devart.Common.Web.Identity
      Imports Devart.Data.DB2.Web.Identity
      Imports ApplicationUser = Devart.Common.Web.Identity.IdentityUser
      
      
    5. Replace the following line in the Create method:

      var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
      
      
      Private manager As var = New ApplicationUserManager(New UserStore(Of ApplicationUser)(context.[Get](Of ApplicationDbContext)()))
      
      

      with:

      var manager = new ApplicationUserManager(new DB2UserStore());
      
      
      Dim manager = New ApplicationUserManager(New DB2UserStore())
      
      
    6. In the App_Start project folder open the Startup.Auth.cs (or Startup.Auth.vb for Visual Basic) file.

      If you use Visual Studio 2013 Update 2, replace

      using Microsoft.AspNet.Identity.EntityFramework;
      
      
      Imports Microsoft.AspNet.Identity.EntityFramework
      
      

      with:

      using Devart.Common.Web.Identity;
      using Devart.Data.DB2.Web.Identity;
      using ApplicationUser = Devart.Common.Web.Identity.IdentityUser;
      
      
      Imports Devart.Common.Web.Identity
      Imports Devart.Data.DB2.Web.Identity
      Imports ApplicationUser = Devart.Common.Web.Identity.IdentityUser
      
      

      If you use Visual Studio 2013 Update 3 or higher, there is no such line, just add the above code to the beginning of the file.

    7. Delete the following line:

      app.CreatePerOwinContext(ApplicationDbContext.Create);
      
      
      app.CreatePerOwinContext(ApplicationDbContext.Create)
      
      
    8. For Visual Studio 2013 Update 3 or higher only - in the Controllers project folder open the ManageController.cs (or ManageController.vb for Visual Basic) file. Add the following code to the beginning of the file:

      using Devart.Common.Web.Identity;
      using Devart.Data.DB2.Web.Identity;
      using ApplicationUser = Devart.Common.Web.Identity.IdentityUser;
      
      
      Imports Devart.Common.Web.Identity
      Imports Devart.Data.DB2.Web.Identity
      Imports ApplicationUser = Devart.Common.Web.Identity.IdentityUser
      
      
    9. In the web.config file of your project, replace the default connection string with your DB2 one.

      <add name="DefaultConnection" connectionString="user id=db2admin;server=db2;database=SAMPLE;" providerName="Devart.Data.DB2" />
      	

    + 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 DB2  | Using ADO.NET Implementation of ASP.NET Identity 1 for DB2  | Using Entity Framework Implementation of ASP.NET Identity 2 for DB2