![](/img/trans.png)
[英]Send email confirmation token as clickable link to user upon registration in ASP.NET Core Web Api
[英]ASP.NET CORE - Send email: Please log in via your web browser and then try again
我正在嘗試在生產中發送一封帶有用戶注冊驗證鏈接的郵件。
為此,我在我的 appsettings.json 中附加了發送郵件的 gmail 帳戶的憑據
APPSETTINGS.JSON:
我的控制器發送郵件的操作如下:
[HttpPost]
public async Task<JsonResult> Register(AddUserViewModel model)
{
if (ModelState.IsValid)
{
User user = await _userHelper.AddUserAsync(model, imageId);
if (user == null)
{
return Json("Email repeat");
}
string myToken = await _userHelper.GenerateEmailConfirmationTokenAsync(user);
string tokenLink = Url.Action("ConfirmEmail", "Account", new
{
userid = user.Id,
token = myToken
}, protocol: HttpContext.Request.Scheme);
Response response = _mailHelper.SendMail(model.Username, "App - Confirmación de cuenta", $"<h1>App - Confirmación de cuenta</h1>" +
$"Para habilitar el usuario, " +
$"por favor hacer clic en el siguiente enlace: </br></br><a href = \"{tokenLink}\">Confirmar Email</a>");
if (response.IsSuccess)
{
return Json("Email send");
}
string message = response.Message;
return Json(message);
}
return Json("Model invalid");
}
返回 Response 的 sendEmail 方法如下:
public Response SendMail(string to, string subject, string body)
{
try
{
string from = _configuration["Mail:From"];
string smtp = _configuration["Mail:Smtp"];
string port = _configuration["Mail:Port"];
string password = _configuration["Mail:Password"];
MimeMessage message = new MimeMessage();
message.From.Add(new MailboxAddress(from));
message.To.Add(new MailboxAddress(to));
message.Subject = subject;
BodyBuilder bodyBuilder = new BodyBuilder
{
HtmlBody = body
};
message.Body = bodyBuilder.ToMessageBody();
using (SmtpClient client = new SmtpClient())
{
client.CheckCertificateRevocation = false;
client.Connect(smtp, int.Parse(port), false);
client.Authenticate(from, password);
client.Send(message);
client.Disconnect(true);
}
return new Response { IsSuccess = true };
}
catch (Exception ex)
{
return new Response
{
IsSuccess = false,
Message = ex.Message,
Result = ex
};
}
}
錯誤消息如下:
“534:5.7.14 \\ u003Chttps://accounts.google.com/signin/continue SARP = 1 \\ u0026scc = 1 \\ u0026plt = AKgnsbt \\ n5.7.14 GCim6bqtaJeANyyQ0NvegYJS8qnYbDSCz3M0IMvB-rgIFdr1rLrIl1wbt4DkimTvNMLDl \\ n5.7.14 8dSGZxGuAWmDwX6gPD1T_lJ3U1e0G8EEAu6Lgt3p5gk1yJpr85Pm2mBN9nO4G33Y \\ u003E \\ n5.7.14請通過您的網絡瀏覽器登錄,然后重試。\\n5.7.14 了解詳情,請訪問\\n5.7.14 https://support.google.com/mail/answer/78754 t15sm12262168pjy.17 - gsmtp"
我發送正確嗎? 我應該更改任何 gmail 設置嗎?
在開發環境中發送沒有問題,對我有什么幫助或建議嗎?
不建議直接使用 Gmail、Outlook 和類似的提供商,因為它們執行的檢查可能會破壞您的代碼,就像您在此處看到的那樣。
這可能會在沒有事先通知的情況下發生。 相信我,它會不時發生。
大多數提供商(特別是 Gmail)現在根據最新的 OAuth/OpenID 規范強制執行基於 Web 的流程,以實現更安全的身份驗證/授權。
這將是我的第一個猜測:Gmail 身份驗證(基於瀏覽器)只是阻止您的登錄嘗試,因為它希望您使用瀏覽器而不是簡單地提交您的憑據。
IAM 解決方案做了很多后台工作來幫助保護用戶,即所謂的風險評估。 這可能需要及時將驗證碼和 MFA 質詢發送回用戶,因此 API 無法真正解決此問題,這也是他們專注於瀏覽器而不是簡單地獲取正確憑據的另一個原因。
電子郵件提供商(特別是 Gmail)非常擅長檢測可能的機器人,這將是我的第二個猜測:它將您的服務檢測為機器人並暫時保留您的帳戶以“保護您”。
這可能適用於較低的環境(又名:您的機器和/或測試環境)而不是生產,因為服務器機器人預防系統通常檢查 IP 地址,來自瀏覽器的用戶代理(如果有),API 調用率,認證率等。
在進行此類集成時,另一個可能會阻止您的事情是速率限制。 通常,每月 500 條消息。
要解決此問題並仍使用 Gmail,可以這樣做: https ://support.google.com/accounts/answer/185833?hl=en
它稱為application password
,是您在帳戶上為允許登錄的特定應用程序生成的不同密碼。這將跳過 OAuth 和機器人驗證(至少在大多數情況下)。
解決此問題的唯一可靠方法是使用適當的發送電子郵件服務。 像Amazon Simple Email Service
、 Sendgrid
或其他一堆東西。
這是因為它們是為 API 調用而創建的,不會妨礙您。 您可以在那里設置自己的域,包括SPF
和DKIM
密鑰,以便收件人的電子郵件服務器將郵件識別為安全且確實來自您的郵件(而不是隨機垃圾郵件)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.