gpt4 book ai didi

ASP.NET core 2 如何跳过身份关联表?

转载 作者:行者123 更新时间:2023-12-04 15:45:47 26 4
gpt4 key购买 nike

我有一个简单的 ASP.NET 应用程序,我正在尝试允许用户使用外部身份验证提供程序登录。到目前为止,我已经设法实现了 google 身份验证,但是,登录后需要用户确认电子邮件。我想跳过这一步。

我试过使用配置

 services.AddIdentity<ApplicationUser, IdentityRole>(config =>
{
config.SignIn.RequireConfirmedEmail = false;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();

但没有运气。我还尝试修改 ExternalLoginCallback 和 ExternalLoginConfirmation 但是我总是把它搞得更糟。

谁能如此友善并分享我必须使用的实际代码,以便在无需确认他的电子邮件的情况下创建新用户?

最佳答案

要跳过电子邮件关联过程,您可以将 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 });
}

这将跳过关联表单并自动使用外部用户的电子邮件名称。

关于ASP.NET core 2 如何跳过身份关联表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55853636/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com