สร้างโครงสร้างพื้นฐานให้กับโครงการใน. NET ด้วยเฟรมเวิร์กเอนทิตีหน่วยการทำงานรูปแบบที่เก็บและตัวตนด้วยโทเค็นการเข้าถึง JWT
ในการสร้าง crud ใหม่สำหรับเอนทิตีที่คุณต้องทำ:
สร้างใน MBB.Abrigo.Core.Models จำลองเอนทิตีของคุณ
public class Person
{
public string Id { get; set; }
[Required]
[MaxLength(50)]
public string FirstName { get; set; }
}
สร้างใน MBB.Abrigo.Core.DTO dto เอนทิตีของคุณ (จะเห็น DTO สำหรับลูกค้า)
public class PersonDTO
{
public string Id { get; set; }
public string FirstName { get; set; }
}
คุณควรเพิ่มตารางใน db สำหรับสิ่งนี้ใน MBB.Abrigo.Infrastructure ในคลาส BaseContext.cs แทรกบรรทัดถัดไป:
public DbSet<Person> Persons { get; set; }
สร้างใน MBB.Abrigo.Infrastructure.IRepository อินเตอร์เฟสสำหรับที่เก็บของคุณหลังจากนี้คุณควรสร้างใน MBB.Abrigo.Infrastructure.Repository ที่เก็บข้อมูลของคุณ
ใน MBB.Abrigo.Infrastructure ในชั้นเรียน UnitOfWork.cs คุณควรเพิ่มที่เก็บ (รูปแบบซิงเกิลตัน) ตัวอย่างเช่น:
public PersonRepository PersonRepository
{
get
{
if (this.personRepository == null)
{
this.personRepository = new PersonRepository(context);
}
return personRepository;
}
}
สร้างใน MBB.Abrigo.Infrastructure.IManager อินเทอร์เฟซสำหรับการดำเนินการคอนโทรลเลอร์ของคุณจะได้รับหลังจากนั้นการดำเนินการนี้ใน MBB.Abrigo.Infrastructure.Manager
สร้างใน MBB.Abrigo.WebApi.Controller ตัวควบคุมซึ่งจะได้รับคำขอของลูกค้าคอนโทรลเลอร์ควรใช้ประโยชน์จากผู้จัดการ ตัวอย่างเช่น:
public class PersonController : ApiController
{
private PersonManager personManager = new PersonManager();
// GET: api/Person
public IEnumerable<PersonDTO> GetPersons()
{
return personManager.GetAll();
}
}
ติดตั้งครั้งแรกโดยคอนโซล PM> Install-Package System.IdentityModel.Tokens.Jwt หลังจากนั้นสร้าง
เพิ่มใน MBB.Abrigo.WebApi.Models.AccountViewModels.cs คลาสสำหรับโมเดลเข้าสู่ระบบตัวอย่างเช่น:
public class LoginViewModel
{
public string Username { get; set; }
public string Password { get; set; }
}
เพิ่มใน MBB.Abrigo.WebApi.Controllers.AccountController.cs คอนโทรลเลอร์สำหรับการเข้าสู่ระบบตัวอย่างเช่น:
public async Task<IHttpActionResult> Login(LoginViewModel model)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var user = await UserManager.FindByEmailAsync(model.Username);
if (user != null)
{
if (UserManager.CheckPassword(user, model.Password))
{
var token = TokenGenerator.GenerateTokenJwt(model.Username);
return Ok(token);
}
else
{
return Unauthorized();
}
}
else
{
return Unauthorized();
}
}
เพิ่มใน MBB.Abrigo.WebApi.Controllers.PersonController.cs การอนุญาตสำหรับคำขอเช่น:
// GET: api/Person
[Authorize]
public IEnumerable<PersonDTO> GetPersons()
{
return personManager.GetAll();
}
สร้างคลาสสำหรับการสร้างโทเค็นใน MBB.Abrigo.WebApi.Security ดูเพิ่มเติมในไดเรกทอรีนั้น
เพิ่มใน MBB.Abrigo.WebApi.App_Start.WebApiConfig.cs บรรทัดถัดไปของรหัส:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Configuración de rutas y servicios de API
config.MapHttpAttributeRoutes();
config.MessageHandlers.Add(new TokenValidationHandler()); //THIS LINE TO ADD
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
เพิ่มใน MBB.Abrigo.WebApi.WebApi.cs บรรทัดถัดไปของรหัส:
<appSettings>
<add key="JWT_SECRET_KEY" value="clave-secreta-api"/>
<add key="JWT_AUDIENCE_TOKEN" value="http://localhost:49220"/>
<add key="JWT_ISSUER_TOKEN" value="http://localhost:49220"/>
<add key="JWT_EXPIRE_MINUTES" value="30"/>
</appSettings>
สำหรับคำถามหรือข้อเสนอแนะใด ๆ สื่อสารกับ ([email protected])