private IAcSession GetAcSessionByLoginName(IAcDomain acDomain, string loginName) { if (EmptyAcDomain.SingleInstance.Equals(acDomain)) { return(AcSessionState.Empty); } var storage = acDomain.GetRequiredService <IAcSessionStorage>(); var acSession = storage.GetData(acDomain.Config.CurrentAcSessionCacheKey) as IAcSession; if (acSession != null) { return(acSession); } var account = AcSessionState.AcMethod.GetAccountByLoginName(acDomain, loginName); if (account == null) { return(AcSessionState.Empty); } var sessionEntity = AcSessionState.AcMethod.GetAcSessionEntity(acDomain, account.Id); if (sessionEntity != null) { if (!sessionEntity.IsAuthenticated) { return(AcSessionState.Empty); } acSession = new AcSessionState(acDomain, sessionEntity); } else { // 使用账户标识作为会话标识会导致一个账户只有一个会话 // TODO:支持账户和会话的一对多,为会话级的动态责任分离做准备 var accountState = AccountState.Create(account); var identity = new AnycmdIdentity(account.LoginName); var acSessionEntity = new AcSession { Id = account.Id, AccountId = account.Id, AuthenticationType = identity.AuthenticationType, Description = null, IsAuthenticated = identity.IsAuthenticated, IsEnabled = 1, LoginName = account.LoginName }; AcSessionState.AcMethod.AddAcSession(acDomain, acSessionEntity); acSession = new AcSessionState(acDomain, account.Id, accountState); } storage.SetData(acDomain.Config.CurrentAcSessionCacheKey, acSession); return(acSession); }
private void DoSignIn(IAcDomain acDomain, Dictionary <string, object> args) { if (EmptyAcDomain.SingleInstance.Equals(acDomain)) { return; } var loginName = args.ContainsKey("loginName") ? (args["loginName"] ?? string.Empty).ToString() : string.Empty; var password = args.ContainsKey("password") ? (args["password"] ?? string.Empty).ToString() : string.Empty; var rememberMe = args.ContainsKey("rememberMe") ? (args["rememberMe"] ?? string.Empty).ToString() : string.Empty; var passwordEncryptionService = acDomain.GetRequiredService <IPasswordEncryptionService>(); if (string.IsNullOrEmpty(loginName) || string.IsNullOrEmpty(password)) { throw new ValidationException("用户名和密码不能为空"); } var addVisitingLogCommand = new AddVisitingLogCommand(AcSessionState.Empty) { IpAddress = IpHelper.GetClientIp(), LoginName = loginName, VisitedOn = null, VisitOn = DateTime.Now, Description = "登录成功", ReasonPhrase = VisitState.LogOnFail.ToName(), StateCode = (int)VisitState.LogOnFail }; password = passwordEncryptionService.Encrypt(password); var account = AcSessionState.AcMethod.GetAccountByLoginName(acDomain, loginName); if (account == null) { addVisitingLogCommand.Description = "用户名错误"; acDomain.MessageDispatcher.DispatchMessage(addVisitingLogCommand); throw new ValidationException(addVisitingLogCommand.Description); } else { addVisitingLogCommand.AccountId = account.Id; } if (password != account.Password) { addVisitingLogCommand.Description = "密码错误"; acDomain.MessageDispatcher.DispatchMessage(addVisitingLogCommand); throw new ValidationException(addVisitingLogCommand.Description); } if (account.IsEnabled == 0) { addVisitingLogCommand.Description = "对不起,该账户已被禁用"; acDomain.MessageDispatcher.DispatchMessage(addVisitingLogCommand); throw new ValidationException(addVisitingLogCommand.Description); } string auditState = account.AuditState == null ? account.AuditState : account.AuditState.ToLower(); CatalogState dicItem; if (!acDomain.CatalogSet.TryGetCatalog(auditState, out dicItem)) { throw new AnycmdException("意外的字典编码" + auditState); } if (auditState == null || auditState == "notaudit") { addVisitingLogCommand.Description = "对不起,该账户尚未审核"; acDomain.MessageDispatcher.DispatchMessage(addVisitingLogCommand); throw new ValidationException(addVisitingLogCommand.Description); } if (auditState == "auditnotpass") { addVisitingLogCommand.Description = "对不起,该账户未通过审核"; acDomain.MessageDispatcher.DispatchMessage(addVisitingLogCommand); throw new ValidationException(addVisitingLogCommand.Description); } if (account.AllowStartTime.HasValue && SystemTime.Now() < account.AllowStartTime.Value) { addVisitingLogCommand.Description = "对不起,该账户的允许登录开始时间还没到。请在" + account.AllowStartTime + "后登录"; acDomain.MessageDispatcher.DispatchMessage(addVisitingLogCommand); throw new ValidationException(addVisitingLogCommand.Description); } if (account.AllowEndTime.HasValue && SystemTime.Now() > account.AllowEndTime.Value) { addVisitingLogCommand.Description = "对不起,该账户的允许登录时间已经过期"; acDomain.MessageDispatcher.DispatchMessage(addVisitingLogCommand); throw new ValidationException(addVisitingLogCommand.Description); } if (account.LockEndTime.HasValue || account.LockStartTime.HasValue) { DateTime lockStartTime = account.LockStartTime ?? DateTime.MinValue; DateTime lockEndTime = account.LockEndTime ?? DateTime.MaxValue; if (SystemTime.Now() > lockStartTime && SystemTime.Now() < lockEndTime) { addVisitingLogCommand.Description = "对不起,该账户暂被锁定"; acDomain.MessageDispatcher.DispatchMessage(addVisitingLogCommand); throw new ValidationException(addVisitingLogCommand.Description); } } if (account.PreviousLoginOn.HasValue && account.PreviousLoginOn.Value >= SystemTime.Now().AddMinutes(5)) { addVisitingLogCommand.Description = "检测到您的上次登录时间在未来。这可能是因为本站点服务器的时间落后导致的,请联系管理员。"; acDomain.MessageDispatcher.DispatchMessage(addVisitingLogCommand); throw new ValidationException(addVisitingLogCommand.Description); } account.PreviousLoginOn = SystemTime.Now(); if (!account.FirstLoginOn.HasValue) { account.FirstLoginOn = SystemTime.Now(); } account.LoginCount = (account.LoginCount ?? 0) + 1; account.IpAddress = IpHelper.GetClientIp(); // 使用账户标识作为会话标识会导致一个账户只有一个会话 // TODO:支持账户和会话的一对多,为会话级的动态责任分离做准备 var sessionEntity = AcSessionState.AcMethod.GetAcSessionEntity(acDomain, account.Id); IAcSession acSession; if (sessionEntity != null) { acSession = new AcSessionState(acDomain, sessionEntity.Id, AccountState.Create(account)); sessionEntity.IsAuthenticated = true; AcSessionState.AcMethod.UpdateAcSession(acDomain, sessionEntity); } else { var accountState = AccountState.Create(account); var identity = new AnycmdIdentity(account.LoginName); var acSessionEntity = new AcSession { Id = account.Id, AccountId = account.Id, AuthenticationType = identity.AuthenticationType, Description = null, IsAuthenticated = identity.IsAuthenticated, IsEnabled = 1, LoginName = account.LoginName }; AcSessionState.AcMethod.AddAcSession(acDomain, acSessionEntity); acSession = new AcSessionState(acDomain, account.Id, accountState); } if (HttpContext.Current != null) { HttpContext.Current.User = acSession; bool createPersistentCookie = rememberMe.Equals("rememberMe", StringComparison.OrdinalIgnoreCase); FormsAuthentication.SetAuthCookie(account.LoginName, createPersistentCookie); } else { Thread.CurrentPrincipal = acSession; } Guid?visitingLogId = Guid.NewGuid(); acSession.SetData("UserContext_Current_VisitingLogId", visitingLogId); acSession.SetData(acDomain.Config.CurrentAcSessionCacheKey, acSession); acDomain.EventBus.Publish(new AccountLoginedEvent(acSession, account)); acDomain.EventBus.Commit(); addVisitingLogCommand.StateCode = (int)VisitState.Logged; addVisitingLogCommand.ReasonPhrase = VisitState.Logged.ToName(); addVisitingLogCommand.Description = "登录成功"; acDomain.MessageDispatcher.DispatchMessage(addVisitingLogCommand); }
private IAcSession GetAcSessionByLoginName(IAcDomain acDomain, string loginName) { if (EmptyAcDomain.SingleInstance.Equals(acDomain)) { return AcSessionState.Empty; } var storage = acDomain.GetRequiredService<IAcSessionStorage>(); var acSession = storage.GetData(acDomain.Config.CurrentAcSessionCacheKey) as IAcSession; if (acSession != null) return acSession; var account = AcSessionState.AcMethod.GetAccountByLoginName(acDomain, loginName); if (account == null) { return AcSessionState.Empty; } var sessionEntity = AcSessionState.AcMethod.GetAcSessionEntity(acDomain, account.Id); if (sessionEntity != null) { if (!sessionEntity.IsAuthenticated) { return AcSessionState.Empty; } acSession = new AcSessionState(acDomain, sessionEntity); } else { // 使用账户标识作为会话标识会导致一个账户只有一个会话 // TODO:支持账户和会话的一对多,为会话级的动态责任分离做准备 var accountState = AccountState.Create(account); var identity = new AnycmdIdentity(account.LoginName); var acSessionEntity = new AcSession { Id = account.Id, AccountId = account.Id, AuthenticationType = identity.AuthenticationType, Description = null, IsAuthenticated = identity.IsAuthenticated, IsEnabled = 1, LoginName = account.LoginName }; AcSessionState.AcMethod.AddAcSession(acDomain, acSessionEntity); acSession = new AcSessionState(acDomain, account.Id, accountState); } storage.SetData(acDomain.Config.CurrentAcSessionCacheKey, acSession); return acSession; }
private void DoSignIn(IAcDomain acDomain, Dictionary<string, object> args) { if (EmptyAcDomain.SingleInstance.Equals(acDomain)) { return; } var loginName = args.ContainsKey("loginName") ? (args["loginName"] ?? string.Empty).ToString() : string.Empty; var password = args.ContainsKey("password") ? (args["password"] ?? string.Empty).ToString() : string.Empty; var rememberMe = args.ContainsKey("rememberMe") ? (args["rememberMe"] ?? string.Empty).ToString() : string.Empty; var passwordEncryptionService = acDomain.GetRequiredService<IPasswordEncryptionService>(); if (string.IsNullOrEmpty(loginName) || string.IsNullOrEmpty(password)) { throw new ValidationException("用户名和密码不能为空"); } var addVisitingLogCommand = new AddVisitingLogCommand(AcSessionState.Empty) { IpAddress = IpHelper.GetClientIp(), LoginName = loginName, VisitedOn = null, VisitOn = DateTime.Now, Description = "登录成功", ReasonPhrase = VisitState.LogOnFail.ToName(), StateCode = (int)VisitState.LogOnFail }; password = passwordEncryptionService.Encrypt(password); var account = AcSessionState.AcMethod.GetAccountByLoginName(acDomain, loginName); if (account == null) { addVisitingLogCommand.Description = "用户名错误"; acDomain.MessageDispatcher.DispatchMessage(addVisitingLogCommand); throw new ValidationException(addVisitingLogCommand.Description); } else { addVisitingLogCommand.AccountId = account.Id; } if (password != account.Password) { addVisitingLogCommand.Description = "密码错误"; acDomain.MessageDispatcher.DispatchMessage(addVisitingLogCommand); throw new ValidationException(addVisitingLogCommand.Description); } if (account.IsEnabled == 0) { addVisitingLogCommand.Description = "对不起,该账户已被禁用"; acDomain.MessageDispatcher.DispatchMessage(addVisitingLogCommand); throw new ValidationException(addVisitingLogCommand.Description); } string auditState = account.AuditState == null ? account.AuditState : account.AuditState.ToLower(); CatalogState dicItem; if (!acDomain.CatalogSet.TryGetCatalog(auditState, out dicItem)) { throw new AnycmdException("意外的字典编码" + auditState); } if (auditState == null || auditState == "notaudit") { addVisitingLogCommand.Description = "对不起,该账户尚未审核"; acDomain.MessageDispatcher.DispatchMessage(addVisitingLogCommand); throw new ValidationException(addVisitingLogCommand.Description); } if (auditState == "auditnotpass") { addVisitingLogCommand.Description = "对不起,该账户未通过审核"; acDomain.MessageDispatcher.DispatchMessage(addVisitingLogCommand); throw new ValidationException(addVisitingLogCommand.Description); } if (account.AllowStartTime.HasValue && SystemTime.Now() < account.AllowStartTime.Value) { addVisitingLogCommand.Description = "对不起,该账户的允许登录开始时间还没到。请在" + account.AllowStartTime + "后登录"; acDomain.MessageDispatcher.DispatchMessage(addVisitingLogCommand); throw new ValidationException(addVisitingLogCommand.Description); } if (account.AllowEndTime.HasValue && SystemTime.Now() > account.AllowEndTime.Value) { addVisitingLogCommand.Description = "对不起,该账户的允许登录时间已经过期"; acDomain.MessageDispatcher.DispatchMessage(addVisitingLogCommand); throw new ValidationException(addVisitingLogCommand.Description); } if (account.LockEndTime.HasValue || account.LockStartTime.HasValue) { DateTime lockStartTime = account.LockStartTime ?? DateTime.MinValue; DateTime lockEndTime = account.LockEndTime ?? DateTime.MaxValue; if (SystemTime.Now() > lockStartTime && SystemTime.Now() < lockEndTime) { addVisitingLogCommand.Description = "对不起,该账户暂被锁定"; acDomain.MessageDispatcher.DispatchMessage(addVisitingLogCommand); throw new ValidationException(addVisitingLogCommand.Description); } } if (account.PreviousLoginOn.HasValue && account.PreviousLoginOn.Value >= SystemTime.Now().AddMinutes(5)) { addVisitingLogCommand.Description = "检测到您的上次登录时间在未来。这可能是因为本站点服务器的时间落后导致的,请联系管理员。"; acDomain.MessageDispatcher.DispatchMessage(addVisitingLogCommand); throw new ValidationException(addVisitingLogCommand.Description); } account.PreviousLoginOn = SystemTime.Now(); if (!account.FirstLoginOn.HasValue) { account.FirstLoginOn = SystemTime.Now(); } account.LoginCount = (account.LoginCount ?? 0) + 1; account.IpAddress = IpHelper.GetClientIp(); // 使用账户标识作为会话标识会导致一个账户只有一个会话 // TODO:支持账户和会话的一对多,为会话级的动态责任分离做准备 var sessionEntity = AcSessionState.AcMethod.GetAcSessionEntity(acDomain, account.Id); IAcSession acSession; if (sessionEntity != null) { acSession = new AcSessionState(acDomain, sessionEntity.Id, AccountState.Create(account)); sessionEntity.IsAuthenticated = true; AcSessionState.AcMethod.UpdateAcSession(acDomain, sessionEntity); } else { var accountState = AccountState.Create(account); var identity = new AnycmdIdentity(account.LoginName); var acSessionEntity = new AcSession { Id = account.Id, AccountId = account.Id, AuthenticationType = identity.AuthenticationType, Description = null, IsAuthenticated = identity.IsAuthenticated, IsEnabled = 1, LoginName = account.LoginName }; AcSessionState.AcMethod.AddAcSession(acDomain, acSessionEntity); acSession = new AcSessionState(acDomain, account.Id, accountState); } if (HttpContext.Current != null) { HttpContext.Current.User = acSession; bool createPersistentCookie = rememberMe.Equals("rememberMe", StringComparison.OrdinalIgnoreCase); FormsAuthentication.SetAuthCookie(account.LoginName, createPersistentCookie); } else { Thread.CurrentPrincipal = acSession; } Guid? visitingLogId = Guid.NewGuid(); acSession.SetData("UserContext_Current_VisitingLogId", visitingLogId); acSession.SetData(acDomain.Config.CurrentAcSessionCacheKey, acSession); acDomain.EventBus.Publish(new AccountLoginedEvent(acSession, account)); acDomain.EventBus.Commit(); addVisitingLogCommand.StateCode = (int)VisitState.Logged; addVisitingLogCommand.ReasonPhrase = VisitState.Logged.ToName(); addVisitingLogCommand.Description = "登录成功"; acDomain.MessageDispatcher.DispatchMessage(addVisitingLogCommand); }