Create database initial migration from current DTO model

This commit is contained in:
2026-01-30 22:19:33 +01:00
parent 6df1ca6d8f
commit 846bb29914
19 changed files with 261 additions and 579 deletions

5
.gitignore vendored
View File

@@ -455,4 +455,7 @@ test-results/
.vscode/** .vscode/**
stripe*.json stripe*.json
stripe.exe stripe.exe
# Sqlite database
BlazorPolicyAuth/blazorpolicyauth.db

View File

@@ -1,4 +1,7 @@
using BlazorPolicyAuth.Models.ViewModels; using System.Net.Http;
using System.Net.Http.Json;
using System.Threading.Tasks;
using BlazorPolicyAuth.Models.ViewModels;
using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.Components.Authorization;
namespace BlazorPolicyAuth.App.Services.AuthService; namespace BlazorPolicyAuth.App.Services.AuthService;

View File

@@ -1,4 +1,5 @@
using BlazorPolicyAuth.Models.ViewModels; using System.Threading.Tasks;
using BlazorPolicyAuth.Models.ViewModels;
namespace BlazorPolicyAuth.App.Services.AuthService; namespace BlazorPolicyAuth.App.Services.AuthService;

View File

@@ -1,5 +1,6 @@
@page "/logout" @page "/logout"
@using Microsoft.AspNetCore.Authentication @using Microsoft.AspNetCore.Authentication
@using Microsoft.AspNetCore.Http
@inject NavigationManager NavigationManager @inject NavigationManager NavigationManager
<div class="row"> <div class="row">

View File

@@ -1,5 +1,6 @@
@page "/Error" @page "/Error"
@using System.Diagnostics @using System.Diagnostics
@using Microsoft.AspNetCore.Http
<PageTitle>Error</PageTitle> <PageTitle>Error</PageTitle>

View File

@@ -10,49 +10,49 @@ public class AppDbContext(DbContextOptions dbContextOptions) : DbContext(dbConte
{ {
base.OnModelCreating(modelBuilder); base.OnModelCreating(modelBuilder);
var demoUserAccounts = new UserAccount[] // var demoUserAccounts = new UserAccount[]
{ // {
new() {Id = 1, UserName = "user1", Password = "user1"}, // new() {Id = 1, Email = "user1", Password = "user1"},
new() {Id = 2, UserName = "user2", Password = "user2"}, // new() {Id = 2, Email = "user2", Password = "user2"},
new() {Id = 3, UserName = "user3", Password = "user3"}, // new() {Id = 3, Email = "user3", Password = "user3"},
new() {Id = 4, UserName = "user4", Password = "user4"}, // new() {Id = 4, Email = "user4", Password = "user4"},
new() {Id = 5, UserName = "user5", Password = "user5"}, // new() {Id = 5, Email = "user5", Password = "user5"},
}; // };
modelBuilder.Entity<UserAccount>().HasData(demoUserAccounts); // modelBuilder.Entity<UserAccount>().HasData(demoUserAccounts);
//
var demoUserAccountPolicies = new UserAccountPolicy[] // var demoUserAccountPolicies = new UserAccountPolicy[]
{ // {
/* User 1 Policies */ // /* User 1 Policies */
new() {Id = 1, UserAccountId = 1, UserPolicy = UserPolicy.VIEW_PRODUCT, IsEnabled = false}, // new() {Id = 1, UserAccountId = 1, UserPolicy = UserPolicy.VIEW_PRODUCT, IsEnabled = false},
new() {Id = 2, UserAccountId = 1, UserPolicy = UserPolicy.ADD_PRODUCT, IsEnabled = false}, // new() {Id = 2, UserAccountId = 1, UserPolicy = UserPolicy.ADD_PRODUCT, IsEnabled = false},
new() {Id = 3, UserAccountId = 1, UserPolicy = UserPolicy.EDIT_PRODUCT, IsEnabled = false}, // new() {Id = 3, UserAccountId = 1, UserPolicy = UserPolicy.EDIT_PRODUCT, IsEnabled = false},
new() {Id = 4, UserAccountId = 1, UserPolicy = UserPolicy.DELETE_PRODUCT, IsEnabled = false}, // new() {Id = 4, UserAccountId = 1, UserPolicy = UserPolicy.DELETE_PRODUCT, IsEnabled = false},
//
/* User 2 Policies */ // /* User 2 Policies */
new() {Id = 5, UserAccountId = 2, UserPolicy = UserPolicy.VIEW_PRODUCT, IsEnabled = true}, // new() {Id = 5, UserAccountId = 2, UserPolicy = UserPolicy.VIEW_PRODUCT, IsEnabled = true},
new() {Id = 6, UserAccountId = 2, UserPolicy = UserPolicy.ADD_PRODUCT, IsEnabled = false}, // new() {Id = 6, UserAccountId = 2, UserPolicy = UserPolicy.ADD_PRODUCT, IsEnabled = false},
new() {Id = 7, UserAccountId = 2, UserPolicy = UserPolicy.EDIT_PRODUCT, IsEnabled = false}, // new() {Id = 7, UserAccountId = 2, UserPolicy = UserPolicy.EDIT_PRODUCT, IsEnabled = false},
new() {Id = 8, UserAccountId = 2, UserPolicy = UserPolicy.DELETE_PRODUCT, IsEnabled = false}, // new() {Id = 8, UserAccountId = 2, UserPolicy = UserPolicy.DELETE_PRODUCT, IsEnabled = false},
//
/* User 3 Policies */ // /* User 3 Policies */
new() {Id = 9, UserAccountId = 3, UserPolicy = UserPolicy.VIEW_PRODUCT, IsEnabled = true}, // new() {Id = 9, UserAccountId = 3, UserPolicy = UserPolicy.VIEW_PRODUCT, IsEnabled = true},
new() {Id = 10, UserAccountId = 3, UserPolicy = UserPolicy.ADD_PRODUCT, IsEnabled = true}, // new() {Id = 10, UserAccountId = 3, UserPolicy = UserPolicy.ADD_PRODUCT, IsEnabled = true},
new() {Id = 11, UserAccountId = 3, UserPolicy = UserPolicy.EDIT_PRODUCT, IsEnabled = false}, // new() {Id = 11, UserAccountId = 3, UserPolicy = UserPolicy.EDIT_PRODUCT, IsEnabled = false},
new() {Id = 12, UserAccountId = 3, UserPolicy = UserPolicy.DELETE_PRODUCT, IsEnabled = false}, // new() {Id = 12, UserAccountId = 3, UserPolicy = UserPolicy.DELETE_PRODUCT, IsEnabled = false},
//
/* User 4 Policies */ // /* User 4 Policies */
new() {Id = 13, UserAccountId = 4, UserPolicy = UserPolicy.VIEW_PRODUCT, IsEnabled = true}, // new() {Id = 13, UserAccountId = 4, UserPolicy = UserPolicy.VIEW_PRODUCT, IsEnabled = true},
new() {Id = 14, UserAccountId = 4, UserPolicy = UserPolicy.ADD_PRODUCT, IsEnabled = true}, // new() {Id = 14, UserAccountId = 4, UserPolicy = UserPolicy.ADD_PRODUCT, IsEnabled = true},
new() {Id = 15, UserAccountId = 4, UserPolicy = UserPolicy.EDIT_PRODUCT, IsEnabled = true}, // new() {Id = 15, UserAccountId = 4, UserPolicy = UserPolicy.EDIT_PRODUCT, IsEnabled = true},
new() {Id = 16, UserAccountId = 4, UserPolicy = UserPolicy.DELETE_PRODUCT, IsEnabled = false}, // new() {Id = 16, UserAccountId = 4, UserPolicy = UserPolicy.DELETE_PRODUCT, IsEnabled = false},
//
/* User 5 Policies */ // /* User 5 Policies */
new() {Id = 17, UserAccountId = 5, UserPolicy = UserPolicy.VIEW_PRODUCT, IsEnabled = true}, // new() {Id = 17, UserAccountId = 5, UserPolicy = UserPolicy.VIEW_PRODUCT, IsEnabled = true},
new() {Id = 18, UserAccountId = 5, UserPolicy = UserPolicy.ADD_PRODUCT, IsEnabled = true}, // new() {Id = 18, UserAccountId = 5, UserPolicy = UserPolicy.ADD_PRODUCT, IsEnabled = true},
new() {Id = 19, UserAccountId = 5, UserPolicy = UserPolicy.EDIT_PRODUCT, IsEnabled = true}, // new() {Id = 19, UserAccountId = 5, UserPolicy = UserPolicy.EDIT_PRODUCT, IsEnabled = true},
new() {Id = 20, UserAccountId = 5, UserPolicy = UserPolicy.DELETE_PRODUCT, IsEnabled = true}, // new() {Id = 20, UserAccountId = 5, UserPolicy = UserPolicy.DELETE_PRODUCT, IsEnabled = true},
}; // };
modelBuilder.Entity<UserAccountPolicy>().HasData(demoUserAccountPolicies); // modelBuilder.Entity<UserAccountPolicy>().HasData(demoUserAccountPolicies);
} }
public DbSet<UserAccount> UserAccounts { get; set; } public DbSet<UserAccount> UserAccounts { get; set; }

View File

@@ -1,5 +1,11 @@
using Microsoft.AspNetCore.Hosting.Server; using System;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features; using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.Extensions.Hosting;
namespace BlazorPolicyAuth; namespace BlazorPolicyAuth;
/// <summary> /// <summary>

View File

@@ -1,244 +0,0 @@
// <auto-generated />
using BlazorPolicyAuth.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace BlazorPolicyAuth.Migrations
{
[DbContext(typeof(AppDbContext))]
[Migration("20251215131042_user account with policies")]
partial class useraccountwithpolicies
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "10.0.1");
modelBuilder.Entity("BlazorPolicyAuth.Models.Entities.UserAccount", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER")
.HasColumnName("id");
b.Property<string>("Password")
.HasMaxLength(100)
.HasColumnType("TEXT")
.HasColumnName("password");
b.Property<string>("UserName")
.HasMaxLength(100)
.HasColumnType("TEXT")
.HasColumnName("user_name");
b.HasKey("Id");
b.ToTable("user_account");
b.HasData(
new
{
Id = 1,
Password = "user1",
UserName = "user1"
},
new
{
Id = 2,
Password = "user2",
UserName = "user2"
},
new
{
Id = 3,
Password = "user3",
UserName = "user3"
},
new
{
Id = 4,
Password = "user4",
UserName = "user4"
},
new
{
Id = 5,
Password = "user5",
UserName = "user5"
});
});
modelBuilder.Entity("BlazorPolicyAuth.Models.Entities.UserAccountPolicy", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER")
.HasColumnName("id");
b.Property<bool>("IsEnabled")
.HasColumnType("INTEGER")
.HasColumnName("is_enabled");
b.Property<int>("UserAccountId")
.HasColumnType("INTEGER")
.HasColumnName("user_account_policy");
b.Property<string>("UserPolicy")
.HasColumnType("TEXT")
.HasColumnName("user_policy");
b.HasKey("Id");
b.ToTable("user_account_policy");
b.HasData(
new
{
Id = 1,
IsEnabled = false,
UserAccountId = 1,
UserPolicy = "VIEW_PRODUCT"
},
new
{
Id = 2,
IsEnabled = false,
UserAccountId = 1,
UserPolicy = "ADD_PRODUCT"
},
new
{
Id = 3,
IsEnabled = false,
UserAccountId = 1,
UserPolicy = "EDIT_PRODUCT"
},
new
{
Id = 4,
IsEnabled = false,
UserAccountId = 1,
UserPolicy = "DELETE_PRODUCT"
},
new
{
Id = 5,
IsEnabled = true,
UserAccountId = 2,
UserPolicy = "VIEW_PRODUCT"
},
new
{
Id = 6,
IsEnabled = false,
UserAccountId = 2,
UserPolicy = "ADD_PRODUCT"
},
new
{
Id = 7,
IsEnabled = false,
UserAccountId = 2,
UserPolicy = "EDIT_PRODUCT"
},
new
{
Id = 8,
IsEnabled = false,
UserAccountId = 2,
UserPolicy = "DELETE_PRODUCT"
},
new
{
Id = 9,
IsEnabled = true,
UserAccountId = 3,
UserPolicy = "VIEW_PRODUCT"
},
new
{
Id = 10,
IsEnabled = true,
UserAccountId = 3,
UserPolicy = "ADD_PRODUCT"
},
new
{
Id = 11,
IsEnabled = false,
UserAccountId = 3,
UserPolicy = "EDIT_PRODUCT"
},
new
{
Id = 12,
IsEnabled = false,
UserAccountId = 3,
UserPolicy = "DELETE_PRODUCT"
},
new
{
Id = 13,
IsEnabled = true,
UserAccountId = 4,
UserPolicy = "VIEW_PRODUCT"
},
new
{
Id = 14,
IsEnabled = true,
UserAccountId = 4,
UserPolicy = "ADD_PRODUCT"
},
new
{
Id = 15,
IsEnabled = true,
UserAccountId = 4,
UserPolicy = "EDIT_PRODUCT"
},
new
{
Id = 16,
IsEnabled = false,
UserAccountId = 4,
UserPolicy = "DELETE_PRODUCT"
},
new
{
Id = 17,
IsEnabled = true,
UserAccountId = 5,
UserPolicy = "VIEW_PRODUCT"
},
new
{
Id = 18,
IsEnabled = true,
UserAccountId = 5,
UserPolicy = "ADD_PRODUCT"
},
new
{
Id = 19,
IsEnabled = true,
UserAccountId = 5,
UserPolicy = "EDIT_PRODUCT"
},
new
{
Id = 20,
IsEnabled = true,
UserAccountId = 5,
UserPolicy = "DELETE_PRODUCT"
});
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -1,94 +0,0 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional
namespace BlazorPolicyAuth.Migrations
{
/// <inheritdoc />
public partial class useraccountwithpolicies : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "user_account",
columns: table => new
{
id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
user_name = table.Column<string>(type: "TEXT", maxLength: 100, nullable: true),
password = table.Column<string>(type: "TEXT", maxLength: 100, nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_user_account", x => x.id);
});
migrationBuilder.CreateTable(
name: "user_account_policy",
columns: table => new
{
id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
user_account_policy = table.Column<int>(type: "INTEGER", nullable: false),
user_policy = table.Column<string>(type: "TEXT", nullable: true),
is_enabled = table.Column<bool>(type: "INTEGER", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_user_account_policy", x => x.id);
});
migrationBuilder.InsertData(
table: "user_account",
columns: new[] { "id", "password", "user_name" },
values: new object[,]
{
{ 1, "user1", "user1" },
{ 2, "user2", "user2" },
{ 3, "user3", "user3" },
{ 4, "user4", "user4" },
{ 5, "user5", "user5" }
});
migrationBuilder.InsertData(
table: "user_account_policy",
columns: new[] { "id", "is_enabled", "user_account_policy", "user_policy" },
values: new object[,]
{
{ 1, false, 1, "VIEW_PRODUCT" },
{ 2, false, 1, "ADD_PRODUCT" },
{ 3, false, 1, "EDIT_PRODUCT" },
{ 4, false, 1, "DELETE_PRODUCT" },
{ 5, true, 2, "VIEW_PRODUCT" },
{ 6, false, 2, "ADD_PRODUCT" },
{ 7, false, 2, "EDIT_PRODUCT" },
{ 8, false, 2, "DELETE_PRODUCT" },
{ 9, true, 3, "VIEW_PRODUCT" },
{ 10, true, 3, "ADD_PRODUCT" },
{ 11, false, 3, "EDIT_PRODUCT" },
{ 12, false, 3, "DELETE_PRODUCT" },
{ 13, true, 4, "VIEW_PRODUCT" },
{ 14, true, 4, "ADD_PRODUCT" },
{ 15, true, 4, "EDIT_PRODUCT" },
{ 16, false, 4, "DELETE_PRODUCT" },
{ 17, true, 5, "VIEW_PRODUCT" },
{ 18, true, 5, "ADD_PRODUCT" },
{ 19, true, 5, "EDIT_PRODUCT" },
{ 20, true, 5, "DELETE_PRODUCT" }
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "user_account");
migrationBuilder.DropTable(
name: "user_account_policy");
}
}
}

View File

@@ -0,0 +1,86 @@
// <auto-generated />
using System;
using BlazorPolicyAuth.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace BlazorPolicyAuth.Migrations
{
[DbContext(typeof(AppDbContext))]
[Migration("20260130211227_createUserAccount")]
partial class createUserAccount
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "10.0.1");
modelBuilder.Entity("BlazorPolicyAuth.Models.Entities.UserAccount", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER")
.HasColumnName("id");
b.Property<DateTime>("DateCreated")
.HasColumnType("TEXT");
b.Property<string>("Email")
.HasMaxLength(200)
.HasColumnType("TEXT")
.HasColumnName("email");
b.Property<string>("Password")
.HasMaxLength(100)
.HasColumnType("TEXT")
.HasColumnName("password");
b.Property<byte[]>("PasswordHash")
.IsRequired()
.HasColumnType("BLOB");
b.Property<byte[]>("PasswordSalt")
.IsRequired()
.HasColumnType("BLOB");
b.Property<string>("Role")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("user_account");
});
modelBuilder.Entity("BlazorPolicyAuth.Models.Entities.UserAccountPolicy", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER")
.HasColumnName("id");
b.Property<bool>("IsEnabled")
.HasColumnType("INTEGER")
.HasColumnName("is_enabled");
b.Property<int>("UserAccountId")
.HasColumnType("INTEGER")
.HasColumnName("user_account_policy");
b.Property<string>("UserPolicy")
.HasColumnType("TEXT")
.HasColumnName("user_policy");
b.HasKey("Id");
b.ToTable("user_account_policy");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,58 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace BlazorPolicyAuth.Migrations
{
/// <inheritdoc />
public partial class createUserAccount : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "user_account",
columns: table => new
{
id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
email = table.Column<string>(type: "TEXT", maxLength: 200, nullable: true),
password = table.Column<string>(type: "TEXT", maxLength: 100, nullable: true),
PasswordHash = table.Column<byte[]>(type: "BLOB", nullable: false),
PasswordSalt = table.Column<byte[]>(type: "BLOB", nullable: false),
DateCreated = table.Column<DateTime>(type: "TEXT", nullable: false),
Role = table.Column<string>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_user_account", x => x.id);
});
migrationBuilder.CreateTable(
name: "user_account_policy",
columns: table => new
{
id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
user_account_policy = table.Column<int>(type: "INTEGER", nullable: false),
user_policy = table.Column<string>(type: "TEXT", nullable: true),
is_enabled = table.Column<bool>(type: "INTEGER", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_user_account_policy", x => x.id);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "user_account");
migrationBuilder.DropTable(
name: "user_account_policy");
}
}
}

View File

@@ -1,4 +1,5 @@
// <auto-generated /> // <auto-generated />
using System;
using BlazorPolicyAuth.Data; using BlazorPolicyAuth.Data;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Infrastructure;
@@ -23,51 +24,34 @@ namespace BlazorPolicyAuth.Migrations
.HasColumnType("INTEGER") .HasColumnType("INTEGER")
.HasColumnName("id"); .HasColumnName("id");
b.Property<DateTime>("DateCreated")
.HasColumnType("TEXT");
b.Property<string>("Email")
.HasMaxLength(200)
.HasColumnType("TEXT")
.HasColumnName("email");
b.Property<string>("Password") b.Property<string>("Password")
.HasMaxLength(100) .HasMaxLength(100)
.HasColumnType("TEXT") .HasColumnType("TEXT")
.HasColumnName("password"); .HasColumnName("password");
b.Property<string>("UserName") b.Property<byte[]>("PasswordHash")
.HasMaxLength(100) .IsRequired()
.HasColumnType("TEXT") .HasColumnType("BLOB");
.HasColumnName("user_name");
b.Property<byte[]>("PasswordSalt")
.IsRequired()
.HasColumnType("BLOB");
b.Property<string>("Role")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id"); b.HasKey("Id");
b.ToTable("user_account"); b.ToTable("user_account");
b.HasData(
new
{
Id = 1,
Password = "user1",
UserName = "user1"
},
new
{
Id = 2,
Password = "user2",
UserName = "user2"
},
new
{
Id = 3,
Password = "user3",
UserName = "user3"
},
new
{
Id = 4,
Password = "user4",
UserName = "user4"
},
new
{
Id = 5,
Password = "user5",
UserName = "user5"
});
}); });
modelBuilder.Entity("BlazorPolicyAuth.Models.Entities.UserAccountPolicy", b => modelBuilder.Entity("BlazorPolicyAuth.Models.Entities.UserAccountPolicy", b =>
@@ -92,148 +76,6 @@ namespace BlazorPolicyAuth.Migrations
b.HasKey("Id"); b.HasKey("Id");
b.ToTable("user_account_policy"); b.ToTable("user_account_policy");
b.HasData(
new
{
Id = 1,
IsEnabled = false,
UserAccountId = 1,
UserPolicy = "VIEW_PRODUCT"
},
new
{
Id = 2,
IsEnabled = false,
UserAccountId = 1,
UserPolicy = "ADD_PRODUCT"
},
new
{
Id = 3,
IsEnabled = false,
UserAccountId = 1,
UserPolicy = "EDIT_PRODUCT"
},
new
{
Id = 4,
IsEnabled = false,
UserAccountId = 1,
UserPolicy = "DELETE_PRODUCT"
},
new
{
Id = 5,
IsEnabled = true,
UserAccountId = 2,
UserPolicy = "VIEW_PRODUCT"
},
new
{
Id = 6,
IsEnabled = false,
UserAccountId = 2,
UserPolicy = "ADD_PRODUCT"
},
new
{
Id = 7,
IsEnabled = false,
UserAccountId = 2,
UserPolicy = "EDIT_PRODUCT"
},
new
{
Id = 8,
IsEnabled = false,
UserAccountId = 2,
UserPolicy = "DELETE_PRODUCT"
},
new
{
Id = 9,
IsEnabled = true,
UserAccountId = 3,
UserPolicy = "VIEW_PRODUCT"
},
new
{
Id = 10,
IsEnabled = true,
UserAccountId = 3,
UserPolicy = "ADD_PRODUCT"
},
new
{
Id = 11,
IsEnabled = false,
UserAccountId = 3,
UserPolicy = "EDIT_PRODUCT"
},
new
{
Id = 12,
IsEnabled = false,
UserAccountId = 3,
UserPolicy = "DELETE_PRODUCT"
},
new
{
Id = 13,
IsEnabled = true,
UserAccountId = 4,
UserPolicy = "VIEW_PRODUCT"
},
new
{
Id = 14,
IsEnabled = true,
UserAccountId = 4,
UserPolicy = "ADD_PRODUCT"
},
new
{
Id = 15,
IsEnabled = true,
UserAccountId = 4,
UserPolicy = "EDIT_PRODUCT"
},
new
{
Id = 16,
IsEnabled = false,
UserAccountId = 4,
UserPolicy = "DELETE_PRODUCT"
},
new
{
Id = 17,
IsEnabled = true,
UserAccountId = 5,
UserPolicy = "VIEW_PRODUCT"
},
new
{
Id = 18,
IsEnabled = true,
UserAccountId = 5,
UserPolicy = "ADD_PRODUCT"
},
new
{
Id = 19,
IsEnabled = true,
UserAccountId = 5,
UserPolicy = "EDIT_PRODUCT"
},
new
{
Id = 20,
IsEnabled = true,
UserAccountId = 5,
UserPolicy = "DELETE_PRODUCT"
});
}); });
#pragma warning restore 612, 618 #pragma warning restore 612, 618
} }

View File

@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations; using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema; using System.ComponentModel.DataAnnotations.Schema;
namespace BlazorPolicyAuth.Models.Entities; namespace BlazorPolicyAuth.Models.Entities;
@@ -11,16 +12,17 @@ public class UserAccount
[Column("id")] [Column("id")]
public int Id { get; set; } public int Id { get; set; }
[Column("user_name")] [Column("email")]
[MaxLength(100)] [MaxLength(200)]
public string? UserName { get; set; } public string? Email { get; set; }
[Column("password")] [Column("password")]
[MaxLength(100)] [MaxLength(100)]
public string? Password { get; set; } public string? Password { get; set; }
public byte[] PasswordHash { get; set; } public byte[] PasswordHash { get; set; } = [];
public byte[] PasswordSalt { get; set; } public byte[] PasswordSalt { get; set; } = [];
public DateTime DateCreated { get; set; } = DateTime.Now; public DateTime DateCreated { get; set; } = DateTime.Now;
public string Role { get; set; } = "User"; public string Role { get; set; } = "User";
} }

View File

@@ -1,10 +1,16 @@
using System;
using System.Net.Http;
using BlazorPolicyAuth; using BlazorPolicyAuth;
using BlazorPolicyAuth.Components; using BlazorPolicyAuth.Components;
using BlazorPolicyAuth.Data; using BlazorPolicyAuth.Data;
using BlazorPolicyAuth.Models.ViewModels; using BlazorPolicyAuth.Models.ViewModels;
using BlazorPolicyAuth.Services.AuthService; using BlazorPolicyAuth.Services.AuthService;
using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using ClientServices = BlazorPolicyAuth.App.Services; using ClientServices = BlazorPolicyAuth.App.Services;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);

View File

@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using BlazorPolicyAuth.Data; using BlazorPolicyAuth.Data;
using BlazorPolicyAuth.Models.Entities; using BlazorPolicyAuth.Models.Entities;
using BlazorPolicyAuth.Models.ViewModels; using BlazorPolicyAuth.Models.ViewModels;
@@ -9,6 +11,9 @@ using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims; using System.Security.Claims;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
namespace BlazorPolicyAuth.Services.AuthService; namespace BlazorPolicyAuth.Services.AuthService;
@@ -25,7 +30,7 @@ public class AuthService(AppDbContext context, IConfiguration configuration, IHt
CreatePasswordHash(request.Password, out byte[] passwordHash, out byte[] passwordSalt); CreatePasswordHash(request.Password, out byte[] passwordHash, out byte[] passwordSalt);
var user = new UserAccount var user = new UserAccount
{ {
UserName = request.Email, Email = request.Email,
PasswordHash = passwordHash, PasswordHash = passwordHash,
PasswordSalt = passwordSalt PasswordSalt = passwordSalt
}; };
@@ -38,7 +43,7 @@ public class AuthService(AppDbContext context, IConfiguration configuration, IHt
public async Task<bool> UserExists(string email) public async Task<bool> UserExists(string email)
{ {
if (await context.UserAccounts.AnyAsync(user => user.UserName.ToLower().Equals(email.ToLower()))) if (await context.UserAccounts.AnyAsync(user => user.Email.ToLower().Equals(email.ToLower())))
{ {
return true; return true;
} }
@@ -48,13 +53,13 @@ public class AuthService(AppDbContext context, IConfiguration configuration, IHt
public async Task<ServiceResponse<string>> Login(string email, string password) public async Task<ServiceResponse<string>> Login(string email, string password)
{ {
var response = new ServiceResponse<string>(); var response = new ServiceResponse<string>();
var user = await context.UserAccounts.FirstOrDefaultAsync(u => u.UserName.ToLower().Equals(email.ToLower())); var user = await context.UserAccounts.FirstOrDefaultAsync(u => u.Email.ToLower().Equals(email.ToLower()));
if (user == null) if (user == null)
{ {
response.Success = false; response.Success = false;
response.Message = "User not found."; response.Message = "User not found.";
} }
else if (!VeriyPasswordHash(password, user.PasswordHash, user.PasswordSalt)) else if (!VerifyPasswordHash(password, user.PasswordHash, user.PasswordSalt))
{ {
response.Success = false; response.Success = false;
response.Message = "Wrong password."; response.Message = "Wrong password.";
@@ -91,7 +96,7 @@ public class AuthService(AppDbContext context, IConfiguration configuration, IHt
public async Task<UserAccount> GetUserByEmail(string email) public async Task<UserAccount> GetUserByEmail(string email)
{ {
return await context.UserAccounts.FirstOrDefaultAsync(u => u.UserName.Equals(email)); return await context.UserAccounts.FirstOrDefaultAsync(u => u.Email.Equals(email));
} }
private static void CreatePasswordHash(string password, out byte[] passwordHash, out byte[] passwordSalt) private static void CreatePasswordHash(string password, out byte[] passwordHash, out byte[] passwordSalt)
@@ -101,7 +106,7 @@ public class AuthService(AppDbContext context, IConfiguration configuration, IHt
passwordHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(password)); passwordHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(password));
} }
private static bool VeriyPasswordHash(string password, byte[] passwordHash, byte[] passwordSalt) private static bool VerifyPasswordHash(string password, byte[] passwordHash, byte[] passwordSalt)
{ {
using var hmac = new HMACSHA512(passwordSalt); using var hmac = new HMACSHA512(passwordSalt);
var computedHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(password)); var computedHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(password));
@@ -113,7 +118,7 @@ public class AuthService(AppDbContext context, IConfiguration configuration, IHt
var claims = new List<Claim> var claims = new List<Claim>
{ {
new(ClaimTypes.NameIdentifier, user.Id.ToString()), new(ClaimTypes.NameIdentifier, user.Id.ToString()),
new(ClaimTypes.Name, user.UserName), new(ClaimTypes.Name, user.Email),
new(ClaimTypes.Role, user.Role) new(ClaimTypes.Role, user.Role)
}; };

View File

@@ -1,4 +1,5 @@
using BlazorPolicyAuth.Models.Entities; using System.Threading.Tasks;
using BlazorPolicyAuth.Models.Entities;
using BlazorPolicyAuth.Models.ViewModels; using BlazorPolicyAuth.Models.ViewModels;
namespace BlazorPolicyAuth.Services.AuthService; namespace BlazorPolicyAuth.Services.AuthService;

View File

@@ -1,4 +1,6 @@
namespace BlazorPolicyAuth; using System.Collections.Generic;
namespace BlazorPolicyAuth;
public class UserPolicy public class UserPolicy
{ {

View File

@@ -9,5 +9,8 @@
"Microsoft.AspNetCore": "Warning" "Microsoft.AspNetCore": "Warning"
} }
}, },
"AllowedHosts": "*" "AllowedHosts": "*",
"AppSettings" : {
"Token": "my top secret key my top secret key my top secret key my top secret key my top secret key my top secret key my top secret key my top secret key my top secret key my top secret key my top secret key my top secret key"
}
} }

Binary file not shown.