private async Task <Account> AuthenticateMember(Operator @operator, CasSecurityCheckResponse casResponse, IWritableDatabase db) { var userName = casResponse.User.ToLowerInvariant(); var account = await db.Accounts .Include(acc => acc.Role) .Include(acc => acc.Operator) .FirstOrDefaultAsync(acc => acc.Username == userName && acc.OperatorId == @operator.Id); if (account != null) { account.LastLoginUtc = DateTime.UtcNow; } else { var defaultRole = await db.Roles.FirstOrDefaultAsync(role => role.Id == (int)Roles.Minimum); var taipeiUtc = await db.UtcTimeOffsets.FirstOrDefaultAsync(utc => utc.Offset == "+08:00"); account = new Account { Username = userName, RealName = userName, RoleId = defaultRole.Id, OperatorId = @operator.Id, UtcTimeOffsetId = taipeiUtc.Id, Active = true, LastLoginUtc = DateTime.UtcNow }; } db.InsertOrUpdate(account, account.Id); await db.SaveChangesAsync(); return(account); }
private async Task SaveUserSession(IWritableDatabase db, User user, string sessionKey, string ipAddress, string extraInfo) { db.UserSessionLogs.Add(new UserSessionLog { UserId = user.Id, SessionKey = sessionKey, IpAddress = ipAddress, PlatformType = PlatformType.Web }); // try to adapt with old structure when the game lobby do authentication var entitySession = new UserSessionEntity { UserId = user.Id, SessionKey = sessionKey, ExtraInfo = extraInfo }; db.InsertOrUpdate(entitySession, user.Id); await db.SaveChangesAsync(); var userSession = new UserSession { IsFunPlay = false, SessionKey = sessionKey, UserId = user.Id, User = user, ExtraInfo = extraInfo }; await cache.SetAsync(userSession.SessionKey, userSession, new DistributedCacheEntryOptions { SlidingExpiration = TimeSpan.FromMinutes(20) }); // The reason why we cache user id here, because RSlotServer project they store user session // on database, and try to get user id by session key from database. // for the workaround, we cache user id on the cache, so RSlotServer can get it from cache also. await cache.SetAsync(userSession.SessionKey + "_UserId", user.Id, new DistributedCacheEntryOptions { SlidingExpiration = TimeSpan.FromMinutes(20) }); }
private async Task <User> CreateUser(IWritableDatabase db, string currencyCode, string externalId, string memberName, int operatorId, bool isTestAccount) { if (IsInvalidMemberName(memberName)) { logger.LogWarning("cust_name: '{0}' is not valid characters", memberName); return(null); } if (!cachedSettings.CurrenciesByIsoCode.TryGetValue(currencyCode, out Currency currency)) { cachedSettings.CurrenciesByIsoCode.TryGetValue("UNK", out currency); } if (currency == null || currency.Id == 0) { logger.LogError("{0} is not a valid currency code!", currencyCode); return(null); } if (!currency.IsVisible) { currency.IsVisible = true; db.Currencies.Update(currency); await db.SaveChangesAsync(); } var existingUsername = await db.Users .Where(u => u.OperatorId == operatorId) .Where(u => u.Name == memberName) .AsNoTracking() .FirstOrDefaultAsync(); if (existingUsername != null) { logger.LogInformation("[USER EXIST] cust_id: {0}, cust_name: {1}, optag: {2}", existingUsername.ExternalId, existingUsername.Name, existingUsername.Operator.Tag); return(null); } try { var newUser = new User { Name = memberName, CurrencyId = currency.Id, ExternalId = externalId, OperatorId = operatorId, IsDemo = isTestAccount }; db.Users.Add(newUser); await db.SaveChangesAsync(); } catch (DuplicateException ex) { logger.LogWarning(ex, "Duplicated exception during create new user {operatorId}-{externalId}-{memberName}", operatorId, externalId, memberName); } return(await db.Users.Where(x => x.ExternalId == externalId) .Where(x => x.OperatorId == operatorId) .Include(x => x.Operator) .Include(x => x.Currency) .AsNoTracking() .FirstOrDefaultAsync()); }