Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e79a083

Browse filesBrowse files
committed
refactor external account confirmation
1 parent 4e56a6b commit e79a083
Copy full SHA for e79a083

File tree

Expand file treeCollapse file tree

2 files changed

+12
-29
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+12
-29
lines changed

‎AspNetCoreIdentity/Controllers/AccountController.cs

Copy file name to clipboardExpand all lines: AspNetCoreIdentity/Controllers/AccountController.cs
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ await _emailSender.SendEmailAsync(user.Email, "Confirm your email",
8181
return new ResultVM
8282
{
8383
Status = Status.Success,
84-
Message = "User has been created, please confirm your email",
84+
Message = "Email confirmation is pending",
8585
Data = user
8686
};
8787
}

‎AspNetCoreIdentity/Controllers/ExternalAccountController.cs

Copy file name to clipboardExpand all lines: AspNetCoreIdentity/Controllers/ExternalAccountController.cs
+11-28Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public IActionResult Index()
3535
[HttpGet]
3636
public IActionResult Login(string provider, string returnUrl = null)
3737
{
38-
var redirectUrl = "/ExternalAccount/Callback";
38+
var redirectUrl = Url.Action("Callback", "ExternalAccount");
3939
var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl);
4040
return new ChallengeResult(provider, properties);
4141
}
@@ -46,7 +46,6 @@ public async Task<IActionResult> Callback(string returnUrl = null, string remote
4646
returnUrl = returnUrl ?? Url.Content("~/");
4747
if (remoteError != null)
4848
{
49-
//ErrorMessage = $"Error from external provider: {remoteError}";
5049
return RedirectToPage("./", new { ReturnUrl = returnUrl });
5150
}
5251

@@ -64,40 +63,24 @@ public async Task<IActionResult> Callback(string returnUrl = null, string remote
6463
return LocalRedirect(returnUrl);
6564
}
6665

67-
68-
// If the user does not have an account, create one.
6966
var userEmail = info.Principal.FindFirstValue(ClaimTypes.Email);
7067

71-
var user = new IdentityUser { Id = Guid.NewGuid().ToString(), UserName = userEmail, Email = userEmail };
72-
7368
var userDb = await _userManager.FindByEmailAsync(userEmail);
7469

7570
if (userDb != null)
7671
{
77-
var emailConfirmed = await _userManager.IsEmailConfirmedAsync(userDb);
78-
79-
if (!result.IsNotAllowed)
80-
{
81-
var newLoginResult = await _userManager.AddLoginAsync(userDb, info);
82-
if (newLoginResult.Succeeded)
83-
{
84-
// Check Email Confirmation
85-
if (emailConfirmed)
86-
{
87-
await _signInManager.SignInAsync(userDb, isPersistent: false);
88-
return LocalRedirect(
89-
$"{returnUrl}?message={info.ProviderDisplayName} has been added successfully");
90-
}
91-
92-
return LocalRedirect(
93-
$"{returnUrl}?message={info.ProviderDisplayName} has been added but email confirmation is pending");
94-
}
95-
}
96-
else
72+
if (!userDb.EmailConfirmed)
9773
{
98-
return LocalRedirect(
99-
$"{returnUrl}?message=Email ({user.Email}) confirmation is pending&type=danger");
74+
var code = await _userManager.GenerateEmailConfirmationTokenAsync(userDb);
75+
await _userManager.ConfirmEmailAsync(userDb, code);
10076
}
77+
78+
// Add the external provider
79+
await _userManager.AddLoginAsync(userDb, info);
80+
81+
await _signInManager.SignInAsync(userDb, isPersistent: false);
82+
return LocalRedirect(
83+
$"{returnUrl}?message={info.ProviderDisplayName} has been added successfully");
10184
}
10285

10386
return LocalRedirect($"/register?associate={userEmail}&loginProvider={info.LoginProvider}&providerDisplayName={info.ProviderDisplayName}&providerKey={info.ProviderKey}");

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.