본문 바로가기
ASP.NET Core

ASP.NET Core MVC 6] Identity 확장

by Fastlane 2022. 1. 13.
728x90
반응형

Gender, City, Country와 같은 추가적인 User data를 저장하기 위해서, IdentityUser class의 확장이 필요하다. 

 

1. IdentityUser를 확장하는, ApplicationUser를 추가하자. 

Models 폴더에 ApplicationUser.cs 파일 추가 

using Microsoft.AspNetCore.Identity;

namespace Employee.Models
{
    public class ApplicationUser : IdentityUser
    {
        public string City { get; set; }
    }
}

2. IdentityUser 참조를 모두 ApplicationUser로 변경한다. 

3. ApplicationDbContext.cs에 ApplicationUser를 사용할 것을 알려준다. 

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>

4. 패키지 관리자 콘솔에서 Migration 한다. 

PM> Add-Migration Extend_IdentityUser
PM> Update-Database

AspNetUsers 테이블에 City 컬럼이 추가된 것을 확인할 수 있다. 

 

5. RegisterViewModel class에 City 추가 

6. Register HttpPost 저장 시, City 추가 


                var user = new ApplicationUser 
                { 
                    UserName = model.Email, 
                    Email = model.Email,
                    City = model.City
                };

 

7. AspNetUsers 테이블의 City컬럼에 값이 저장된 것을 확인할 수 있다. 

 

728x90
반응형

댓글