Browse Source

Merge pull request #12712 from abpframework/liangshiwei/externalLogin

Add IExternalLoginProviderWithPassword
pull/12799/head
maliming 4 years ago
committed by GitHub
parent
commit
e6de48a7de
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      modules/identity/src/Volo.Abp.Identity.AspNetCore/Volo/Abp/Identity/AspNetCore/AbpSignInManager.cs
  2. 19
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/ExternalLoginProviderBase.cs
  3. 70
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/ExternalLoginProviderWithPasswordBase.cs
  4. 4
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IExternalLoginProvider.cs
  5. 28
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IExternalLoginProviderWithPassword.cs

18
modules/identity/src/Volo.Abp.Identity.AspNetCore/Volo/Abp/Identity/AspNetCore/AbpSignInManager.cs

@ -49,11 +49,25 @@ public class AbpSignInManager : SignInManager<IdentityUser>
var user = await UserManager.FindByNameAsync(userName);
if (user == null)
{
user = await externalLoginProvider.CreateUserAsync(userName, externalLoginProviderInfo.Name);
if (externalLoginProvider is IExternalLoginProviderWithPassword externalLoginProviderWithPassword)
{
user = await externalLoginProviderWithPassword.CreateUserAsync(userName, externalLoginProviderInfo.Name, password);
}
else
{
user = await externalLoginProvider.CreateUserAsync(userName, externalLoginProviderInfo.Name);
}
}
else
{
await externalLoginProvider.UpdateUserAsync(user, externalLoginProviderInfo.Name);
if (externalLoginProvider is IExternalLoginProviderWithPassword externalLoginProviderWithPassword)
{
await externalLoginProviderWithPassword.UpdateUserAsync(user, externalLoginProviderInfo.Name, password);
}
else
{
await externalLoginProvider.UpdateUserAsync(user, externalLoginProviderInfo.Name);
}
}
return await SignInOrTwoFactorAsync(user, isPersistent);

19
modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/ExternalLoginProviderBase.cs

@ -16,7 +16,6 @@ public abstract class ExternalLoginProviderBase : IExternalLoginProvider
protected IdentityUserManager UserManager { get; }
protected IIdentityUserRepository IdentityUserRepository { get; }
protected IOptions<IdentityOptions> IdentityOptions { get; }
protected ExternalLoginProviderBase(
IGuidGenerator guidGenerator,
ICurrentTenant currentTenant,
@ -32,7 +31,7 @@ public abstract class ExternalLoginProviderBase : IExternalLoginProvider
}
public abstract Task<bool> TryAuthenticateAsync(string userName, string plainPassword);
public abstract Task<bool> IsEnabledAsync();
public virtual async Task<IdentityUser> CreateUserAsync(string userName, string providerName)
@ -40,8 +39,14 @@ public abstract class ExternalLoginProviderBase : IExternalLoginProvider
await IdentityOptions.SetAsync();
var externalUser = await GetUserInfoAsync(userName);
NormalizeExternalLoginUserInfo(externalUser, userName);
return await CreateUserAsync(externalUser, userName, providerName);
}
protected virtual async Task<IdentityUser> CreateUserAsync(ExternalLoginUserInfo externalUser, string userName, string providerName)
{
NormalizeExternalLoginUserInfo(externalUser, userName);
var user = new IdentityUser(
GuidGenerator.Create(),
userName,
@ -81,8 +86,14 @@ public abstract class ExternalLoginProviderBase : IExternalLoginProvider
public virtual async Task UpdateUserAsync(IdentityUser user, string providerName)
{
await IdentityOptions.SetAsync();
var externalUser = await GetUserInfoAsync(user);
await UpdateUserAsync(user, externalUser, providerName);
}
protected virtual async Task UpdateUserAsync(IdentityUser user, ExternalLoginUserInfo externalUser ,string providerName)
{
NormalizeExternalLoginUserInfo(externalUser, user.UserName);
if (!externalUser.Name.IsNullOrWhiteSpace())

70
modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/ExternalLoginProviderWithPasswordBase.cs

@ -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);
}
}

4
modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IExternalLoginProvider.cs

@ -28,10 +28,10 @@ public interface IExternalLoginProvider
/// <param name="providerName">The name of this provider</param>
/// <param name="user">The user that can be updated</param>
Task UpdateUserAsync(IdentityUser user, string providerName);
/// <summary>
/// Return a value indicating whether this source is enabled.
/// </summary>
/// <returns></returns>
Task<bool> IsEnabledAsync();
}
}

28
modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IExternalLoginProviderWithPassword.cs

@ -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…
Cancel
Save