Send email confirmation after register via external login (#8013)
This commit is contained in:
parent
def25cb3de
commit
4e271ac124
|
|
@ -4,9 +4,11 @@
|
||||||
using System;
|
using System;
|
||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
|
using System.Text.Encodings.Web;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Identity.UI.Services;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
@ -94,19 +96,22 @@ namespace Microsoft.AspNetCore.Identity.UI.V3.Pages.Account.Internal
|
||||||
private readonly UserManager<TUser> _userManager;
|
private readonly UserManager<TUser> _userManager;
|
||||||
private readonly IUserStore<TUser> _userStore;
|
private readonly IUserStore<TUser> _userStore;
|
||||||
private readonly IUserEmailStore<TUser> _emailStore;
|
private readonly IUserEmailStore<TUser> _emailStore;
|
||||||
|
private readonly IEmailSender _emailSender;
|
||||||
private readonly ILogger<ExternalLoginModel> _logger;
|
private readonly ILogger<ExternalLoginModel> _logger;
|
||||||
|
|
||||||
public ExternalLoginModel(
|
public ExternalLoginModel(
|
||||||
SignInManager<TUser> signInManager,
|
SignInManager<TUser> signInManager,
|
||||||
UserManager<TUser> userManager,
|
UserManager<TUser> userManager,
|
||||||
IUserStore<TUser> userStore,
|
IUserStore<TUser> userStore,
|
||||||
ILogger<ExternalLoginModel> logger)
|
ILogger<ExternalLoginModel> logger,
|
||||||
|
IEmailSender emailSender)
|
||||||
{
|
{
|
||||||
_signInManager = signInManager;
|
_signInManager = signInManager;
|
||||||
_userManager = userManager;
|
_userManager = userManager;
|
||||||
_userStore = userStore;
|
_userStore = userStore;
|
||||||
_emailStore = GetEmailStore();
|
_emailStore = GetEmailStore();
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
|
_emailSender = emailSender;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override IActionResult OnGet()
|
public override IActionResult OnGet()
|
||||||
|
|
@ -190,6 +195,18 @@ namespace Microsoft.AspNetCore.Identity.UI.V3.Pages.Account.Internal
|
||||||
{
|
{
|
||||||
await _signInManager.SignInAsync(user, isPersistent: false);
|
await _signInManager.SignInAsync(user, isPersistent: false);
|
||||||
_logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider);
|
_logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider);
|
||||||
|
|
||||||
|
var userId = await _userManager.GetUserIdAsync(user);
|
||||||
|
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
|
||||||
|
var callbackUrl = Url.Page(
|
||||||
|
"/Account/ConfirmEmail",
|
||||||
|
pageHandler: null,
|
||||||
|
values: new { userId = userId, code = code },
|
||||||
|
protocol: Request.Scheme);
|
||||||
|
|
||||||
|
await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
|
||||||
|
$"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");
|
||||||
|
|
||||||
return LocalRedirect(returnUrl);
|
return LocalRedirect(returnUrl);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,11 @@
|
||||||
using System;
|
using System;
|
||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
|
using System.Text.Encodings.Web;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Identity.UI.Services;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
@ -93,19 +95,22 @@ namespace Microsoft.AspNetCore.Identity.UI.V4.Pages.Account.Internal
|
||||||
private readonly UserManager<TUser> _userManager;
|
private readonly UserManager<TUser> _userManager;
|
||||||
private readonly IUserStore<TUser> _userStore;
|
private readonly IUserStore<TUser> _userStore;
|
||||||
private readonly IUserEmailStore<TUser> _emailStore;
|
private readonly IUserEmailStore<TUser> _emailStore;
|
||||||
|
private readonly IEmailSender _emailSender;
|
||||||
private readonly ILogger<ExternalLoginModel> _logger;
|
private readonly ILogger<ExternalLoginModel> _logger;
|
||||||
|
|
||||||
public ExternalLoginModel(
|
public ExternalLoginModel(
|
||||||
SignInManager<TUser> signInManager,
|
SignInManager<TUser> signInManager,
|
||||||
UserManager<TUser> userManager,
|
UserManager<TUser> userManager,
|
||||||
IUserStore<TUser> userStore,
|
IUserStore<TUser> userStore,
|
||||||
ILogger<ExternalLoginModel> logger)
|
ILogger<ExternalLoginModel> logger,
|
||||||
|
IEmailSender emailSender)
|
||||||
{
|
{
|
||||||
_signInManager = signInManager;
|
_signInManager = signInManager;
|
||||||
_userManager = userManager;
|
_userManager = userManager;
|
||||||
_userStore = userStore;
|
_userStore = userStore;
|
||||||
_emailStore = GetEmailStore();
|
_emailStore = GetEmailStore();
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
|
_emailSender = emailSender;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override IActionResult OnGet()
|
public override IActionResult OnGet()
|
||||||
|
|
@ -189,6 +194,18 @@ namespace Microsoft.AspNetCore.Identity.UI.V4.Pages.Account.Internal
|
||||||
{
|
{
|
||||||
await _signInManager.SignInAsync(user, isPersistent: false);
|
await _signInManager.SignInAsync(user, isPersistent: false);
|
||||||
_logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider);
|
_logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider);
|
||||||
|
|
||||||
|
var userId = await _userManager.GetUserIdAsync(user);
|
||||||
|
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
|
||||||
|
var callbackUrl = Url.Page(
|
||||||
|
"/Account/ConfirmEmail",
|
||||||
|
pageHandler: null,
|
||||||
|
values: new { userId = userId, code = code },
|
||||||
|
protocol: Request.Scheme);
|
||||||
|
|
||||||
|
await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
|
||||||
|
$"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");
|
||||||
|
|
||||||
return LocalRedirect(returnUrl);
|
return LocalRedirect(returnUrl);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<Description>Identity sample MVC application on ASP.NET Core using the default UI</Description>
|
<Description>Identity sample MVC application on ASP.NET Core using the default UI</Description>
|
||||||
|
|
@ -18,6 +18,7 @@
|
||||||
<Reference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" />
|
<Reference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" />
|
||||||
<Reference Include="Microsoft.AspNetCore.Identity.UI" />
|
<Reference Include="Microsoft.AspNetCore.Identity.UI" />
|
||||||
<Reference Include="Microsoft.AspNetCore.Mvc" />
|
<Reference Include="Microsoft.AspNetCore.Mvc" />
|
||||||
|
<Reference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" />
|
||||||
<Reference Include="Microsoft.AspNetCore.Server.IISIntegration" />
|
<Reference Include="Microsoft.AspNetCore.Server.IISIntegration" />
|
||||||
<Reference Include="Microsoft.AspNetCore.Server.Kestrel" />
|
<Reference Include="Microsoft.AspNetCore.Server.Kestrel" />
|
||||||
<Reference Include="Microsoft.AspNetCore.StaticFiles" />
|
<Reference Include="Microsoft.AspNetCore.StaticFiles" />
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,7 @@ namespace IdentitySample.DefaultUI
|
||||||
.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
|
.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
|
||||||
x => x.MigrationsAssembly("IdentitySample.DefaultUI")));
|
x => x.MigrationsAssembly("IdentitySample.DefaultUI")));
|
||||||
|
|
||||||
services.AddMvc();
|
services.AddMvc().AddNewtonsoftJson();
|
||||||
|
|
||||||
services.AddDefaultIdentity<ApplicationUser>()
|
services.AddDefaultIdentity<ApplicationUser>()
|
||||||
.AddRoles<IdentityRole>()
|
.AddRoles<IdentityRole>()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue