ASP.Net: login เข้าระบบด้วย Username โดยไม่เก็บ Email

เตรียมโปรเจ็กส์ก่อน โดยสร้างโปรเจ็กส์แบบ MVC แบบมี user

  1. แก้ไขหน้า Register
  2. แก้ไขหน้า Login
  3. IdentityConfig

1. แก้ไขหน้า Register

Models/AccountViewModels.cs

Views/Account/Register.cshtml

Controllers/AccountController.cs

รันดูจะเห็นว่าในหน้า Register เราได้เพิ่มช่องรับ UserName เข้ามา และไม่มีช่องใส่ Email

2. แก้ไขหน้า Login

ทำตาม login เข้าระบบด้วย Username แทนที่ Email

3. IdentityConfig

App_Start\IdentityConfig.cs

บรรทัดที่ 16: เปลี่ยนค่า RequireUniqueEmail จาก true เป็น false
ซึ่งถ้าไม่กำหนดค่าในบรรทัดที่ 16 นี้ จะชึ้น Error ว่า “Email cannot be null or empty.

Link