sozsoft-platform/api/src/Sozsoft.Platform.HttpApi.Host/Identity/PlatformAccountController.cs
2026-08-21 22:27:59 +03:00

124 lines
4.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Options;
using Sozsoft.Platform.Localization;
using Volo.Abp;
using Volo.Abp.Account;
using Volo.Abp.Account.Web.Areas.Account.Controllers.Models;
using Volo.Abp.Auditing;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Identity;
using Volo.Abp.Settings;
using Volo.Abp.Validation;
using AccountController = Volo.Abp.Account.Web.Areas.Account.Controllers.AccountController;
using IdentityUser = Volo.Abp.Identity.IdentityUser;
using SignInResult = Microsoft.AspNetCore.Identity.SignInResult;
using UserLoginInfo = Volo.Abp.Account.Web.Areas.Account.Controllers.Models.UserLoginInfo;
namespace Sozsoft.Platform.Identity;
[Dependency(ReplaceServices = true)]
[ExposeServices(typeof(AccountController))]
[RemoteService(Name = AccountRemoteServiceConsts.RemoteServiceName)]
public class PlatformAccountController : AccountController
{
#region Captcha Related Props
public readonly ICaptchaManager captchaManager;
private readonly IStringLocalizer<PlatformResource> LP;
[BindProperty(Name = "g-recaptcha-response")]
[DisableAuditing]
public string CaptchaResponse { get; set; }
#endregion
public PlatformAccountController(
SignInManager<IdentityUser> signInManager,
IdentityUserManager userManager,
ISettingProvider settingProvider,
IdentitySecurityLogManager identitySecurityLogManager,
IOptions<IdentityOptions> identityOptions,
IdentityDynamicClaimsPrincipalContributorCache identityDynamicClaimsPrincipalContributorCache,
ICaptchaManager captchaManager,
IStringLocalizer<PlatformResource> LP
) : base(signInManager, userManager, settingProvider, identitySecurityLogManager, identityOptions, identityDynamicClaimsPrincipalContributorCache)
{
this.captchaManager = captchaManager;
this.LP = LP;
}
public override async Task<AbpLoginResult> Login(UserLoginInfo login)
{
await CheckLocalLoginAsync();
ValidateLoginInfo(login);
var user = await ReplaceEmailAndReturnUser(login);
if (user == null)
{
return new PlatformLoginResult(SignInResult.Failed, LP);
}
//Verify captcha if AccessFailedCount > maxAccessFailedCount
var maxAccessFailedCount = await SettingProvider.GetAsync<int>(PlatformConsts.AbpAccount.Captcha.MaxFailedAccessAttempts);
if (user.AccessFailedCount >= maxAccessFailedCount)
{
var tokenVerified = await captchaManager.VerifyCaptchaAsync(CaptchaResponse);
if (!tokenVerified)
{
return new PlatformLoginResult(PlatformLoginResultType.ShowCaptcha, LP);
}
}
var signInResult = await SignInManager.PasswordSignInAsync(
login.UserNameOrEmailAddress,
login.Password,
login.RememberMe,
true
);
await IdentitySecurityLogManager.SaveAsync(new IdentitySecurityLogContext()
{
Identity = IdentitySecurityLogIdentityConsts.Identity,
Action = signInResult.ToIdentitySecurityLogActionK(),
UserName = login.UserNameOrEmailAddress
});
var result = new PlatformLoginResult(signInResult, LP);
// Captcha yalnızca gerçek parola hatasında gösterilir. Lisans limiti, tenant pasif,
// çalışma saati gibi PreSignInCheck sonuçları ezilirse kullanıcı asıl sebebi göremez.
if (result.PResult == PlatformLoginResultType.Failed && user.AccessFailedCount >= maxAccessFailedCount)
{
result.PResult = PlatformLoginResultType.ShowCaptcha;
}
return result;
}
protected async Task<IdentityUser> ReplaceEmailAndReturnUser(UserLoginInfo login)
{
if (ValidationHelper.IsValidEmailAddress(login.UserNameOrEmailAddress))
{
var userByEmail = await UserManager.FindByEmailAsync(login.UserNameOrEmailAddress);
if (userByEmail != null)
{
login.UserNameOrEmailAddress = userByEmail.UserName;
return userByEmail;
}
}
else
{
var userByUsername = await UserManager.FindByNameAsync(login.UserNameOrEmailAddress);
if (userByUsername != null)
{
return userByUsername;
}
}
return null;
}
}