public UserContext Login(string userName, string password) { using (SecurityDbContext securityDbContext = new SecurityDbContext()) { UserInfo user = this.GetUser(userName, new int?()); if (user == null) { return((UserContext)null); } if (!OperationAccess.PasswordEqual(user.Password, password)) { if (user.LoginTryTime >= (int)ConfigurationController.LoginTryNo) { user.IsActive = false; } ++user.LoginTryTime; //user.ObjectState = ObjectState.Modified; //securityDbContext.SyncObjectState<UserInfo>(user); securityDbContext.SaveChanges(); return((UserContext)null); } if (!user.IsActive) { SecurityManager.SaveActivityLogForLoginFailedAction(userName, "کاربر غیر فعال"); throw new LoginException(ConfigurationController.ApplicationLanguage == AppLanguage.English ? "User is not Active." : "کاربر غیر فعال می باشد."); } user.LastLoginDate = new DateTime?(DateTime.Now); user.LoginTryTime = 0; //user.ObjectState = ObjectState.Modified; //securityDbContext.SyncObjectState<UserInfo>(user); securityDbContext.SaveChanges(); return(this.InitiateUserContext(user)); } }
public async Task <UserContext> LoginAsync(string userName, string password) { using (SecurityDbContext context = new SecurityDbContext()) { UserInfo uInfo = await this.GetUserAsync(userName, new int?()); if (uInfo == null) { return((UserContext)null); } if (!OperationAccess.PasswordEqual(uInfo.Password, password)) { if (uInfo.LoginTryTime >= (int)ConfigurationController.LoginTryNo) { uInfo.IsActive = false; } ++uInfo.LoginTryTime; //uInfo.ObjectState = ObjectState.Modified; //context.SyncObjectState<UserInfo>(uInfo); int num = await context.SaveChangesAsync(); return((UserContext)null); } if (!uInfo.IsActive) { await SecurityManager.SaveActivityLogForLoginFailedActionAsync(userName, "کاربر غیر فعال"); throw new LoginException("کاربر غیر فعال می باشد."); } uInfo.LastLoginDate = new DateTime?(DateTime.Now); uInfo.LoginTryTime = 0; //uInfo.ObjectState = ObjectState.Modified; //context.SyncObjectState<UserInfo>(uInfo); int num1 = await context.SaveChangesAsync(); UserContext userContext = this.InitiateUserContext(uInfo); return(userContext); } }