Search code examples
asp.net-mvcidentityusermanager

ASP.NET Identity IUserEmailStore error in MVC


I am trying to send a confirmation email when a user registers together with the generated Token. Token is OK. But when sending email,I am getting this error:

System.NotSupportedException: Store does not implement IUserEmailStore<TUser>.

And my code inside AccountController in the Register method looks like this: It is the SendEmailAsync() method which is causing the error:

if (result.Succeeded)
{
    var 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 this link: <a href=\"" + callbackUrl + "\">link</a>");
    ViewBag.Link = callbackUrl;
    return View("DisplayEmail");
}

In my IdentityConfig.cs file, I have a class:

public class ApplicationUserManager : UserManager<ApplicationUser>
{
        public ApplicationUserManager(IUserStore<ApplicationUser> store)
            : base(store)
        {
        }
}

Also inside my IdentityConfig.cs file, I have another class:

public class EmailService : IIdentityMessageService
    {
        public Task SendAsync(IdentityMessage message)
        {
            // Plug in your email service here to send an email.
            SmtpClient client = new SmtpClient();
            return client.SendMailAsync(ConfigurationManager.AppSettings["TranscriptEmailAddr"],
                                        message.Destination,
                                        message.Subject,
                                        message.Body);                       
        }
    }

Please assist.


Solution

  • I just figured out that I had Microsoft.AspNet.Identity.EntityFramework v1.0.0 installed but I needed v2.x. I installed it through the Package Manager console by entering:

    Install-Package Microsoft.AspNet.Identity.EntityFramework -Version 2.2.1
    

    In this later version, UserStore implements IUserEmailStore which has the new email methods.