From 8f3ea3a35d53a19d8416e3125df380cc7353b43b Mon Sep 17 00:00:00 2001 From: Peace Date: Fri, 14 Jun 2024 14:07:38 +0900 Subject: [PATCH] Identity --- AspNetCoreApi/AspNetCoreApi.csproj | 1 + .../Controllers/ApplicationUserController.cs | 32 ++++++ AspNetCoreApi/DbContexts/AppDbContext.cs | 1 + .../20240614045441_add_app_user.Designer.cs | 49 +++++++++ .../Migrations/20240614045441_add_app_user.cs | 22 ++++ ...0240614045917_add_app_user_fix.Designer.cs | 102 ++++++++++++++++++ .../20240614045917_add_app_user_fix.cs | 57 ++++++++++ .../Migrations/AppDbContextModelSnapshot.cs | 53 +++++++++ AspNetCoreApi/Models/ApplicationUser.cs | 8 ++ AspNetCoreApi/Program.cs | 11 +- .../Services/ApplicationUserService.cs | 25 +++++ README.md | 3 +- 12 files changed, 361 insertions(+), 3 deletions(-) create mode 100644 AspNetCoreApi/Controllers/ApplicationUserController.cs create mode 100644 AspNetCoreApi/Migrations/20240614045441_add_app_user.Designer.cs create mode 100644 AspNetCoreApi/Migrations/20240614045441_add_app_user.cs create mode 100644 AspNetCoreApi/Migrations/20240614045917_add_app_user_fix.Designer.cs create mode 100644 AspNetCoreApi/Migrations/20240614045917_add_app_user_fix.cs create mode 100644 AspNetCoreApi/Models/ApplicationUser.cs create mode 100644 AspNetCoreApi/Services/ApplicationUserService.cs diff --git a/AspNetCoreApi/AspNetCoreApi.csproj b/AspNetCoreApi/AspNetCoreApi.csproj index 3bb40ba..281a2ce 100644 --- a/AspNetCoreApi/AspNetCoreApi.csproj +++ b/AspNetCoreApi/AspNetCoreApi.csproj @@ -7,6 +7,7 @@ + all diff --git a/AspNetCoreApi/Controllers/ApplicationUserController.cs b/AspNetCoreApi/Controllers/ApplicationUserController.cs new file mode 100644 index 0000000..32978dc --- /dev/null +++ b/AspNetCoreApi/Controllers/ApplicationUserController.cs @@ -0,0 +1,32 @@ +using AspNetCoreApi.Models; +using AspNetCoreApi.Services; +using Microsoft.AspNetCore.Mvc; + +namespace AspNetCoreApi.Controllers +{ + [ApiController] + [Route("[controller]")] + public class ApplicationUserController : ControllerBase + { + private readonly ILogger _logger; + private readonly ApplicationUserService _service; + + public ApplicationUserController(ILogger logger, ApplicationUserService service) + { + _logger = logger; + _service = service; + } + + [HttpPost(Name = "PostApplicationUser")] + public async Task Post(string name, string email, string password) + { + ApplicationUser user = new ApplicationUser + { + UserName = name, + Email = email, + }; + + return await _service.Register(user, password); + } + } +} diff --git a/AspNetCoreApi/DbContexts/AppDbContext.cs b/AspNetCoreApi/DbContexts/AppDbContext.cs index 823da57..4a46c87 100644 --- a/AspNetCoreApi/DbContexts/AppDbContext.cs +++ b/AspNetCoreApi/DbContexts/AppDbContext.cs @@ -6,6 +6,7 @@ namespace AspNetCoreApi.DbContexts public class AppDbContext : DbContext { public DbSet Products { get; set; } + public DbSet ApplicationUsers { get; set; } public AppDbContext(DbContextOptions options) : base(options) { diff --git a/AspNetCoreApi/Migrations/20240614045441_add_app_user.Designer.cs b/AspNetCoreApi/Migrations/20240614045441_add_app_user.Designer.cs new file mode 100644 index 0000000..647a596 --- /dev/null +++ b/AspNetCoreApi/Migrations/20240614045441_add_app_user.Designer.cs @@ -0,0 +1,49 @@ +// +using AspNetCoreApi.DbContexts; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace AspNetCoreApi.Migrations +{ + [DbContext(typeof(AppDbContext))] + [Migration("20240614045441_add_app_user")] + partial class add_app_user + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.6") + .HasAnnotation("Relational:MaxIdentifierLength", 64); + + MySqlModelBuilderExtensions.AutoIncrementColumns(modelBuilder); + + modelBuilder.Entity("AspNetCoreApi.Models.Product", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("longtext"); + + b.Property("Price") + .HasColumnType("decimal(65,30)"); + + b.HasKey("Id"); + + b.ToTable("Products"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/AspNetCoreApi/Migrations/20240614045441_add_app_user.cs b/AspNetCoreApi/Migrations/20240614045441_add_app_user.cs new file mode 100644 index 0000000..3816dfd --- /dev/null +++ b/AspNetCoreApi/Migrations/20240614045441_add_app_user.cs @@ -0,0 +1,22 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace AspNetCoreApi.Migrations +{ + /// + public partial class add_app_user : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + + } + } +} diff --git a/AspNetCoreApi/Migrations/20240614045917_add_app_user_fix.Designer.cs b/AspNetCoreApi/Migrations/20240614045917_add_app_user_fix.Designer.cs new file mode 100644 index 0000000..2d1f306 --- /dev/null +++ b/AspNetCoreApi/Migrations/20240614045917_add_app_user_fix.Designer.cs @@ -0,0 +1,102 @@ +// +using System; +using AspNetCoreApi.DbContexts; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace AspNetCoreApi.Migrations +{ + [DbContext(typeof(AppDbContext))] + [Migration("20240614045917_add_app_user_fix")] + partial class add_app_user_fix + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.6") + .HasAnnotation("Relational:MaxIdentifierLength", 64); + + MySqlModelBuilderExtensions.AutoIncrementColumns(modelBuilder); + + modelBuilder.Entity("AspNetCoreApi.Models.ApplicationUser", b => + { + b.Property("Id") + .HasColumnType("varchar(255)"); + + b.Property("AccessFailedCount") + .HasColumnType("int"); + + b.Property("ConcurrencyStamp") + .HasColumnType("longtext"); + + b.Property("Email") + .HasColumnType("longtext"); + + b.Property("EmailConfirmed") + .HasColumnType("tinyint(1)"); + + b.Property("LockoutEnabled") + .HasColumnType("tinyint(1)"); + + b.Property("LockoutEnd") + .HasColumnType("datetime(6)"); + + b.Property("NormalizedEmail") + .HasColumnType("longtext"); + + b.Property("NormalizedUserName") + .HasColumnType("longtext"); + + b.Property("PasswordHash") + .HasColumnType("longtext"); + + b.Property("PhoneNumber") + .HasColumnType("longtext"); + + b.Property("PhoneNumberConfirmed") + .HasColumnType("tinyint(1)"); + + b.Property("SecurityStamp") + .HasColumnType("longtext"); + + b.Property("TwoFactorEnabled") + .HasColumnType("tinyint(1)"); + + b.Property("UserName") + .HasColumnType("longtext"); + + b.HasKey("Id"); + + b.ToTable("ApplicationUsers"); + }); + + modelBuilder.Entity("AspNetCoreApi.Models.Product", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("longtext"); + + b.Property("Price") + .HasColumnType("decimal(65,30)"); + + b.HasKey("Id"); + + b.ToTable("Products"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/AspNetCoreApi/Migrations/20240614045917_add_app_user_fix.cs b/AspNetCoreApi/Migrations/20240614045917_add_app_user_fix.cs new file mode 100644 index 0000000..6553b2c --- /dev/null +++ b/AspNetCoreApi/Migrations/20240614045917_add_app_user_fix.cs @@ -0,0 +1,57 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace AspNetCoreApi.Migrations +{ + /// + public partial class add_app_user_fix : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "ApplicationUsers", + columns: table => new + { + Id = table.Column(type: "varchar(255)", nullable: false) + .Annotation("MySql:CharSet", "utf8mb4"), + UserName = table.Column(type: "longtext", nullable: true) + .Annotation("MySql:CharSet", "utf8mb4"), + NormalizedUserName = table.Column(type: "longtext", nullable: true) + .Annotation("MySql:CharSet", "utf8mb4"), + Email = table.Column(type: "longtext", nullable: true) + .Annotation("MySql:CharSet", "utf8mb4"), + NormalizedEmail = table.Column(type: "longtext", nullable: true) + .Annotation("MySql:CharSet", "utf8mb4"), + EmailConfirmed = table.Column(type: "tinyint(1)", nullable: false), + PasswordHash = table.Column(type: "longtext", nullable: true) + .Annotation("MySql:CharSet", "utf8mb4"), + SecurityStamp = table.Column(type: "longtext", nullable: true) + .Annotation("MySql:CharSet", "utf8mb4"), + ConcurrencyStamp = table.Column(type: "longtext", nullable: true) + .Annotation("MySql:CharSet", "utf8mb4"), + PhoneNumber = table.Column(type: "longtext", nullable: true) + .Annotation("MySql:CharSet", "utf8mb4"), + PhoneNumberConfirmed = table.Column(type: "tinyint(1)", nullable: false), + TwoFactorEnabled = table.Column(type: "tinyint(1)", nullable: false), + LockoutEnd = table.Column(type: "datetime(6)", nullable: true), + LockoutEnabled = table.Column(type: "tinyint(1)", nullable: false), + AccessFailedCount = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ApplicationUsers", x => x.Id); + }) + .Annotation("MySql:CharSet", "utf8mb4"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "ApplicationUsers"); + } + } +} diff --git a/AspNetCoreApi/Migrations/AppDbContextModelSnapshot.cs b/AspNetCoreApi/Migrations/AppDbContextModelSnapshot.cs index e30aa82..084a9ae 100644 --- a/AspNetCoreApi/Migrations/AppDbContextModelSnapshot.cs +++ b/AspNetCoreApi/Migrations/AppDbContextModelSnapshot.cs @@ -1,4 +1,5 @@ // +using System; using AspNetCoreApi.DbContexts; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; @@ -21,6 +22,58 @@ namespace AspNetCoreApi.Migrations MySqlModelBuilderExtensions.AutoIncrementColumns(modelBuilder); + modelBuilder.Entity("AspNetCoreApi.Models.ApplicationUser", b => + { + b.Property("Id") + .HasColumnType("varchar(255)"); + + b.Property("AccessFailedCount") + .HasColumnType("int"); + + b.Property("ConcurrencyStamp") + .HasColumnType("longtext"); + + b.Property("Email") + .HasColumnType("longtext"); + + b.Property("EmailConfirmed") + .HasColumnType("tinyint(1)"); + + b.Property("LockoutEnabled") + .HasColumnType("tinyint(1)"); + + b.Property("LockoutEnd") + .HasColumnType("datetime(6)"); + + b.Property("NormalizedEmail") + .HasColumnType("longtext"); + + b.Property("NormalizedUserName") + .HasColumnType("longtext"); + + b.Property("PasswordHash") + .HasColumnType("longtext"); + + b.Property("PhoneNumber") + .HasColumnType("longtext"); + + b.Property("PhoneNumberConfirmed") + .HasColumnType("tinyint(1)"); + + b.Property("SecurityStamp") + .HasColumnType("longtext"); + + b.Property("TwoFactorEnabled") + .HasColumnType("tinyint(1)"); + + b.Property("UserName") + .HasColumnType("longtext"); + + b.HasKey("Id"); + + b.ToTable("ApplicationUsers"); + }); + modelBuilder.Entity("AspNetCoreApi.Models.Product", b => { b.Property("Id") diff --git a/AspNetCoreApi/Models/ApplicationUser.cs b/AspNetCoreApi/Models/ApplicationUser.cs new file mode 100644 index 0000000..dbc0e67 --- /dev/null +++ b/AspNetCoreApi/Models/ApplicationUser.cs @@ -0,0 +1,8 @@ +using Microsoft.AspNetCore.Identity; + +namespace AspNetCoreApi.Models +{ + public class ApplicationUser : IdentityUser + { + } +} diff --git a/AspNetCoreApi/Program.cs b/AspNetCoreApi/Program.cs index 3317913..298315c 100644 --- a/AspNetCoreApi/Program.cs +++ b/AspNetCoreApi/Program.cs @@ -1,9 +1,10 @@ using AspNetCoreApi.DbContexts; using AspNetCoreApi.Middlewares; +using AspNetCoreApi.Models; using AspNetCoreApi.Services; +using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; -using Pomelo.EntityFrameworkCore.MySql; namespace AspNetCoreApi { @@ -22,9 +23,15 @@ namespace AspNetCoreApi options => options .UseMySql(connString, dbServerVersion)); - // Dependancy injection + // Add Dependancy injection builder.Services.AddScoped(); + // Add Identity + builder.Services.AddScoped(); + builder.Services.AddIdentity() + .AddEntityFrameworkStores() + .AddDefaultTokenProviders(); + // Add services to the container. builder.Services.AddControllers(); diff --git a/AspNetCoreApi/Services/ApplicationUserService.cs b/AspNetCoreApi/Services/ApplicationUserService.cs new file mode 100644 index 0000000..20c2772 --- /dev/null +++ b/AspNetCoreApi/Services/ApplicationUserService.cs @@ -0,0 +1,25 @@ +using AspNetCoreApi.DbContexts; +using AspNetCoreApi.Models; +using Microsoft.AspNetCore.Identity; + +namespace AspNetCoreApi.Services +{ + public class ApplicationUserService + { + private readonly UserManager _manager; + + public ApplicationUserService(UserManager manager) + { + _manager = manager; + } + + public async Task Register(ApplicationUser user, string password) + { + var result = await _manager.CreateAsync(user, password); + if (result.Succeeded) + return true; + + return false; + } + } +} diff --git a/README.md b/README.md index c53bd21..58f3c6e 100644 --- a/README.md +++ b/README.md @@ -8,4 +8,5 @@ - Routing - EntityFramework - Controller - - Dependancy Injection \ No newline at end of file + - Dependancy Injection + - Identity \ No newline at end of file