diff options
author | 杨宇千 <crupest@outlook.com> | 2019-08-12 16:24:17 +0800 |
---|---|---|
committer | 杨宇千 <crupest@outlook.com> | 2019-08-12 16:24:17 +0800 |
commit | afaad69437dc12ffb706667eb63875b116631234 (patch) | |
tree | af074659bc9490457f1627c520c1774895a3975f /Timeline/Services | |
parent | 4367f283f196d8b6f195855b4592bf65aa10859e (diff) | |
download | timeline-afaad69437dc12ffb706667eb63875b116631234.tar.gz timeline-afaad69437dc12ffb706667eb63875b116631234.tar.bz2 timeline-afaad69437dc12ffb706667eb63875b116631234.zip |
Add username format check.
Diffstat (limited to 'Timeline/Services')
-rw-r--r-- | Timeline/Services/UserService.cs | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/Timeline/Services/UserService.cs b/Timeline/Services/UserService.cs index 50aa4187..0993d3dc 100644 --- a/Timeline/Services/UserService.cs +++ b/Timeline/Services/UserService.cs @@ -278,6 +278,8 @@ namespace Timeline.Services private readonly IJwtService _jwtService;
private readonly IPasswordService _passwordService;
+ private readonly UsernameValidator _usernameValidator;
+
public UserService(ILogger<UserService> logger, IMemoryCache memoryCache, DatabaseContext databaseContext, IJwtService jwtService, IPasswordService passwordService)
{
_logger = logger;
@@ -285,6 +287,8 @@ namespace Timeline.Services _databaseContext = databaseContext;
_jwtService = jwtService;
_passwordService = passwordService;
+
+ _usernameValidator = new UsernameValidator();
}
private string GenerateCacheKeyByUserId(long id) => $"user:{id}";
@@ -377,6 +381,11 @@ namespace Timeline.Services if (password == null)
throw new ArgumentNullException(nameof(password));
+ if (!_usernameValidator.Validate(username, out var message))
+ {
+ throw new UsernameBadFormatException(username, message);
+ }
+
var user = await _databaseContext.Users.Where(u => u.Name == username).SingleOrDefaultAsync();
if (user == null)
|