まず、トークンが正しいかどうかを判断する必要があります。 あなたの生成トークンで、そのトークンをメモし、その後にデータベースをチェックしてください。
string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
UserManager.EmailService = new EmailService();
await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
これは私のチェック用のメールサービスです。
public class EmailService : IIdentityMessageService
{
public System.Threading.Tasks.Task SendAsync(IdentityMessage message)
{
// Plug in your email service here to send an email.
var mail = new MailMessage
{
Subject = message.Subject,
Body = message.Body,
IsBodyHtml = true
};
mail.To.Add(message.Destination);
mail.From = new MailAddress("[email protected]","me");
var smtp = new SmtpClient
{
Host = "smtp.mail.com",
Port = 25,
UseDefaultCredentials = false,
Credentials = new System.Net.NetworkCredential("[email protected]", "password"),
EnableSsl = true
};
// Enter seders User name and password
smtp.Send(mail);
return System.Threading.Tasks.Task.FromResult(0);
}
電子メールの確認リンクが表示されたら、それはcallbackurlと一致する必要があります。 urlをクリックすると、このメソッドが呼び出されます。
[AllowAnonymous]
public async Task<ActionResult> ConfirmEmail(string userId, string code)
{
if (userId == null || code == null)
return View("Error");
var result = await UserManager.ConfirmEmailAsync(userId, code);
return View(result.Succeeded ? "ConfirmEmail" : "Error");
}
この方法では、コード変数がIDテーブルのコードと一致する必要があります。
また、Identity Frameworkでは重要な問題があります。 MVCには依存性のある注入コンテナなどはありません。独自のIoCを作成する必要があります。私はいつもこれを行うために構造マップを好む。生涯のマネージャーは他よりも優れているので(ninject、unityなど)。また、データベースまたはセッションマネージャへのリクエストを減らすために、現在のユーザーオブジェクトを使用しています。私のMVCレジストリで
public class CurrentUser : ICurrentUser
{
private readonly ApplicationDbContext _context;
private readonly IIdentity _identity;
private ApplicationUser _user;
public CurrentUser(IIdentity identity, ApplicationDbContext context)
{
_identity = identity;
_context = context;
}
public ApplicationUser User
{
get { return _user ?? (_user = _context.Users.Find(_identity.GetUserId())); }
}
}
(のStructureMapのドキュメントを確認してください)
は、私はいつもにも、私は、オブジェクト参照の問題とタイトを防ぐため、同じユーザーに対して同じオブジェクトを使用して、私はこれで
public class MvcRegistry : Registry
{
public MvcRegistry()
{
For<BundleCollection>().Use(BundleTable.Bundles);
For<RouteCollection>().Use(RouteTable.Routes);
For<IIdentity>().Use(() => HttpContext.Current.User.Identity);
For<IUserStore<ApplicationUser>>().Use<UserStore<ApplicationUser>>();
For<DbContext>().Use(() => new ApplicationDbContext());
For<IAuthenticationManager>().Use(() => HttpContext.Current.GetOwinContext().Authentication);
For<HttpSessionStateBase>().Use(() => new HttpSessionStateWrapper(HttpContext.Current.Session));
For<HttpContextBase>().Use(() => new HttpContextWrapper(HttpContext.Current));
For<HttpServerUtilityBase>().Use(() => new HttpServerUtilityWrapper(HttpContext.Current.Server));
}
}
に必要なすべてを登録しますカップリングの問題もある。
どのように失敗しますか?それはどこで失敗するのですか? – Heki
ここでスクリーンショットを投稿します。 –
私はどのラインのラインに沿って考えていたのですが、メッセージはスクリーンショットではなく、何かですが、先に進む: – Heki