我有一个简单的ASP.NET应用程序,我试图允许用户使用外部身份验证提供程序登录。到目前为止,我已经成功实现了google auth,但是,登录后要求用户确认电子邮件。我想跳过这一步。
我尝试将config与
一起使用 services.AddIdentity<ApplicationUser, IdentityRole>(config =>
{
config.SignIn.RequireConfirmedEmail = false;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
但没有运气。我也尝试修改ExternalLoginCallback和ExternalLoginConfirmation,但是我总是把它弄得更乱。
有人能这么善良地分享我创建新用户而无需确认其电子邮件所必须使用的实际代码吗?
答案 0 :(得分:1)
要跳过电子邮件关联过程,可以将ExternalLoginCallback
修改为:
[HttpGet]
[AllowAnonymous]
public async Task<IActionResult> ExternalLoginCallback(string returnUrl = null, string remoteError = null)
{
if (remoteError != null)
{
ErrorMessage = $"Error from external provider: {remoteError}";
return RedirectToAction(nameof(Login));
}
var info = await _signInManager.GetExternalLoginInfoAsync();
if (info == null)
{
return RedirectToAction(nameof(Login));
}
// Sign in the user with this external login provider if the user already has a login.
var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent: false, bypassTwoFactor: true);
if (result.Succeeded)
{
_logger.LogInformation("User logged in with {Name} provider.", info.LoginProvider);
return RedirectToLocal(returnUrl);
}
if (result.IsLockedOut)
{
return RedirectToAction(nameof(Lockout));
}
else
{
var userEmail = info.Principal.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Email).Value.ToString();
var user = new ApplicationUser { UserName = userEmail, Email = userEmail };
var resultCreateUser = await _userManager.CreateAsync(user);
if (resultCreateUser.Succeeded)
{
var resultAddLogin = await _userManager.AddLoginAsync(user, info);
if (resultAddLogin.Succeeded)
{
await _signInManager.SignInAsync(user, isPersistent: false);
_logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider);
return RedirectToLocal(returnUrl);
}
}
}
// If the user does not have an account, then ask the user to create an account.
ViewData["ReturnUrl"] = returnUrl;
ViewData["LoginProvider"] = info.LoginProvider;
var email = info.Principal.FindFirstValue(ClaimTypes.Email);
return View("ExternalLogin", new ExternalLoginViewModel { Email = email });
}
这将跳过关联表单,并自动使用外部用户的电子邮件名。