Skip to content

Commit

Permalink
Add custome vaildator
Browse files Browse the repository at this point in the history
  • Loading branch information
dev.soheilalizadeh committed Dec 17, 2018
1 parent b86275d commit 23362d2
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
18 changes: 18 additions & 0 deletions App/Services/Identity/Validators/AppRoleValidator.cs
Original file line number Diff line number Diff line change
@@ -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<Role>
{
public AppRoleValidator(IdentityErrorDescriber errors) : base(errors)
{
}

public override Task<IdentityResult> ValidateAsync(RoleManager<Role> manager, Role role)
{
return base.ValidateAsync(manager, role);
}
}
}
37 changes: 37 additions & 0 deletions App/Services/Identity/Validators/AppUserValidator.cs
Original file line number Diff line number Diff line change
@@ -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<User>
{
public AppUserValidator(IdentityErrorDescriber errors) : base(errors)
{
}

public override async Task<IdentityResult> ValidateAsync(UserManager<User> manager, User user)
{
var result = await base.ValidateAsync(manager, user);

this.ValidateUserName(user,result.Errors.ToList());

return result;
}


private void ValidateUserName(User user, List<IdentityError> errors)
{
if (user.UserName.Contains("Admin"))
{
errors.Add(new IdentityError
{
Code = "InvalidUser",
Description = "این کاربر معتبر نیست"
});
}
}
}
}
2 changes: 2 additions & 0 deletions App/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public void ConfigureServices(IServiceCollection services)
})
.AddUserStore<AppUserStore>()
.AddRoleStore<AppRoleStore>()
.AddUserValidator<AppUserValidator>()
.AddRoleValidator<AppRoleValidator>()
.AddDefaultTokenProviders();

}
Expand Down

0 comments on commit 23362d2

Please sign in to comment.