diff --git a/App/Services/Identity/Validators/AppRoleValidator.cs b/App/Services/Identity/Validators/AppRoleValidator.cs new file mode 100644 index 0000000..0e0d5b5 --- /dev/null +++ b/App/Services/Identity/Validators/AppRoleValidator.cs @@ -0,0 +1,18 @@ +using System.Threading.Tasks; +using App.Domain.Identity; +using Microsoft.AspNetCore.Identity; + +namespace App.Services.Identity.Validators +{ + public class AppRoleValidator : RoleValidator + { + public AppRoleValidator(IdentityErrorDescriber errors) : base(errors) + { + } + + public override Task ValidateAsync(RoleManager manager, Role role) + { + return base.ValidateAsync(manager, role); + } + } +} \ No newline at end of file diff --git a/App/Services/Identity/Validators/AppUserValidator.cs b/App/Services/Identity/Validators/AppUserValidator.cs new file mode 100644 index 0000000..996985c --- /dev/null +++ b/App/Services/Identity/Validators/AppUserValidator.cs @@ -0,0 +1,37 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using App.Domain.Identity; +using Microsoft.AspNetCore.Identity; + +namespace App.Services.Identity.Validators +{ + public class AppUserValidator : UserValidator + { + public AppUserValidator(IdentityErrorDescriber errors) : base(errors) + { + } + + public override async Task ValidateAsync(UserManager manager, User user) + { + var result = await base.ValidateAsync(manager, user); + + this.ValidateUserName(user,result.Errors.ToList()); + + return result; + } + + + private void ValidateUserName(User user, List errors) + { + if (user.UserName.Contains("Admin")) + { + errors.Add(new IdentityError + { + Code = "InvalidUser", + Description = "این کاربر معتبر نیست" + }); + } + } + } +} \ No newline at end of file diff --git a/App/Startup.cs b/App/Startup.cs index bdc7f43..41d2986 100644 --- a/App/Startup.cs +++ b/App/Startup.cs @@ -57,6 +57,8 @@ public void ConfigureServices(IServiceCollection services) }) .AddUserStore() .AddRoleStore() + .AddUserValidator() + .AddRoleValidator() .AddDefaultTokenProviders(); }