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 Oracle enables you to employ an implementation of ASP.NET Identity for Oracle 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 Oracle for storing identity information, perform the following steps:
-
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.
-
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 Oracle assemblies:
- Devart.Data.dll
- Devart.Data.Oracle.dll
- Devart.Data.Oracle.Entity.EF6.dll
You need to add the last file from the Entity\EF6 subfolder of the dotConnect for Oracle 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 Oracle one.
<add name="DefaultConnection" connectionString="User Id=Scott;Password=tiger;Data Source=Ora;" providerName="Devart.Data.Oracle" />
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.Oracle" type="Devart.Data.Oracle.Entity.OracleEntityProviderServices, Devart.Data.Oracle.Entity.EF6, Version=8.4.215.0, Culture=neutral, PublicKeyToken=09af7300eec23701" /> </providers>
Note: replace "8.4.215.0" with the actual version.
- Open the IdentityModels.cs file from the Models project folder.
Add the following namespaces to the using list:
C#csharp Copy Code
using System.Data.Entity; using System.Data.Entity.Migrations;
Visual Basic Copy Code
Imports System.Data.Entity Imports System.Data.Entity.Migrations
-
Add the new Configuration class and register the dotConnect for Oracle provider SQL generator for Code-First Migrations.
C#csharp Copy Code
internal class Configuration : DbMigrationsConfiguration<ApplicationDbContext> { public Configuration() { AutomaticMigrationsEnabled = true; SetSqlGenerator("Devart.Data.Oracle", new Devart.Data.Oracle.Entity.Migrations.OracleEntityMigrationSqlGenerator()); } }
Visual Basic Copy Code
Friend Class Configuration Inherits DbMigrationsConfiguration(Of ApplicationDbContext) Public Sub New() AutomaticMigrationsEnabled = True SetSqlGenerator("Devart.Data.Oracle", New Devart.Data.Oracle.Entity.Migrations.OracleEntityMigrationSqlGenerator()) End Sub End Class
-
Extend the ApplicationDbContext class code.
We need to add a static constructor setting the database initializer for the context. Additionally we need to set the TruncateLongDefaultNames option of dotConnect for Oracle Entity Framework provider to true in order for foreign key names to be generated correctly because of Oracle 30 character limitation for foreign key names.
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 NCLOB data type to NVARCHAR(256) via a lightweight convention.
The code before editing:
C#csharp Copy Code
public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("DefaultConnection") { } }
Visual Basic Copy Code
Public Class ApplicationDbContext Inherits IdentityDbContext(Of ApplicationUser) Public Sub New() MyBase.New("DefaultConnection") End Sub End Class
The code after editing:
C#csharp Copy Code
public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("DefaultConnection") { } static ApplicationDbContext() { var config = Devart.Data.Oracle.Entity.Configuration.OracleEntityProviderConfig.Instance; config.CodeFirstOptions.TruncateLongDefaultNames = true; Database.SetInitializer(new MigrateDatabaseToLatestVersion<ApplicationDbContext, Configuration>()); } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder .Properties() .Where(p => p.PropertyType == typeof(string) && !p.Name.Contains("Id") && !p.Name.Contains("Provider")) .Configure(p => p.HasMaxLength(256)); } }
Visual Basic Copy Code
Public Class ApplicationDbContext Inherits IdentityDbContext(Of ApplicationUser) Public Sub New() MyBase.New("DefaultConnection") End Sub Shared Sub New() Dim config = Devart.Data.Oracle.Entity.Configuration.OracleEntityProviderConfig.Instance config.CodeFirstOptions.TruncateLongDefaultNames = True Database.SetInitializer(New MigrateDatabaseToLatestVersion(Of ApplicationDbContext, Configuration)()) End Sub Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder) MyBase.OnModelCreating(modelBuilder) modelBuilder.Properties().Where(Function(p) p.PropertyType = GetType(String) AndAlso Not _ p.Name.Contains("Id") AndAlso Not p.Name.Contains("Provider")).Configure(Function(p) _ p.HasMaxLength(256)) End Sub End Class
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 on the top of the page.
- Enter a new user name and password to the corresponding boxes and click the Register button.
- The new user is now registered and logged in, and the ASP.NET Identity tables are created in the Oracle database.
- 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.
See Also
Using ADO.NET Implementation of ASP.NET Identity 1 for Oracle | Using Entity Framework Implementation of ASP.NET Identity 2 for Oracle | Using ADO.NET Implementation of ASP.NET Identity 2 for Oracle | Using Entity Framework Core Implementation of ASP.NET Core Identity for Oracle