示例#1
0
        public async Task Test_MongoDbMapperWithCustomKey_CreateAndLoadPoco()
        {
            var person = new TrackableTestPocoWithCustomId
            {
                CustomId = UniqueInt64Id.GenerateNewId(),
                Name     = "Testor",
                Age      = 25
            };
            await _mapper.CreateAsync(_collection, person);

            var person2 = await _mapper.LoadAsync(_collection, person.CustomId);

            Assert.Equal(person.CustomId, person2.CustomId);
            Assert.Equal(person.Name, person2.Name);
            Assert.Equal(person.Age, person2.Age);
        }
示例#2
0
        // If account exists, check password correct.
        // Otherwise create new account with id and password.
        public static async Task <AccountInfo> AuthenticateAsync(string id, string password)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                return(null);
            }

            var accountCollection = MongoDbStorage.Instance.Database.GetCollection <AccountInfo>("Account");

            await EnsureIndex(accountCollection);

            var account = await accountCollection.Find(a => a.Id == id).FirstOrDefaultAsync();

            if (account != null)
            {
                if (PasswordUtility.Verify(password, account.PassSalt, account.PassHash) == false)
                {
                    return(null);
                }

                account.LastLoginTime = DateTime.UtcNow;
                await accountCollection.ReplaceOneAsync(a => a.Id == id, account);
            }
            else
            {
                var saltHash = PasswordUtility.CreateSaltHash(password);
                account = new AccountInfo
                {
                    Id            = id,
                    PassSalt      = saltHash.Item1,
                    PassHash      = saltHash.Item2,
                    UserId        = UniqueInt64Id.GenerateNewId(),
                    RegisterTime  = DateTime.UtcNow,
                    LastLoginTime = DateTime.UtcNow
                };
                await accountCollection.InsertOneAsync(account);
            }

            return(account);
        }