To maintain full control over the Identity UI in an ASP.NET Core 2.1 MVC project, scaffold the identity using the Windows version of Visual Studio 2017.
Scaffold the Identity
- Right click the web application project and select Add, New Scaffolded Item…
- On the left side of the Add Scaffold dialog select Identity, and click the Add button
- Select your existing Layout page, so the scaffolding will not overwrite it
- Select the Identity functions (files) you want to override, and click the down arrow to select your existing data context class
- The MY_SOLUTION\MY_WEBAPP\ScaffoldingReadme.txt file contains tasks to do to set up the Identity UI. Based on that
- Add the following lines to the Configure method of the Startup.cs file in this order. If a line is already in the file, make sure the order is correct.
app.UseStaticFiles() app.UseAuthentication() app.UseMvc()
- Add to the ConfigureServices method of the Startup.cs file, if the line is not there.
services.AddMvc()
- Add the following lines to the Configure method of the Startup.cs file in this order. If a line is already in the file, make sure the order is correct.
- The Aeras, Identity, Pages, Account folder contains the .cshtml files you can modify.
- The code is in the .cshtml.cs file
Use your own SMTP server
To configure Identity to use you own SMTP server to send the Forgot my password and email verification emails, register a custom email sender.
- Add to the ConfigureServices method of the Startup.cs file
// using Microsoft.AspNetCore.Identity.UI.Services; services.AddSingleton<IEmailSender, EmailSender>();
- Create the EmailSender class in the web application project
// using Microsoft.AspNetCore.Identity.UI.Services; // using System.Threading.Tasks; public class EmailSender : IEmailSender { public Task SendEmailAsync(string sTo, string sSubject, string sMessage) { MyMessagingClass messaging = new MyMessagingClass(); // Send an email message with SMTP messaging.MySendEmailMethod(sTo, sSubject, sMessage); return Task.CompletedTask; } }
For more information see https://docs.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-2.1&tabs=visual-studio#scaffold-identity-into-a-razor-project-with-authorization