Ejemplo n.º 1
0
        public Session UserValid(string username, string password)
        {
            MongoServer server = MongoServer.Create(DbSettings.ConnectionString);
            MongoDatabase db = server.GetDatabase(DbConstants.DbNameUsers, Credentials, new SafeMode(true));
            MongoCollection<BsonDocument> col = db.GetCollection(DbConstants.CollectionNameAccount);

            var query = Query.And(
                Query.EQ(DbConstants.SessionFieldUsername, username),
                Query.EQ(DbConstants.SessionFieldPassword, GetMd5Hash(password)));

            var bsonDoc = col.FindOne(query);

            if (bsonDoc == null)
            {
                return null;
            }

            Session session = new Session();
            session.Username = bsonDoc[DbConstants.SessionFieldUsername].AsString;
            string userId = bsonDoc[DbConstants.PropertiesFieldUserId].AsString;
            session.UserId = ObjectId.Parse(userId);

            // Lookup our avatar's name from the Properties
            MongoCollection<BsonDocument> colProp = db.GetCollection(DbConstants.CollectionNameProperties);
            var queryProp = Query.EQ(DbConstants.PropertiesFieldUserId, userId);

            session.Avatarname = session.Username;

            var resultAvatar = colProp.FindOne(queryProp);
            if (resultAvatar != null)
            {
                if (resultAvatar.Contains(DbConstants.PropertiesFieldAvatarname))
                {
                    session.Avatarname = resultAvatar[DbConstants.PropertiesFieldAvatarname].AsString;
                }
            }

            return session;
        }
Ejemplo n.º 2
0
        public Session GetUserBySession(string token)
        {
            MongoServer server = MongoServer.Create(DbSettings.ConnectionString);
            MongoDatabase db = server.GetDatabase(DbConstants.DbNameUsers, Credentials, new SafeMode(true));

            MongoCollection<BsonDocument> mongColl = db.GetCollection(DbConstants.CollectionNameSession);

            QueryDocument query = new QueryDocument(DbConstants.SessionFieldSessionId, token);
            BsonDocument bson = mongColl.FindOne(query);

            if (bson == null)
            {
                return null;
            }

            Session session = new Session();
            session.Id = bson[DbConstants.SessionFieldSessionId].AsString;
            string userId = bson[DbConstants.SessionFieldUserId].AsString;
            session.UserId = ObjectId.Parse(userId);

            session.Username = bson[DbConstants.SessionFieldUsername].AsString;

            // Look up for avatar's name
            MongoCollection<BsonDocument> colProp = db.GetCollection(DbConstants.CollectionNameProperties);
            var queryProp = Query.EQ(DbConstants.PropertiesFieldUserId, userId);

            session.Avatarname = session.Username;

            var resultAvatar = colProp.FindOne(queryProp);
            if (resultAvatar != null)
            {
                if (resultAvatar.Contains(DbConstants.PropertiesFieldAvatarname))
                {
                    session.Avatarname = resultAvatar[DbConstants.PropertiesFieldAvatarname].AsString;
                }
            }

            server.Disconnect();

            return session;
        }