تزریق وابستگیها در پروفایلهای AutoMapper در برنامههای ASP.NET Core
نویسنده: وحید نصیری
تاریخ: ۱۳۹۹/۰۷/۱۲ ۲۰:۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
namespace AutoMapperInjection.Entities
{
public class User
{
public int Id { set; get; }
public string HashedPassword { set; get; }
}
} namespace AutoMapperInjection.Models
{
public class UserDto
{
public string Password { set; get; }
}
} namespace AutoMapperInjection.Controllers
{
[ApiController]
[Route("[controller]")]
public class HomeController : ControllerBase
{
private readonly IMapper _mapper;
public HomeController(IMapper mapper)
{
_mapper = mapper ?? throw new NullReferenceException(nameof(mapper));
}
[HttpPost("[action]")]
public IActionResult RegisterUser(UserDto model)
{
var user = _mapper.Map<User>(model);
// TODO: Save user
return Ok();
}
}
} using System;
using System.Security.Cryptography;
using System.Text;
namespace AutoMapperInjection.Services
{
public interface IPasswordHasherService
{
string GetSha256Hash(string input);
}
public class PasswordHasherService : IPasswordHasherService
{
public string GetSha256Hash(string input)
{
using var hashAlgorithm = new SHA256CryptoServiceProvider();
var byteValue = Encoding.UTF8.GetBytes(input);
var byteHash = hashAlgorithm.ComputeHash(byteValue);
return Convert.ToBase64String(byteHash);
}
}
} public class HashedPasswordResolver : IValueResolver<UserDto, User, string>
{
private readonly IPasswordHasherService _hasher;
public HashedPasswordResolver(IPasswordHasherService hasher)
{
_hasher = hasher ?? throw new ArgumentNullException(nameof(hasher));
}
public string Resolve(UserDto source, User destination, string destMember, ResolutionContext context)
{
return _hasher.GetSha256Hash(source.Password);
}
} public class UserDtoMappingsProfile : Profile
{
public UserDtoMappingsProfile()
{
// Map from User (entity) to UserDto, and back
this.CreateMap<User, UserDto>()
.ReverseMap()
.ForMember(user => user.HashedPassword, exp => exp.MapFrom<HashedPasswordResolver>());
}
} namespace AutoMapperInjection
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IPasswordHasherService, PasswordHasherService>();
services.AddAutoMapper(typeof(UserDtoMappingsProfile).Assembly);
services.AddControllers();
} public class UserDtoMappingsAction : IMappingAction<UserDto, User>
{
private readonly IPasswordHasherService _hasher;
public UserDtoMappingsAction(IPasswordHasherService hasher)
{
_hasher = hasher ?? throw new ArgumentNullException(nameof(hasher));
}
public void Process(UserDto source, User destination, ResolutionContext context)
{
destination.HashedPassword = _hasher.GetSha256Hash(source.Password);
}
} public class UserDtoMappingsProfile : Profile
{
public UserDtoMappingsProfile()
{
// Map from User (entity) to UserDto, and back
this.CreateMap<User, UserDto>()
.ReverseMap()
.AfterMap<UserDtoMappingsAction>();
}
}