-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added forget/reset password feature to account module
- Loading branch information
1 parent
478e93f
commit 8614a45
Showing
29 changed files
with
626 additions
and
19 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
18 changes: 18 additions & 0 deletions
18
...s/account/src/Volo.Abp.Account.Application.Contracts/Volo/Abp/Account/ResetPasswordDto.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,18 @@ | ||
using System; | ||
using System.ComponentModel.DataAnnotations; | ||
using Volo.Abp.Auditing; | ||
|
||
namespace Volo.Abp.Account | ||
{ | ||
public class ResetPasswordDto | ||
{ | ||
public Guid UserId { get; set; } | ||
|
||
[Required] | ||
public string ResetToken { get; set; } | ||
|
||
[Required] | ||
[DisableAuditing] | ||
public string Password { get; set; } | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...t/src/Volo.Abp.Account.Application.Contracts/Volo/Abp/Account/SendPasswordResetCodeDto.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,21 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using Volo.Abp.Identity; | ||
using Volo.Abp.Validation; | ||
|
||
namespace Volo.Abp.Account | ||
{ | ||
public class SendPasswordResetCodeDto | ||
{ | ||
[Required] | ||
[EmailAddress] | ||
[DynamicStringLength(typeof(IdentityUserConsts), nameof(IdentityUserConsts.MaxEmailLength))] | ||
public string Email { get; set; } | ||
|
||
[Required] | ||
public string AppName { get; set; } | ||
|
||
public string ReturnUrl { get; set; } | ||
|
||
public string ReturnUrlHash { get; set; } | ||
} | ||
} |
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
7 changes: 7 additions & 0 deletions
7
modules/account/src/Volo.Abp.Account.Application/Volo/Abp/Account/AccountUrlNames.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,7 @@ | ||
namespace Volo.Abp.Account | ||
{ | ||
public static class AccountUrlNames | ||
{ | ||
public const string PasswordReset = "Abp.Account.PasswordReset"; | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
modules/account/src/Volo.Abp.Account.Application/Volo/Abp/Account/Emailing/AccountEmailer.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,106 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Text.Encodings.Web; | ||
using System.Threading.Tasks; | ||
using System.Web; | ||
using Microsoft.Extensions.Localization; | ||
using Volo.Abp.Account.Emailing.Templates; | ||
using Volo.Abp.Account.Localization; | ||
using Volo.Abp.DependencyInjection; | ||
using Volo.Abp.Emailing; | ||
using Volo.Abp.Identity; | ||
using Volo.Abp.MultiTenancy; | ||
using Volo.Abp.TextTemplating; | ||
using Volo.Abp.UI.Navigation.Urls; | ||
|
||
namespace Volo.Abp.Account.Emailing | ||
{ | ||
public class AccountEmailer : IAccountEmailer, ITransientDependency | ||
{ | ||
protected ITemplateRenderer TemplateRenderer { get; } | ||
protected IEmailSender EmailSender { get; } | ||
protected IStringLocalizer<AccountResource> StringLocalizer { get; } | ||
protected IAppUrlProvider AppUrlProvider { get; } | ||
protected ICurrentTenant CurrentTenant { get; } | ||
|
||
public AccountEmailer( | ||
IEmailSender emailSender, | ||
ITemplateRenderer templateRenderer, | ||
IStringLocalizer<AccountResource> stringLocalizer, | ||
IAppUrlProvider appUrlProvider, | ||
ICurrentTenant currentTenant) | ||
{ | ||
EmailSender = emailSender; | ||
StringLocalizer = stringLocalizer; | ||
AppUrlProvider = appUrlProvider; | ||
CurrentTenant = currentTenant; | ||
TemplateRenderer = templateRenderer; | ||
} | ||
|
||
public virtual async Task SendPasswordResetLinkAsync( | ||
IdentityUser user, | ||
string resetToken, | ||
string appName, | ||
string returnUrl = null, | ||
string returnUrlHash = null) | ||
{ | ||
Debug.Assert(CurrentTenant.Id == user.TenantId, "This method can only work for current tenant!"); | ||
|
||
var url = await AppUrlProvider.GetResetPasswordUrlAsync(appName); | ||
|
||
var link = $"{url}?userId={user.Id}&tenantId={user.TenantId}&resetToken={UrlEncoder.Default.Encode(resetToken)}"; | ||
|
||
if (!returnUrl.IsNullOrEmpty()) | ||
{ | ||
link += "&returnUrl=" + NormalizeReturnUrl(returnUrl); | ||
} | ||
|
||
if (!returnUrlHash.IsNullOrEmpty()) | ||
{ | ||
link += "&returnUrlHash=" + returnUrlHash; | ||
} | ||
|
||
var emailContent = await TemplateRenderer.RenderAsync( | ||
AccountEmailTemplates.PasswordResetLink, | ||
new { link = link } | ||
); | ||
|
||
await EmailSender.SendAsync( | ||
user.Email, | ||
StringLocalizer["PasswordReset"], | ||
emailContent | ||
); | ||
} | ||
|
||
private string NormalizeReturnUrl(string returnUrl) | ||
{ | ||
if (returnUrl.IsNullOrEmpty()) | ||
{ | ||
return returnUrl; | ||
} | ||
|
||
//Handling openid connect login | ||
if (returnUrl.StartsWith("/connect/authorize/callback", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
if (returnUrl.Contains("?")) | ||
{ | ||
var queryPart = returnUrl.Split('?')[1]; | ||
var queryParameters = queryPart.Split('&'); | ||
foreach (var queryParameter in queryParameters) | ||
{ | ||
if (queryParameter.Contains("=")) | ||
{ | ||
var queryParam = queryParameter.Split('='); | ||
if (queryParam[0] == "redirect_uri") | ||
{ | ||
return HttpUtility.UrlDecode(queryParam[1]); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return returnUrl; | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...Volo.Abp.Account.Application/Volo/Abp/Account/Emailing/AppUrlProviderAccountExtensions.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,13 @@ | ||
using System.Threading.Tasks; | ||
using Volo.Abp.UI.Navigation.Urls; | ||
|
||
namespace Volo.Abp.Account.Emailing | ||
{ | ||
public static class AppUrlProviderAccountExtensions | ||
{ | ||
public static Task<string> GetResetPasswordUrlAsync(this IAppUrlProvider appUrlProvider, string appName) | ||
{ | ||
return appUrlProvider.GetUrlAsync(appName, AccountUrlNames.PasswordReset); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...les/account/src/Volo.Abp.Account.Application/Volo/Abp/Account/Emailing/IAccountEmailer.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,16 @@ | ||
using System.Threading.Tasks; | ||
using Volo.Abp.Identity; | ||
|
||
namespace Volo.Abp.Account.Emailing | ||
{ | ||
public interface IAccountEmailer | ||
{ | ||
Task SendPasswordResetLinkAsync( | ||
IdentityUser user, | ||
string resetToken, | ||
string appName, | ||
string returnUrl = null, | ||
string returnUrlHash = null | ||
); | ||
} | ||
} |
Oops, something went wrong.