Search code examples
asp.net-mvc-5asp.net-identity

UserManager.SendEmailAsync, can I safely ignore it?


I find that Asp.Net Identity 2.0's UserManager.SendEmailAsync is too crude for the things I want to do. (Edit: MVC5) I'd like to setup a mail template with some HTML. I don't want the caller of SendEmailAsync to worry about this.

So, I'm on my way to declaring that I won't be using this method, at all. I've decided to create a separate assembly and service that will take care of everything, and only exposing one method, something like someService.ScheduleMessage(1 /* userId */, MessageKindEnum.FORGOT_PASSWORD, new SomeRelevantData(verificationCode), Scheduling.IMMEDIATELY).

Now, before I rush off to do this, is there any reason why it would be a bad thing to ignore UserManager.SendEmailAsync? To me it seems like a utility method, nothing more. I even fail to see why it's in the UserManager at all. Am I missing something or is it fine to do this?


Solution

  • I presume you are talking about Identity V2 for MVC5, not Asp.Net Core.

    If you look through source code of this method, you'll see that this is a thin wrapper around implementation of IIdentityMessageService. So you are quite correct in calling this a utility method and I quite agree with sentiment that it does not really belong there.

    However, this method is called internally by EmailTokenProvider that is in turn called by UserManager when issuing 2-FactorAuthentication. So it is needed only when you are using 2FA. If you are not using 2FA, then you'll be fine with using your own implementation.

    Another option is to provide your own implementation of IIdentityMessageService where you can do (though limited by incoming parameters) pretty much what you want to do. And then provide your own implementation in constructor of UserManager.