Got a login

This commit is contained in:
Paul Schneider
2023-04-05 22:29:40 +01:00
parent 908d461552
commit b46c4b3447
61 changed files with 7795 additions and 611 deletions

View File

@ -15,6 +15,7 @@ using System.Reflection;
using Yavsc.Abstract.Templates;
using Microsoft.AspNetCore.Identity;
using RazorEngine.Configuration;
using Yavsc.Interface;
namespace Yavsc.Lib
{
@ -30,16 +31,14 @@ namespace Yavsc.Lib
readonly IStringLocalizer<EMailer> stringLocalizer;
readonly ApplicationDbContext dbContext;
readonly IEmailSender mailSender;
readonly ILogger logger;
public EMailer(ApplicationDbContext context, IEmailSender sender,
public EMailer(ApplicationDbContext context,
IStringLocalizer<EMailer> localizer,
ILoggerFactory loggerFactory)
{
stringLocalizer = localizer;
mailSender = sender;
logger = loggerFactory.CreateLogger<EMailer>();

View File

@ -1,3 +1,4 @@
using System.Text;
using System;
using System.Net;
using System.Threading.Tasks;
@ -7,10 +8,13 @@ using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using MimeKit;
using Yavsc.Abstract.Manage;
using Microsoft.AspNetCore.Identity.UI.Services;
using Yavsc.Interface;
using Yavsc.Settings;
namespace Yavsc.Services
{
public class MailSender : IEmailSender
public class MailSender : IEmailSender, ITrueEmailSender
{
readonly SiteSettings siteSettings;
readonly SmtpSettings smtpSettings;
@ -22,10 +26,9 @@ namespace Yavsc.Services
ILoggerFactory loggerFactory
)
{
siteSettings = sitesOptions?.Value;
smtpSettings = smtpOptions?.Value;
siteSettings = sitesOptions.Value;
smtpSettings = smtpOptions.Value;
logger = loggerFactory.CreateLogger<MailSender>();
}
/// <summary>
@ -39,52 +42,42 @@ namespace Yavsc.Services
/// </returns>
public async Task<EmailSentViewModel> SendEmailAsync(string username, string email, string subject, string message)
public async Task SendEmailAsync(string email, string subject, string htmlMessage)
{
EmailSentViewModel model = new EmailSentViewModel{ EMail = email };
try
{
logger.LogInformation($"sendEmail for {email} : {message}");
MimeMessage msg = new MimeMessage();
msg.From.Add(new MailboxAddress(
siteSettings.Owner.Name,
siteSettings.Owner.EMail));
msg.To.Add(new MailboxAddress(username, email));
msg.Body = new TextPart("plain")
{
Text = message
};
msg.Subject = subject;
msg.MessageId = MimeKit.Utils.MimeUtils.GenerateMessageId(
siteSettings.Authority
);
using (SmtpClient sc = new SmtpClient())
{
sc.Connect(
smtpSettings.Host,
smtpSettings.Port,
SecureSocketOptions.Auto);
if (smtpSettings.UserName!=null) {
NetworkCredential creds = new NetworkCredential(
smtpSettings.UserName, smtpSettings.Password, smtpSettings.Domain);
await sc.AuthenticateAsync(System.Text.Encoding.UTF8, creds, System.Threading.CancellationToken.None);
}
await sc.SendAsync(msg);
model.MessageId = msg.MessageId;
model.Sent = true; // a duplicate info to remove from the view model, that equals to MessageId == null
logger.LogInformation($"Sent : {msg}");
}
}
catch (Exception ex)
{
model.Sent = false;
model.ErrorMessage = ex.Message;
return model;
}
return model;
await SendEmailAsync("", email, subject, htmlMessage);
}
public async Task<string> SendEmailAsync(string name, string email, string subject, string htmlMessage)
{
logger.LogInformation($"SendEmail for {email} : {subject}");
MimeMessage msg = new ();
msg.From.Add(new MailboxAddress(siteSettings.Owner.Name,
siteSettings.Owner.EMail));
msg.To.Add(new MailboxAddress(name, email));
msg.Body = new TextPart("html")
{
Text = htmlMessage
};
msg.Subject = subject;
msg.MessageId = MimeKit.Utils.MimeUtils.GenerateMessageId(
siteSettings.Authority
);
using (SmtpClient sc = new ())
{
sc.Connect(
smtpSettings.Server,
smtpSettings.Port,
SecureSocketOptions.Auto);
if (smtpSettings.UserName!=null) {
NetworkCredential creds = new (
smtpSettings.UserName, smtpSettings.Password);
await sc.AuthenticateAsync(System.Text.Encoding.UTF8, creds, System.Threading.CancellationToken.None);
}
await sc.SendAsync(msg);
logger.LogInformation($"Sent : {msg.MessageId}");
}
return msg.MessageId;
}
}
}

View File

@ -1,6 +1,8 @@
using Microsoft.AspNetCore.SignalR;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Yavsc.Interface;
using Yavsc.Interfaces.Workflow;
using Yavsc.Models;
using Yavsc.Models.Google.Messaging;
@ -12,7 +14,7 @@ namespace Yavsc.Services
public class YavscMessageSender : IYavscMessageSender
{
private readonly ILogger _logger;
readonly IEmailSender _emailSender;
readonly ITrueEmailSender _emailSender;
readonly SiteSettings siteSettings;
readonly ApplicationDbContext _dbContext;
readonly IConnexionManager _cxManager;
@ -21,7 +23,7 @@ namespace Yavsc.Services
public YavscMessageSender(
ILoggerFactory loggerFactory,
IOptions<SiteSettings> sitesOptions,
IEmailSender emailSender,
ITrueEmailSender emailSender,
ApplicationDbContext dbContext,
IConnexionManager cxManager,
IHubContext<ChatHub> hubContext
@ -90,24 +92,12 @@ namespace Yavsc.Services
_logger.LogDebug($"Sending to {user.UserName} <{user.Email}> : {body}");
var mailSent = await _emailSender.SendEmailAsync(
user.UserName,
user.Email,
result.message_id = await _emailSender.SendEmailAsync(user.UserName, user.Email,
$"{ev.Sender} (un client) vous demande un rendez-vous",
body + Environment.NewLine
);
if (!mailSent.Sent)
{
result.message_id = mailSent.MessageId;
result.error = mailSent.ErrorMessage;
response.failure++;
}
else
{
result.message_id = mailSent.MessageId;
body + Environment.NewLine);
response.success++;
}
var cxids = _cxManager.GetConnexionIds(user.UserName);
if (cxids == null)
{