Esempio n. 1
0
        public static Account CreateAccount(string username, string nickname, int playerId, byte[] salt, byte[] hash)
        {
            using (DatabaseContainer dc = new DatabaseContainer())
            {
                if (dc.Accounts.FirstOrDefault(a => a.Username == username) != null)
                {
                    throw new ArgumentException($"Username: {username} is already taken");
                }

                Player player = dc.Players.FirstOrDefault(p => p.Id == playerId) ?? CreatePlayer(nickname);

                Account account = new Account()
                {
                    Username = username,
                    Player = player,
                    Salt = Encoding.ASCII.GetString(salt),
                    Hash = Encoding.ASCII.GetString(hash)
                };

                player.Account = account;

                dc.Accounts.Add(account);
                dc.SaveChanges();
                return account;
            }
        }
Esempio n. 2
0
        public static Account CreateAccount(string username, string nickname, int playerId, byte[] salt, byte[] hash)
        {
            using (DatabaseContainer dc = new DatabaseContainer())
            {
                if (dc.Accounts.FirstOrDefault(a => a.Username == username) != null)
                {
                    throw new ArgumentException($"Username: {username} is already taken");
                }

                Player player = dc.Players.FirstOrDefault(p => p.Id == playerId) ?? CreatePlayer(nickname);
                Account account = new Account()
                {
                    Username = username,
                    Player = player,
                    Salt = Convert.ToBase64String(salt),
                    Hash = Convert.ToBase64String(hash)
                };

                player.Account = account;
                dc.Accounts.Add(account);
                dc.Entry(player).State = EntityState.Modified; // Avoid making duplicate players.

                dc.SaveChanges();
                return account;
            }
        }