@@ -6,60 +6,63 @@ namespace SimpleWebChatApplication.Controllers;
66public class LoginController : ControllerBase {
77 private readonly ILogger < LoginController > _logger ;
88 private readonly IDataProvider _provider ;
9- private readonly ICheckingTools _tools ;
10- private readonly ISession Session ;
9+ private readonly IGeneralTools _tools ;
10+ private readonly IHttpConnectionInfo _info ;
1111
12- public LoginController ( ILogger < LoginController > logger , IDataProvider provider , ICheckingTools tools ) {
12+ public LoginController ( ILogger < LoginController > logger , IDataProvider provider , IGeneralTools tools , IHttpConnectionInfo info ) {
1313 _logger = logger ;
1414 _provider = provider ;
1515 _tools = tools ;
16- Session = HttpContext . Session ;
16+ _info = info ;
1717 }
1818
1919
2020 [ HttpGet ]
2121 [ ResponseCache ( CacheProfileName = "NoStore" ) ]
2222 public Models . Login Get ( ) => _tools . IsLogin ( out var displayName )
23- ? new ( ) { Success = true , Code = 0 , Message = "已登录。" , DisplayName = displayName }
24- : new ( ) { Success = false , Code = 3 , Message = "未登录。" } ;
23+ ? new ( ) { Success = true , Code = 0 , DisplayName = displayName }
24+ : new ( ) { Success = false , Code = 3 } ;
2525
2626
2727 [ HttpPost ]
2828 [ ResponseCache ( CacheProfileName = "NoStore" ) ]
29- public Models . Login Post ( string ? account , string ? password ) {
29+ public Models . Login Post ( [ FromForm ] string ? account , [ FromForm ] string ? password ) {
3030 if ( _tools . IsLogin ( ) ) {
3131 return new ( ) { Success = true , Code = 1 , Message = "您已经登录过了。" } ;
3232 }
33- if ( account is null || password is null ) {
33+ if ( string . IsNullOrWhiteSpace ( account ) || string . IsNullOrWhiteSpace ( password ) ) {
3434 return new ( ) { Success = false , Code = 5 , Message = "用户名或密码为空。" } ;
3535 }
3636 _ = Hubs . Cache . MemoryCache . TryGetValue ( $ "TryLoginCount of { account } ", out int count ) ;
3737 if ( count > 5 ) {
38- _logger . LogWarning ( "用户 {} 尝试登录次数过多,最后一次 IP 地址为 {}。" , account , HttpContext . Connection . RemoteIpAddress ) ;
38+ _logger . LogWarning ( "Post: 用户 {} 尝试登录次数过多,最后一次 IP 地址为 {}。" , account , _info . RemoteAddress ) ;
3939 return new ( ) { Success = false , Code = 7 , Message = "尝试登录次数过多,请在30分钟后重试。" } ;
4040 }
41- if ( account . Length is < 4 or > 32 || ! ICheckingTools . IsPasswordComplicated ( password ) ) {
42- _ = Hubs . Cache . Set ( $ "TryLoginCount of { account } ", count ++ , TimeSpan . FromMinutes ( 30 ) , TimeSpan . FromHours ( 2 ) ) ;
41+ if ( account . Length is < 4 or > 32 || ! IGeneralTools . IsPasswordComplicated ( password ) ) {
42+ _ = Hubs . Cache . Set ( $ "TryLoginCount of { account } ", ++ count , TimeSpan . FromMinutes ( 30 ) , TimeSpan . FromHours ( 2 ) ) ;
4343 return new ( ) { Success = false , Code = 6 , Message = "用户名或密码错误。" } ;
4444 }
45- using var reader = _provider . GetUserReader ( account ) ;
45+ using var reader = _provider . GetUserReader ( account , out var cmd ) ;
4646 if ( ! reader . Read ( ) ) {
47- _ = Hubs . Cache . Set ( $ "TryLoginCount of { account } ", count ++ , TimeSpan . FromMinutes ( 30 ) , TimeSpan . FromHours ( 2 ) ) ;
47+ _ = Hubs . Cache . Set ( $ "TryLoginCount of { account } ", ++ count , TimeSpan . FromMinutes ( 30 ) , TimeSpan . FromHours ( 2 ) ) ;
4848 return new ( ) { Success = false , Code = 6 , Message = "用户名或密码错误。" } ;
4949 }
5050 var hash = new byte [ 64 ] ;
5151 _ = reader . GetBytes ( 3 , 0 , hash , 0 , 64 ) ;
5252 var salt = new byte [ 16 ] ;
5353 _ = reader . GetBytes ( 4 , 0 , salt , 0 , 16 ) ;
54- if ( ! ICheckingTools . VerifyPassword ( password , hash , salt ) ) {
55- _ = Hubs . Cache . Set ( $ "TryLoginCount of { account } ", count ++ , TimeSpan . FromMinutes ( 30 ) , TimeSpan . FromHours ( 2 ) ) ;
54+ if ( ! IGeneralTools . VerifyPassword ( password , hash , salt ) ) {
55+ _ = Hubs . Cache . Set ( $ "TryLoginCount of { account } ", ++ count , TimeSpan . FromMinutes ( 30 ) , TimeSpan . FromHours ( 2 ) ) ;
5656 return new ( ) { Success = false , Code = 6 , Message = "用户名或密码错误。" } ;
5757 }
5858 Hubs . Cache . MemoryCache . Remove ( $ "TryLoginCount of { account } ") ;
59- Session . SetString ( "Name" , account ) ;
60- Session . SetString ( "Nick" , reader . GetString ( 2 ) ) ;
61- Session . Set ( "Hash" , hash ) ;
62- Session . Set ( "Salt" , salt ) ;
63- return new ( ) { Success = true , Code = 0 , Message = "登录成功!" } ;
59+ HttpContext . Session . SetString ( "Name" , account ) ;
60+ HttpContext . Session . SetString ( "Nick" , reader . GetString ( 2 ) ) ;
61+ HttpContext . Session . Set ( "Hash" , hash ) ;
62+ HttpContext . Session . Set ( "Salt" , salt ) ;
63+ cmd . Dispose ( ) ;
64+ _logger . LogDebug ( "Post: 用户 {} 于 {} 登录成功。" , account , _info . RemoteAddress ) ;
65+ return new ( ) { Success = true , Code = 0 } ;
66+
6467 }
6568}
0 commit comments