Skip to content

Commit

Permalink
擴展 IdentityUser 欄位
Browse files Browse the repository at this point in the history
  • Loading branch information
malagege committed Aug 19, 2022
1 parent 62198ab commit b36f5e0
Show file tree
Hide file tree
Showing 10 changed files with 390 additions and 65 deletions.
6 changes: 4 additions & 2 deletions AuthSample/AppDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using AuthSample.Models;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace AuthSample
{
public class AppDbContext : IdentityDbContext
// IdentityDbContext 預設為IdentityUser ,擴展 User 類別帶入IdentityDbContext
public class AppDbContext : IdentityDbContext<ApplicationUser>
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
Expand Down
12 changes: 7 additions & 5 deletions AuthSample/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AuthSample.ViewModels.Account;
using AuthSample.Models;
using AuthSample.ViewModels.Account;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
Expand All @@ -7,10 +8,10 @@ namespace AuthSample.Controllers
{
public class AccountController : Controller
{
private readonly UserManager<IdentityUser> _userManger;
private readonly SignInManager<IdentityUser> _signInManger;
private readonly UserManager<ApplicationUser> _userManger;
private readonly SignInManager<ApplicationUser> _signInManger;

public AccountController(UserManager<IdentityUser> userManager, SignInManager<IdentityUser> signInManager)
public AccountController(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager)
{
_userManger = userManager;
_signInManger = signInManager;
Expand Down Expand Up @@ -72,10 +73,11 @@ public async Task<IActionResult> RegisterAsync(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new IdentityUser
var user = new ApplicationUser
{
UserName = model.Email,
Email = model.Email,
City = model.City,
};

var result = await _userManger.CreateAsync(user, model.Password); // 第二個參數密碼要放,沒放不會檢核密碼規則
Expand Down
276 changes: 276 additions & 0 deletions AuthSample/Migrations/20220819022329_Extend_IdentityUser.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions AuthSample/Migrations/20220819022329_Extend_IdentityUser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.EntityFrameworkCore.Migrations;

namespace AuthSample.Migrations
{
public partial class Extend_IdentityUser : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "City",
table: "AspNetUsers",
type: "nvarchar(max)",
nullable: true);
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "City",
table: "AspNetUsers");
}
}
}

0 comments on commit b36f5e0

Please sign in to comment.