mirror of https://github.com/abpframework/abp.git
committed by
GitHub
5 changed files with 131 additions and 8 deletions
@ -0,0 +1,70 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Identity; |
|||
using Microsoft.Extensions.Options; |
|||
using Volo.Abp.Guids; |
|||
using Volo.Abp.MultiTenancy; |
|||
|
|||
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> identityOptions, |
|||
bool canObtainUserInfoWithoutPassword = false) : |
|||
base(guidGenerator, |
|||
currentTenant, |
|||
userManager, |
|||
identityUserRepository, |
|||
identityOptions) |
|||
{ |
|||
CanObtainUserInfoWithoutPassword = canObtainUserInfoWithoutPassword; |
|||
} |
|||
|
|||
public async Task<IdentityUser> CreateUserAsync(string userName, string providerName, string plainPassword) |
|||
{ |
|||
if (CanObtainUserInfoWithoutPassword) |
|||
{ |
|||
return await CreateUserAsync(userName, providerName); |
|||
} |
|||
|
|||
await IdentityOptions.SetAsync(); |
|||
|
|||
var externalUser = await GetUserInfoAsync(userName, plainPassword); |
|||
|
|||
return await CreateUserAsync(externalUser, userName, providerName); |
|||
} |
|||
|
|||
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<ExternalLoginUserInfo> 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<ExternalLoginUserInfo> GetUserInfoAsync(string userName, string plainPassword); |
|||
|
|||
protected virtual Task<ExternalLoginUserInfo> GetUserInfoAsync(IdentityUser user, string plainPassword) |
|||
{ |
|||
return GetUserInfoAsync(user.UserName, plainPassword); |
|||
} |
|||
} |
|||
@ -0,0 +1,28 @@ |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.Abp.Identity |
|||
{ |
|||
public interface IExternalLoginProviderWithPassword |
|||
{ |
|||
bool CanObtainUserInfoWithoutPassword { get; } |
|||
|
|||
/// <summary>
|
|||
/// 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.
|
|||
/// </summary>
|
|||
/// <param name="userName">User name</param>
|
|||
/// <param name="providerName">The name of this provider</param>
|
|||
/// <param name="plainPassword">The plain password of the user</param>
|
|||
/// <returns>Newly created user</returns>
|
|||
Task<IdentityUser> CreateUserAsync(string userName, string providerName, string plainPassword); |
|||
|
|||
/// <summary>
|
|||
/// This method is called after an existing user is authenticated by this source.
|
|||
/// It can be used to update some properties of the user by the source.
|
|||
/// </summary>
|
|||
/// <param name="providerName">The name of this provider</param>
|
|||
/// <param name="user">The user that can be updated</param>
|
|||
/// <param name="plainPassword">The plain password of the user</param>
|
|||
Task UpdateUserAsync(IdentityUser user, string providerName, string plainPassword); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue