-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
template: add local auth and email sending (#484)
- Add Individual user accounts (local auth) as a template option - Add Azure ACS as an email option - Add SendGrid as an email option - Add `reset` method to API callers - Switch external logins in template to use ExternalLogin razor page instead of OnTicketReceived hooks
- Loading branch information
Showing
54 changed files
with
1,973 additions
and
530 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
...tes/Coalesce.Vue.Template/content/Coalesce.Starter.Vue.Data/Auth/UserManagementService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
using Coalesce.Starter.Vue.Data.Communication; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Mvc; | ||
using System.Diagnostics; | ||
using System.Net.Mail; | ||
using System.Text.Encodings.Web; | ||
|
||
namespace Coalesce.Starter.Vue.Data.Auth; | ||
|
||
public class UserManagementService( | ||
UserManager<User> _userManager, | ||
IUrlHelper urlHelper, | ||
IEmailService emailSender | ||
) | ||
{ | ||
public async Task<ItemResult> SendEmailConfirmationRequest(User user) | ||
{ | ||
if (user.EmailConfirmed) return "Email is already confirmed."; | ||
if (string.IsNullOrWhiteSpace(user.Email)) return "User has no email"; | ||
|
||
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); | ||
|
||
var link = urlHelper.PageLink("/ConfirmEmail", values: new { userId = user.Id, code = code })!; | ||
|
||
var result = await emailSender.SendEmailAsync( | ||
user.Email, | ||
"Confirm your email", | ||
$""" | ||
Please <a href="{HtmlEncoder.Default.Encode(link)}">click here</a> to confirm your account. | ||
If you didn't request this, ignore this email and do not click the link. | ||
""" | ||
); | ||
|
||
if (result.WasSuccessful) | ||
{ | ||
result.Message += " Please click the link in the email to confirm your account."; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public async Task<ItemResult> SendEmailChangeRequest(User user, string newEmail) | ||
{ | ||
// This is secured by virtue of the filtering done in the [DefaultDataSource]. | ||
// Regular users can only fetch themselves out of the data source, | ||
// admins can only view users in their own tenant, | ||
// and tenant admins can view everyone. | ||
|
||
if (string.IsNullOrEmpty(newEmail) || !MailAddress.TryCreate(newEmail, out _)) | ||
{ | ||
return "New email is not valid."; | ||
} | ||
|
||
if (string.Equals(user.Email, newEmail, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return "New email is not different."; | ||
} | ||
|
||
var code = await _userManager.GenerateChangeEmailTokenAsync(user, newEmail); | ||
|
||
var link = urlHelper.PageLink("/ConfirmEmail", values: new { userId = user.Id, code = code, newEmail = newEmail })!; | ||
|
||
var result = await emailSender.SendEmailAsync( | ||
newEmail, | ||
"Confirm your email", | ||
$""" | ||
Please <a href="{HtmlEncoder.Default.Encode(link)}">click here</a> to complete your email change request. | ||
If you didn't request this, ignore this email and do not click the link. | ||
""" | ||
); | ||
|
||
if (result.WasSuccessful) | ||
{ | ||
result.Message += " Please click the link in the email to complete the change."; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public async Task<ItemResult> SendPasswordResetRequest(User? user) | ||
{ | ||
if (user?.Email is not null) | ||
{ | ||
var code = await _userManager.GeneratePasswordResetTokenAsync(user); | ||
|
||
var link = urlHelper.PageLink("ResetPassword", values: new { userId = user.Id, code = code })!; | ||
|
||
await emailSender.SendEmailAsync( | ||
user.Email, | ||
"Password Reset", | ||
$""" | ||
Please <a href="{HtmlEncoder.Default.Encode(link)}">click here</a> to reset your password. | ||
If you didn't request this, ignore this email and do not click the link. | ||
""" | ||
); | ||
} | ||
|
||
return new ItemResult(true, | ||
"If the user account exists, the email address on the account " + | ||
"will receive an email shortly with password reset instructions."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.