main
Peace 11 months ago
parent 5371eeab56
commit 8f3ea3a35d
  1. 1
      AspNetCoreApi/AspNetCoreApi.csproj
  2. 32
      AspNetCoreApi/Controllers/ApplicationUserController.cs
  3. 1
      AspNetCoreApi/DbContexts/AppDbContext.cs
  4. 49
      AspNetCoreApi/Migrations/20240614045441_add_app_user.Designer.cs
  5. 22
      AspNetCoreApi/Migrations/20240614045441_add_app_user.cs
  6. 102
      AspNetCoreApi/Migrations/20240614045917_add_app_user_fix.Designer.cs
  7. 57
      AspNetCoreApi/Migrations/20240614045917_add_app_user_fix.cs
  8. 53
      AspNetCoreApi/Migrations/AppDbContextModelSnapshot.cs
  9. 8
      AspNetCoreApi/Models/ApplicationUser.cs
  10. 11
      AspNetCoreApi/Program.cs
  11. 25
      AspNetCoreApi/Services/ApplicationUserService.cs
  12. 3
      README.md

@ -7,6 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.6">
<PrivateAssets>all</PrivateAssets>

@ -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<ProductController> _logger;
private readonly ApplicationUserService _service;
public ApplicationUserController(ILogger<ProductController> logger, ApplicationUserService service)
{
_logger = logger;
_service = service;
}
[HttpPost(Name = "PostApplicationUser")]
public async Task<bool> Post(string name, string email, string password)
{
ApplicationUser user = new ApplicationUser
{
UserName = name,
Email = email,
};
return await _service.Register(user, password);
}
}
}

@ -6,6 +6,7 @@ namespace AspNetCoreApi.DbContexts
public class AppDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
public DbSet<ApplicationUser> ApplicationUsers { get; set; }
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{

@ -0,0 +1,49 @@
// <auto-generated />
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
{
/// <inheritdoc />
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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Name")
.IsRequired()
.HasColumnType("longtext");
b.Property<decimal>("Price")
.HasColumnType("decimal(65,30)");
b.HasKey("Id");
b.ToTable("Products");
});
#pragma warning restore 612, 618
}
}
}

@ -0,0 +1,22 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace AspNetCoreApi.Migrations
{
/// <inheritdoc />
public partial class add_app_user : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
}

@ -0,0 +1,102 @@
// <auto-generated />
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
{
/// <inheritdoc />
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<string>("Id")
.HasColumnType("varchar(255)");
b.Property<int>("AccessFailedCount")
.HasColumnType("int");
b.Property<string>("ConcurrencyStamp")
.HasColumnType("longtext");
b.Property<string>("Email")
.HasColumnType("longtext");
b.Property<bool>("EmailConfirmed")
.HasColumnType("tinyint(1)");
b.Property<bool>("LockoutEnabled")
.HasColumnType("tinyint(1)");
b.Property<DateTimeOffset?>("LockoutEnd")
.HasColumnType("datetime(6)");
b.Property<string>("NormalizedEmail")
.HasColumnType("longtext");
b.Property<string>("NormalizedUserName")
.HasColumnType("longtext");
b.Property<string>("PasswordHash")
.HasColumnType("longtext");
b.Property<string>("PhoneNumber")
.HasColumnType("longtext");
b.Property<bool>("PhoneNumberConfirmed")
.HasColumnType("tinyint(1)");
b.Property<string>("SecurityStamp")
.HasColumnType("longtext");
b.Property<bool>("TwoFactorEnabled")
.HasColumnType("tinyint(1)");
b.Property<string>("UserName")
.HasColumnType("longtext");
b.HasKey("Id");
b.ToTable("ApplicationUsers");
});
modelBuilder.Entity("AspNetCoreApi.Models.Product", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Name")
.IsRequired()
.HasColumnType("longtext");
b.Property<decimal>("Price")
.HasColumnType("decimal(65,30)");
b.HasKey("Id");
b.ToTable("Products");
});
#pragma warning restore 612, 618
}
}
}

@ -0,0 +1,57 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace AspNetCoreApi.Migrations
{
/// <inheritdoc />
public partial class add_app_user_fix : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "ApplicationUsers",
columns: table => new
{
Id = table.Column<string>(type: "varchar(255)", nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
UserName = table.Column<string>(type: "longtext", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
NormalizedUserName = table.Column<string>(type: "longtext", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
Email = table.Column<string>(type: "longtext", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
NormalizedEmail = table.Column<string>(type: "longtext", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
EmailConfirmed = table.Column<bool>(type: "tinyint(1)", nullable: false),
PasswordHash = table.Column<string>(type: "longtext", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
SecurityStamp = table.Column<string>(type: "longtext", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
ConcurrencyStamp = table.Column<string>(type: "longtext", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
PhoneNumber = table.Column<string>(type: "longtext", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
PhoneNumberConfirmed = table.Column<bool>(type: "tinyint(1)", nullable: false),
TwoFactorEnabled = table.Column<bool>(type: "tinyint(1)", nullable: false),
LockoutEnd = table.Column<DateTimeOffset>(type: "datetime(6)", nullable: true),
LockoutEnabled = table.Column<bool>(type: "tinyint(1)", nullable: false),
AccessFailedCount = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ApplicationUsers", x => x.Id);
})
.Annotation("MySql:CharSet", "utf8mb4");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "ApplicationUsers");
}
}
}

@ -1,4 +1,5 @@
// <auto-generated />
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<string>("Id")
.HasColumnType("varchar(255)");
b.Property<int>("AccessFailedCount")
.HasColumnType("int");
b.Property<string>("ConcurrencyStamp")
.HasColumnType("longtext");
b.Property<string>("Email")
.HasColumnType("longtext");
b.Property<bool>("EmailConfirmed")
.HasColumnType("tinyint(1)");
b.Property<bool>("LockoutEnabled")
.HasColumnType("tinyint(1)");
b.Property<DateTimeOffset?>("LockoutEnd")
.HasColumnType("datetime(6)");
b.Property<string>("NormalizedEmail")
.HasColumnType("longtext");
b.Property<string>("NormalizedUserName")
.HasColumnType("longtext");
b.Property<string>("PasswordHash")
.HasColumnType("longtext");
b.Property<string>("PhoneNumber")
.HasColumnType("longtext");
b.Property<bool>("PhoneNumberConfirmed")
.HasColumnType("tinyint(1)");
b.Property<string>("SecurityStamp")
.HasColumnType("longtext");
b.Property<bool>("TwoFactorEnabled")
.HasColumnType("tinyint(1)");
b.Property<string>("UserName")
.HasColumnType("longtext");
b.HasKey("Id");
b.ToTable("ApplicationUsers");
});
modelBuilder.Entity("AspNetCoreApi.Models.Product", b =>
{
b.Property<int>("Id")

@ -0,0 +1,8 @@
using Microsoft.AspNetCore.Identity;
namespace AspNetCoreApi.Models
{
public class ApplicationUser : IdentityUser
{
}
}

@ -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<ProductService>();
// Add Identity
builder.Services.AddScoped<ApplicationUserService>();
builder.Services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<AppDbContext>()
.AddDefaultTokenProviders();
// Add services to the container.
builder.Services.AddControllers();

@ -0,0 +1,25 @@
using AspNetCoreApi.DbContexts;
using AspNetCoreApi.Models;
using Microsoft.AspNetCore.Identity;
namespace AspNetCoreApi.Services
{
public class ApplicationUserService
{
private readonly UserManager<ApplicationUser> _manager;
public ApplicationUserService(UserManager<ApplicationUser> manager)
{
_manager = manager;
}
public async Task<bool> Register(ApplicationUser user, string password)
{
var result = await _manager.CreateAsync(user, password);
if (result.Succeeded)
return true;
return false;
}
}
}

@ -8,4 +8,5 @@
- Routing
- EntityFramework
- Controller
- Dependancy Injection
- Dependancy Injection
- Identity
Loading…
Cancel
Save