Blazor Authentication using SQLite

By default, the current Blazor template uses SQL Server localdb for storing authentication data. You have to do some change when you want to use sqlite instead. Please note that the belows step applies only to new project that just generated by template, where there is no db migration done yet.

Steps Outline:

  1. Update Nuget package
  2. Modify Startup.cs and connection string
  3. Modify Database Migration code

 

Step 1:

Add Nuget “Microsoft.EntityFrameworkCore.Sqlite” and remove “Microsoft.EntityFrameworkCore.SqlServer”

 

Step 2:

In Startup.cs, change “UseSqlServer” to “UseSqlite

public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext(options =>
options.UseSqlite(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity()
.AddEntityFrameworkStores();
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddScoped<AuthenticationStateProvider, RevalidatingAuthenticationStateProvider>();
services.AddSingleton();
}

Then modify the connection string in appsetting.json to something like “DataSource=<yourdbname>.db” which point to your sqlite database file.

After Step 2, you may try to build the project. You will get errors in a file “00000000000000_CreateIdentitySchema.cs”, this is in fact the initial database migration action for authentication. We should proceed to step 3 to modify source code.

 

Step 3:

In file “00000000000000_CreateIdentitySchema.cs“, Change all the occurrance of

.HasAnnotation(“SqlServer:ValueGenerationStrategy”, SqlServerValueGenerationStrategy.IdentityColumn)” or “.Annotation(“SqlServer:ValueGenerationStrategy”, SqlServerValueGenerationStrategy.IdentityColumn)

to “.HasAnnotation(“Sqlite:Autoincrement”, true)” or “.Annotation(“Sqlite:Autoincrement”, true)” respectively.

 

After all above steps, you should be able to build the project without error. Then start the app, try to register a new user. It will prompt you to apply first database migration.

Windows Hook & DLL Injection

MSDN Introduction:

Hooks Overview

Using Hooks

Win32 Hook API:

SetWindowsHookEx

RegisterShellHookWindow

SetWinEventHook

CreateRemoteThread

LoadLibrary

Reference articles:

API hooking revealed

Three Ways to Inject Your Code into Another Process

Hooks and DLLs

Hooking the native API and controlling process creation on a system-wide basis

Global Interceptable Program and System Hooks in .NET

ASP.NET Core SignalR

You need these two NuGet packages:

  1. Microsoft.AspNetCore.SignalR.Server
  2. Microsoft.AspNetCore.WebSockets

 

However, I cannot find the first one on Nuget Package Manager. Finally I download it from web:

https://dotnet.myget.org/feed/aspnetcore-master/package/nuget/Microsoft.AspNetCore.SignalR.Server

and then add a local package source in “Nuget Package Manager” to install it.

Then I follow this guide for a simple demo:

https://radu-matei.github.io/blog/signalr-core/

 
Indepth SignalR:

INSIDE SIGNALR – ADDRESSING CLIENTS, RETURN VALUES, BROKEN CONNECTIONS AND SECURITY
 

 

 

 

Starting a Angular 2 project, with ASP.NET Core in VS2017

Prerequistes:

  1. Visual Studio 2017 (you need to install workloads “ASP.NET and web development” and “.NET Core cross-platform development”(not sure…))
  2. Node.js version 6 or later

Please note that you need Visual Studio 2017 release version. This version of Visual Studio include .NET core version 1.0.0. To verify, you can type the following command in command prompt:

dotnet --version

FIRST, you should install the Microsoft ASP.NET Core SPA (Single Page Application) templates. Type the following command in command prompt:

dotnet new --install Microsoft.AspNetCore.SpaTemplates::*

Then, use the following command, a template project will be created:

dotnet new angular

After that, you can use Visual Studio 2017 to open the .csproj file. The project can be compiled and run immediately without any modification.

EF Core with SQLite in VS2017

This post is about using Entity Framework Core to access SQLite database in Visual Studio 2017. I will demonstrate the Database First approach.

 

FIRST, Install the following Nuget packages in your project:

  1. “Microsoft.EntityFrameworkCore.SQLite” (SQLite provider)
  2. “Microsoft.EntityFrameworkCore.Tools” (including”Scaffold-DbContext” and other db commands)
  3. “Microsoft.EntityFrameworkCore.Sqlite.Design” (the “Scaffold-DbContext” need this to generate class for SQLite database)

 

SECOND, Open “Nuget Package Manager Console”. Type the following command

<pre>

Scaffold-DbContext "Datasource=C:\ASPNET\APIApp\webbookmarks.db3" Microsoft.EntityFrameworkCore.SQLite -OutputDir Models

</pre>

After that you should find a XXXContext.cs and other class files representing your data model in the project folder.

 

THIRD, Then add these class files to your project using “Add Existing Items…” in your project menu.

Please note that your database connection string input in the Scaffold-DbContext command will be hard-coded in the datacontext.cs file as follows:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
optionsBuilder.UseSqlite(@"Datasource=C:\Users\bliss\Desktop\bible web\biblebliss\bible.db");
}

 

Finally, you can use use the following code to access the SQLite database:


var = new dbcontext();

var versionshortnames = db.Version.Select(v => v.VersionShort).AsEnumerable();

 

References:

https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.SQLite/
https://docs.microsoft.com/en-us/ef/core/providers/sqlite/