diff --git a/.gitignore b/.gitignore index 1606c75..cd4b611 100644 --- a/.gitignore +++ b/.gitignore @@ -455,4 +455,7 @@ test-results/ .vscode/** stripe*.json -stripe.exe \ No newline at end of file +stripe.exe + +# Sqlite database +BlazorPolicyAuth/blazorpolicyauth.db \ No newline at end of file diff --git a/BlazorPolicyAuth/App.Services/AuthService/AuthService.cs b/BlazorPolicyAuth/App.Services/AuthService/AuthService.cs index f0c687a..b0c7a7b 100644 --- a/BlazorPolicyAuth/App.Services/AuthService/AuthService.cs +++ b/BlazorPolicyAuth/App.Services/AuthService/AuthService.cs @@ -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; namespace BlazorPolicyAuth.App.Services.AuthService; diff --git a/BlazorPolicyAuth/App.Services/AuthService/IAuthService.cs b/BlazorPolicyAuth/App.Services/AuthService/IAuthService.cs index 39e3646..6e53432 100644 --- a/BlazorPolicyAuth/App.Services/AuthService/IAuthService.cs +++ b/BlazorPolicyAuth/App.Services/AuthService/IAuthService.cs @@ -1,4 +1,5 @@ -using BlazorPolicyAuth.Models.ViewModels; +using System.Threading.Tasks; +using BlazorPolicyAuth.Models.ViewModels; namespace BlazorPolicyAuth.App.Services.AuthService; diff --git a/BlazorPolicyAuth/Components/Pages/Account/Logout.razor b/BlazorPolicyAuth/Components/Pages/Account/Logout.razor index 10965f1..f6ca83a 100644 --- a/BlazorPolicyAuth/Components/Pages/Account/Logout.razor +++ b/BlazorPolicyAuth/Components/Pages/Account/Logout.razor @@ -1,5 +1,6 @@ @page "/logout" @using Microsoft.AspNetCore.Authentication +@using Microsoft.AspNetCore.Http @inject NavigationManager NavigationManager
diff --git a/BlazorPolicyAuth/Components/Pages/Error.razor b/BlazorPolicyAuth/Components/Pages/Error.razor index 576cc2d..911346d 100644 --- a/BlazorPolicyAuth/Components/Pages/Error.razor +++ b/BlazorPolicyAuth/Components/Pages/Error.razor @@ -1,5 +1,6 @@ @page "/Error" @using System.Diagnostics +@using Microsoft.AspNetCore.Http Error diff --git a/BlazorPolicyAuth/Data/AppDbContext.cs b/BlazorPolicyAuth/Data/AppDbContext.cs index cd60383..4fe526a 100644 --- a/BlazorPolicyAuth/Data/AppDbContext.cs +++ b/BlazorPolicyAuth/Data/AppDbContext.cs @@ -10,49 +10,49 @@ public class AppDbContext(DbContextOptions dbContextOptions) : DbContext(dbConte { base.OnModelCreating(modelBuilder); - var demoUserAccounts = new UserAccount[] - { - new() {Id = 1, UserName = "user1", Password = "user1"}, - new() {Id = 2, UserName = "user2", Password = "user2"}, - new() {Id = 3, UserName = "user3", Password = "user3"}, - new() {Id = 4, UserName = "user4", Password = "user4"}, - new() {Id = 5, UserName = "user5", Password = "user5"}, - }; - modelBuilder.Entity().HasData(demoUserAccounts); - - var demoUserAccountPolicies = new UserAccountPolicy[] - { - /* User 1 Policies */ - new() {Id = 1, UserAccountId = 1, UserPolicy = UserPolicy.VIEW_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 = 4, UserAccountId = 1, UserPolicy = UserPolicy.DELETE_PRODUCT, IsEnabled = false}, - - /* User 2 Policies */ - new() {Id = 5, UserAccountId = 2, UserPolicy = UserPolicy.VIEW_PRODUCT, IsEnabled = true}, - new() {Id = 6, UserAccountId = 2, UserPolicy = UserPolicy.ADD_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}, - - /* User 3 Policies */ - new() {Id = 9, UserAccountId = 3, UserPolicy = UserPolicy.VIEW_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 = 12, UserAccountId = 3, UserPolicy = UserPolicy.DELETE_PRODUCT, IsEnabled = false}, - - /* User 4 Policies */ - new() {Id = 13, UserAccountId = 4, UserPolicy = UserPolicy.VIEW_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 = 16, UserAccountId = 4, UserPolicy = UserPolicy.DELETE_PRODUCT, IsEnabled = false}, - - /* User 5 Policies */ - new() {Id = 17, UserAccountId = 5, UserPolicy = UserPolicy.VIEW_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 = 20, UserAccountId = 5, UserPolicy = UserPolicy.DELETE_PRODUCT, IsEnabled = true}, - }; - modelBuilder.Entity().HasData(demoUserAccountPolicies); + // var demoUserAccounts = new UserAccount[] + // { + // new() {Id = 1, Email = "user1", Password = "user1"}, + // new() {Id = 2, Email = "user2", Password = "user2"}, + // new() {Id = 3, Email = "user3", Password = "user3"}, + // new() {Id = 4, Email = "user4", Password = "user4"}, + // new() {Id = 5, Email = "user5", Password = "user5"}, + // }; + // modelBuilder.Entity().HasData(demoUserAccounts); + // + // var demoUserAccountPolicies = new UserAccountPolicy[] + // { + // /* User 1 Policies */ + // new() {Id = 1, UserAccountId = 1, UserPolicy = UserPolicy.VIEW_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 = 4, UserAccountId = 1, UserPolicy = UserPolicy.DELETE_PRODUCT, IsEnabled = false}, + // + // /* User 2 Policies */ + // new() {Id = 5, UserAccountId = 2, UserPolicy = UserPolicy.VIEW_PRODUCT, IsEnabled = true}, + // new() {Id = 6, UserAccountId = 2, UserPolicy = UserPolicy.ADD_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}, + // + // /* User 3 Policies */ + // new() {Id = 9, UserAccountId = 3, UserPolicy = UserPolicy.VIEW_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 = 12, UserAccountId = 3, UserPolicy = UserPolicy.DELETE_PRODUCT, IsEnabled = false}, + // + // /* User 4 Policies */ + // new() {Id = 13, UserAccountId = 4, UserPolicy = UserPolicy.VIEW_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 = 16, UserAccountId = 4, UserPolicy = UserPolicy.DELETE_PRODUCT, IsEnabled = false}, + // + // /* User 5 Policies */ + // new() {Id = 17, UserAccountId = 5, UserPolicy = UserPolicy.VIEW_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 = 20, UserAccountId = 5, UserPolicy = UserPolicy.DELETE_PRODUCT, IsEnabled = true}, + // }; + // modelBuilder.Entity().HasData(demoUserAccountPolicies); } public DbSet UserAccounts { get; set; } diff --git a/BlazorPolicyAuth/HttpClientSetupService.cs b/BlazorPolicyAuth/HttpClientSetupService.cs index caaf065..8b8076a 100644 --- a/BlazorPolicyAuth/HttpClientSetupService.cs +++ b/BlazorPolicyAuth/HttpClientSetupService.cs @@ -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.Extensions.Hosting; namespace BlazorPolicyAuth; /// diff --git a/BlazorPolicyAuth/Migrations/20251215131042_user account with policies.Designer.cs b/BlazorPolicyAuth/Migrations/20251215131042_user account with policies.Designer.cs deleted file mode 100644 index b2c4ba5..0000000 --- a/BlazorPolicyAuth/Migrations/20251215131042_user account with policies.Designer.cs +++ /dev/null @@ -1,244 +0,0 @@ -// -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 - { - /// - 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("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER") - .HasColumnName("id"); - - b.Property("Password") - .HasMaxLength(100) - .HasColumnType("TEXT") - .HasColumnName("password"); - - b.Property("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("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER") - .HasColumnName("id"); - - b.Property("IsEnabled") - .HasColumnType("INTEGER") - .HasColumnName("is_enabled"); - - b.Property("UserAccountId") - .HasColumnType("INTEGER") - .HasColumnName("user_account_policy"); - - b.Property("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 - } - } -} diff --git a/BlazorPolicyAuth/Migrations/20251215131042_user account with policies.cs b/BlazorPolicyAuth/Migrations/20251215131042_user account with policies.cs deleted file mode 100644 index abebc6d..0000000 --- a/BlazorPolicyAuth/Migrations/20251215131042_user account with policies.cs +++ /dev/null @@ -1,94 +0,0 @@ -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional - -namespace BlazorPolicyAuth.Migrations -{ - /// - public partial class useraccountwithpolicies : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "user_account", - columns: table => new - { - id = table.Column(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), - user_name = table.Column(type: "TEXT", maxLength: 100, nullable: true), - password = table.Column(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(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), - user_account_policy = table.Column(type: "INTEGER", nullable: false), - user_policy = table.Column(type: "TEXT", nullable: true), - is_enabled = table.Column(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" } - }); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "user_account"); - - migrationBuilder.DropTable( - name: "user_account_policy"); - } - } -} diff --git a/BlazorPolicyAuth/Migrations/20260130211227_createUserAccount.Designer.cs b/BlazorPolicyAuth/Migrations/20260130211227_createUserAccount.Designer.cs new file mode 100644 index 0000000..8bdd804 --- /dev/null +++ b/BlazorPolicyAuth/Migrations/20260130211227_createUserAccount.Designer.cs @@ -0,0 +1,86 @@ +// +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 + { + /// + 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("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER") + .HasColumnName("id"); + + b.Property("DateCreated") + .HasColumnType("TEXT"); + + b.Property("Email") + .HasMaxLength(200) + .HasColumnType("TEXT") + .HasColumnName("email"); + + b.Property("Password") + .HasMaxLength(100) + .HasColumnType("TEXT") + .HasColumnName("password"); + + b.Property("PasswordHash") + .IsRequired() + .HasColumnType("BLOB"); + + b.Property("PasswordSalt") + .IsRequired() + .HasColumnType("BLOB"); + + b.Property("Role") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("user_account"); + }); + + modelBuilder.Entity("BlazorPolicyAuth.Models.Entities.UserAccountPolicy", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER") + .HasColumnName("id"); + + b.Property("IsEnabled") + .HasColumnType("INTEGER") + .HasColumnName("is_enabled"); + + b.Property("UserAccountId") + .HasColumnType("INTEGER") + .HasColumnName("user_account_policy"); + + b.Property("UserPolicy") + .HasColumnType("TEXT") + .HasColumnName("user_policy"); + + b.HasKey("Id"); + + b.ToTable("user_account_policy"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/BlazorPolicyAuth/Migrations/20260130211227_createUserAccount.cs b/BlazorPolicyAuth/Migrations/20260130211227_createUserAccount.cs new file mode 100644 index 0000000..c90a7de --- /dev/null +++ b/BlazorPolicyAuth/Migrations/20260130211227_createUserAccount.cs @@ -0,0 +1,58 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace BlazorPolicyAuth.Migrations +{ + /// + public partial class createUserAccount : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "user_account", + columns: table => new + { + id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + email = table.Column(type: "TEXT", maxLength: 200, nullable: true), + password = table.Column(type: "TEXT", maxLength: 100, nullable: true), + PasswordHash = table.Column(type: "BLOB", nullable: false), + PasswordSalt = table.Column(type: "BLOB", nullable: false), + DateCreated = table.Column(type: "TEXT", nullable: false), + Role = table.Column(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(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + user_account_policy = table.Column(type: "INTEGER", nullable: false), + user_policy = table.Column(type: "TEXT", nullable: true), + is_enabled = table.Column(type: "INTEGER", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_user_account_policy", x => x.id); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "user_account"); + + migrationBuilder.DropTable( + name: "user_account_policy"); + } + } +} diff --git a/BlazorPolicyAuth/Migrations/AppDbContextModelSnapshot.cs b/BlazorPolicyAuth/Migrations/AppDbContextModelSnapshot.cs index 3dcf1b5..ae70d7a 100644 --- a/BlazorPolicyAuth/Migrations/AppDbContextModelSnapshot.cs +++ b/BlazorPolicyAuth/Migrations/AppDbContextModelSnapshot.cs @@ -1,4 +1,5 @@ // +using System; using BlazorPolicyAuth.Data; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; @@ -23,51 +24,34 @@ namespace BlazorPolicyAuth.Migrations .HasColumnType("INTEGER") .HasColumnName("id"); + b.Property("DateCreated") + .HasColumnType("TEXT"); + + b.Property("Email") + .HasMaxLength(200) + .HasColumnType("TEXT") + .HasColumnName("email"); + b.Property("Password") .HasMaxLength(100) .HasColumnType("TEXT") .HasColumnName("password"); - b.Property("UserName") - .HasMaxLength(100) - .HasColumnType("TEXT") - .HasColumnName("user_name"); + b.Property("PasswordHash") + .IsRequired() + .HasColumnType("BLOB"); + + b.Property("PasswordSalt") + .IsRequired() + .HasColumnType("BLOB"); + + b.Property("Role") + .IsRequired() + .HasColumnType("TEXT"); 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 => @@ -92,148 +76,6 @@ namespace BlazorPolicyAuth.Migrations 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 } diff --git a/BlazorPolicyAuth/Models/Entities/UserAccount.cs b/BlazorPolicyAuth/Models/Entities/UserAccount.cs index 8504e39..c4b7675 100644 --- a/BlazorPolicyAuth/Models/Entities/UserAccount.cs +++ b/BlazorPolicyAuth/Models/Entities/UserAccount.cs @@ -1,4 +1,5 @@ -using System.ComponentModel.DataAnnotations; +using System; +using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace BlazorPolicyAuth.Models.Entities; @@ -11,16 +12,17 @@ public class UserAccount [Column("id")] public int Id { get; set; } - [Column("user_name")] - [MaxLength(100)] - public string? UserName { get; set; } + [Column("email")] + [MaxLength(200)] + public string? Email { get; set; } [Column("password")] [MaxLength(100)] public string? Password { get; set; } - public byte[] PasswordHash { get; set; } - public byte[] PasswordSalt { get; set; } + public byte[] PasswordHash { get; set; } = []; + public byte[] PasswordSalt { get; set; } = []; + public DateTime DateCreated { get; set; } = DateTime.Now; public string Role { get; set; } = "User"; } \ No newline at end of file diff --git a/BlazorPolicyAuth/Program.cs b/BlazorPolicyAuth/Program.cs index 8f83081..34ef402 100644 --- a/BlazorPolicyAuth/Program.cs +++ b/BlazorPolicyAuth/Program.cs @@ -1,10 +1,16 @@ +using System; +using System.Net.Http; using BlazorPolicyAuth; using BlazorPolicyAuth.Components; using BlazorPolicyAuth.Data; using BlazorPolicyAuth.Models.ViewModels; using BlazorPolicyAuth.Services.AuthService; using Microsoft.AspNetCore.Authentication.Cookies; +using Microsoft.AspNetCore.Builder; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; using ClientServices = BlazorPolicyAuth.App.Services; var builder = WebApplication.CreateBuilder(args); diff --git a/BlazorPolicyAuth/Services/AuthService/AuthService.cs b/BlazorPolicyAuth/Services/AuthService/AuthService.cs index 03ccbb5..785584a 100644 --- a/BlazorPolicyAuth/Services/AuthService/AuthService.cs +++ b/BlazorPolicyAuth/Services/AuthService/AuthService.cs @@ -1,4 +1,6 @@  +using System; +using System.Collections.Generic; using BlazorPolicyAuth.Data; using BlazorPolicyAuth.Models.Entities; using BlazorPolicyAuth.Models.ViewModels; @@ -9,6 +11,9 @@ using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; using System.Security.Cryptography; using System.Text; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Configuration; 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); var user = new UserAccount { - UserName = request.Email, + Email = request.Email, PasswordHash = passwordHash, PasswordSalt = passwordSalt }; @@ -38,7 +43,7 @@ public class AuthService(AppDbContext context, IConfiguration configuration, IHt public async Task 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; } @@ -48,13 +53,13 @@ public class AuthService(AppDbContext context, IConfiguration configuration, IHt public async Task> Login(string email, string password) { var response = new ServiceResponse(); - 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) { response.Success = false; 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.Message = "Wrong password."; @@ -91,7 +96,7 @@ public class AuthService(AppDbContext context, IConfiguration configuration, IHt public async Task 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) @@ -101,7 +106,7 @@ public class AuthService(AppDbContext context, IConfiguration configuration, IHt 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); var computedHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(password)); @@ -113,7 +118,7 @@ public class AuthService(AppDbContext context, IConfiguration configuration, IHt var claims = new List { new(ClaimTypes.NameIdentifier, user.Id.ToString()), - new(ClaimTypes.Name, user.UserName), + new(ClaimTypes.Name, user.Email), new(ClaimTypes.Role, user.Role) }; diff --git a/BlazorPolicyAuth/Services/AuthService/IAuthService.cs b/BlazorPolicyAuth/Services/AuthService/IAuthService.cs index a1c5189..7ea0674 100644 --- a/BlazorPolicyAuth/Services/AuthService/IAuthService.cs +++ b/BlazorPolicyAuth/Services/AuthService/IAuthService.cs @@ -1,4 +1,5 @@ -using BlazorPolicyAuth.Models.Entities; +using System.Threading.Tasks; +using BlazorPolicyAuth.Models.Entities; using BlazorPolicyAuth.Models.ViewModels; namespace BlazorPolicyAuth.Services.AuthService; diff --git a/BlazorPolicyAuth/UserPolicy.cs b/BlazorPolicyAuth/UserPolicy.cs index cbbc2f6..f6f5bc7 100644 --- a/BlazorPolicyAuth/UserPolicy.cs +++ b/BlazorPolicyAuth/UserPolicy.cs @@ -1,4 +1,6 @@ -namespace BlazorPolicyAuth; +using System.Collections.Generic; + +namespace BlazorPolicyAuth; public class UserPolicy { diff --git a/BlazorPolicyAuth/appsettings.json b/BlazorPolicyAuth/appsettings.json index 64a4acb..62ed08b 100644 --- a/BlazorPolicyAuth/appsettings.json +++ b/BlazorPolicyAuth/appsettings.json @@ -9,5 +9,8 @@ "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" + } } diff --git a/BlazorPolicyAuth/blazorpolicyauth.db b/BlazorPolicyAuth/blazorpolicyauth.db deleted file mode 100644 index 65cafa3..0000000 Binary files a/BlazorPolicyAuth/blazorpolicyauth.db and /dev/null differ