Monday, January 1, 2018

Asp.Net Identity SQLite Provider on a Web Forms project

  Mostly, the tutorial or sample I found from googling was for MVC and C#. So I decided to install the Asp.Net.Identity.SQLite by following the guide from MVC then modify it to work for Web Forms then convert it to VB.

I'm using Visual Studio 2017.

Create a new ASP.NET Web Forms Site project, targeting .Net Framework 4.7.1




Install Nuget Package AspNet.Identity.SQLite


ok, and wait till finish installing



The Uninstall the Microsoft.AspNet.Identity.EntityFramework



Replace all text  Microsoft.AspNet.Identity.EntityFramework with AspNet.Identity.SQLite for the Current Project.


It should have 5 occurrence replaced

Create a new Sqlite database name Identity.db into the App_Data folder for the current project


Open a Query Editor for the Sqlite database, the run the sql query.
You can get the query from the package folder inside your solution folder "AspNet.Identity.SQLite.1.0.6352.28669\content\SQLiteIdentity.sql"

Execute the query



In the IdentityModels.vb, replace the whole ApplicationDbContext class:

Public Class ApplicationDbContext
    Inherits IdentityDbContext(Of ApplicationUser)

    Public Sub New()

        MyBase.New("DefaultConnection")

    End Sub

End Class

with this:

Public Class ApplicationDbContext
    Inherits SQLiteDatabase
    Public Sub New()
        MyBase.New("DefaultConnection")
    End Sub
End Class


The replace the UserManager class

Public Class UserManager
    Inherits UserManager(Of ApplicationUser)
    Public Sub New()
        MyBase.New(New UserStore(Of ApplicationUser)(New ApplicationDbContext()))
    End Sub
End Class

with this:

Public Class UserManager
    Inherits UserManager(Of ApplicationUser)
    Public Sub New()
        MyBase.New(New UserStore(Of ApplicationUser, IdentityRole)(New ApplicationDbContext()))
    End Sub
End Class

  Lastly, replace the default connectionstring in the web.config with this


  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=|DataDirectory|\Identity.db;Version=3;" providerName="System.Data.SQLite"/>
  </connectionStrings>

Done..

But, in case of error releted tu SQLite.Interop.dll when running, try following this step
Unable to load DLL 'SQLite.Interop.dll': The specified module could not be found










No comments:

Post a Comment