I want to make Email Activation in my project. I works with Entity Framework to database connections in repository layer and Services uses this layers.
I want create key and insert to database with Entity Framework and Dependency Injection service without noncontroller.
Repository Layes
public void Insert(T entity)
{
if (entity == null)
{
throw new ArgumentNullException("entity");
}
_entities.Add(entity);
SaveChanges();
}
ActivationService
public void Insert(EmailValid entity)
{
_repositoryBase.Insert(entity);
}
Non Controller Class
public class EmailActivaitonKey
{
private readonly IActivationService _activationService;
public EmailActivaitonKey()
{
this._activationService = Startup.ActivationService;
}
public string ActivationKey(string email)
{
string guid = Guid.NewGuid().ToString();
while (_activationService.GetByFilter(i => i.ActivationKey == guid) != null)
{
guid = Guid.NewGuid().ToString();
}
string key = email + ":OSK:" + DateTime.Now + ":OSK:" + guid;
EmailValid emailValid = new EmailValid
{
Email = email,
Time = DateTime.Today,
ActivationKey = key
};
_activationService.Insert(emailValid);
return new Helpers.AESEncryption().EncryptText(key);
}
}
in other class declares EmailActivationKey
MailMessage mailMessage = new MailMessage
{
From = new MailAddress("***@***.***"),
Body = "Crypto Box Activation",
Subject = $"<a href='/Email/Activation?key={new EmailActivaitonKey().ActivationKey(email)}'><h1>Click For Activation<h1><a>",
To = { email }
};
in Startup:
public static IActivationService ActivationService;
//then
services.AddScoped<IActivationService, ActivationService>();
ActivationService = services.BuildServiceProvider().GetService<IActivationService>();
I looked at this question and this one too, but I did not get any results.