From 80ea30be0a63e634495314b12433a72201cf2d9b Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Mon, 23 May 2022 18:30:14 +0800 Subject: [PATCH] Improved --- .../ExternalLoginProviderWithPasswordBase.cs | 27 ++++++++++++++++--- .../IExternalLoginProviderWithPassword.cs | 2 ++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/ExternalLoginProviderWithPasswordBase.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/ExternalLoginProviderWithPasswordBase.cs index 612091f8ea..e791004fd0 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/ExternalLoginProviderWithPasswordBase.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/ExternalLoginProviderWithPasswordBase.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.Options; using Volo.Abp.Guids; @@ -8,22 +9,31 @@ namespace Volo.Abp.Identity; public abstract class ExternalLoginProviderWithPasswordBase : ExternalLoginProviderBase, IExternalLoginProviderWithPassword { + public bool CanObtainUserInfoWithoutPassword { get; } + public ExternalLoginProviderWithPasswordBase( IGuidGenerator guidGenerator, ICurrentTenant currentTenant, IdentityUserManager userManager, IIdentityUserRepository identityUserRepository, - IOptions identityOptions) : + IOptions identityOptions, + bool canObtainUserInfoWithoutPassword = false) : base(guidGenerator, currentTenant, userManager, identityUserRepository, identityOptions) { + CanObtainUserInfoWithoutPassword = canObtainUserInfoWithoutPassword; } public async Task CreateUserAsync(string userName, string providerName, string plainPassword) { + if (CanObtainUserInfoWithoutPassword) + { + return await CreateUserAsync(userName, providerName); + } + await IdentityOptions.SetAsync(); var externalUser = await GetUserInfoAsync(userName, plainPassword); @@ -33,13 +43,24 @@ public abstract class ExternalLoginProviderWithPasswordBase : ExternalLoginProvi public async Task UpdateUserAsync(IdentityUser user, string providerName, string plainPassword) { + if (CanObtainUserInfoWithoutPassword) + { + await UpdateUserAsync(user, providerName); + return; + } + await IdentityOptions.SetAsync(); var externalUser = await GetUserInfoAsync(user, plainPassword); await UpdateUserAsync(user, externalUser, providerName); } - + + protected override Task GetUserInfoAsync(string userName) + { + throw new NotImplementedException($"{nameof(GetUserInfoAsync)} is not implemented default. It should be overriden and implemented by the deriving class!"); + } + protected abstract Task GetUserInfoAsync(string userName, string plainPassword); protected virtual Task GetUserInfoAsync(IdentityUser user, string plainPassword) diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IExternalLoginProviderWithPassword.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IExternalLoginProviderWithPassword.cs index ae91079a76..fa811c852e 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IExternalLoginProviderWithPassword.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IExternalLoginProviderWithPassword.cs @@ -4,6 +4,8 @@ namespace Volo.Abp.Identity { public interface IExternalLoginProviderWithPassword { + bool CanObtainUserInfoWithoutPassword { get; } + /// /// This method is called when a user is authenticated by this source but the user does not exists yet. /// So, the source should create the user and fill the properties.