私は.net 4.5.2とsendgridを使用しています。私は以下のリンクをガイドとして使用しましたが、sendgrid v2を使用するのではなく、sendgrid v3を使用しています。.netウェブアプリケーションの承認セクションは、確認メールがクリックされる前にアクセス可能です。
確認メールには、登録ユーザーの電子メールアドレスに送信されたリンクで動作します。そのリンクがクリックされると、AspNetUsersの "Email Confirmation"フィールドがfalseからtrueになります。
しかし、ユーザーが最初に登録フォームを送信すると、確認メールをクリックする前に、システムにログインするようになります。どういうわけか_LoginPartialが呼び出されているのは、ユーザーのメールアドレスとログオフがNavbarの一番上にあるからです。
ActionControllerのログインアクションは、登録直後から電子メールの確認がクリックされるまで少し前に呼び出されています。それはマイクロソフト社のドキュメントにはないと思います。
しかし、それを修正するためのアドバイスは素晴らしいでしょう。 AspNetUserテーブルでEmailConfirmation == falseを確認できました。しかし、それを行うための正しい場所はありますか?
Prevent login when EmailConfirmed is falseをチェックアウトして、デフォルトのログインアクションコードをコメントアウトし、これを以下に置き換えましたが、違いはありませんでした。
if (ModelState.IsValid)
{
var user = await UserManager.FindByNameAsync(model.Email);
if (user == null)
{
ModelState.AddModelError("", "Invalid login attempt.");
return View(model);
}
//Add this to check if the email was confirmed.
if (!await UserManager.IsEmailConfirmedAsync(user.Id))
{
ModelState.AddModelError("", "You need to confirm your email.");
return View(model);
}
if (await UserManager.IsLockedOutAsync(user.Id))
{
return View("Lockout");
}
if (await UserManager.CheckPasswordAsync(user, model.Password))
{
// Uncomment to enable lockout when password login fails
//await UserManager.ResetAccessFailedCountAsync(user.Id);
return await LoginCommon(user, model.RememberMe, returnUrl);
}
else
{
// Uncomment to enable lockout when password login fails
//await UserManager.AccessFailedAsync(user.Id);
ModelState.AddModelError("", "Invalid login attempt.");
return View(model);
}
}
// If we got this far, something failed, redisplay form
return View(model);
レジスタアクション:
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser {UserName = model.Email, Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
// Send an email with this link
/*These bottom three lines were commented out */
string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\"></a>");
return RedirectToAction("ConfirmRegistration");
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
ログインアクション:
await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
無効になります:あなたのRegister
アクションで
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
return View(model);
}
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, change to shouldLockout: true
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
return RedirectToLocal(returnUrl);
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.RequiresVerification:
return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
case SignInStatus.Failure:
default:
ModelState.AddModelError("", "Invalid login attempt.");
return View(model);
}
}