コード例 #1
0
ファイル: Database.cs プロジェクト: Clapoty/DeReader
        public void InsertUser(string name, string password)
        {
            if(string.IsNullOrEmpty(name)) throw new ArgumentException(name);
            if (string.IsNullOrEmpty(password)) throw new ArgumentException(password);

            var mongoUser = new MongoUser(name, new PasswordEvidence(password), false);
            _database.AddUser(mongoUser);
        }
コード例 #2
0
 public void TestConstructor1()
 {
     var u = new MongoUser("u", new PasswordEvidence("p"), true);
     var ph = MongoUser.HashPassword("u", "p");
     Assert.AreEqual("u", u.Username);
     Assert.AreEqual(ph, u.PasswordHash);
     Assert.AreEqual(true, u.IsReadOnly);
 }
コード例 #3
0
 public void TestConstructor2()
 {
     var h = MongoUser.HashPassword("u", "p");
     var u = new MongoUser("u", h, true);
     Assert.AreEqual("u", u.Username);
     Assert.AreEqual(h, u.PasswordHash);
     Assert.AreEqual(true, u.IsReadOnly);
 }
コード例 #4
0
 /// <summary>
 /// Add User
 /// </summary>
 /// <param name="strUser">Username</param>
 /// <param name="password">Password</param>
 /// <param name="isReadOnly">Is ReadOnly</param>
 public static void AddUserToDB(String strUser, String password, Boolean isReadOnly)
 {
     MongoDatabase mongoDB = SystemManager.GetCurrentDataBase();
     MongoUser newUser = new MongoUser(strUser, password, false);
     if (mongoDB.FindUser(strUser) == null)
     {
         mongoDB.AddUser(newUser);
     }
 }
コード例 #5
0
 //这里有个漏洞,对于数据库来说,对于local的验证和对于admin的验证是相同的。
 //如果是加入用户到服务器中,是加入到local还是admin,需要考虑一下。
 /// <summary>
 /// Add A User to Admin database
 /// </summary>
 /// <param name="strUser">Username</param>
 /// <param name="password">Password</param>
 /// <param name="isReadOnly">Is ReadOnly</param>
 public static void AddUserToSvr(String strUser, String password, Boolean isReadOnly)
 {
     MongoServer mongoSvr = SystemManager.GetCurrentServer();
     //必须使用MongoCredentials来添加用户不然的话,Password将使用明文登入到数据库中!
     //这样的话,在使用MongoCredentials登入的时候,会发生密码错误引发的认证失败
     MongoUser newUser = new MongoUser(strUser, password, true);
     if (mongoSvr.GetDatabase(DATABASE_NAME_ADMIN).FindUser(strUser) == null)
     {
         mongoSvr.GetDatabase(DATABASE_NAME_ADMIN).AddUser(newUser);
     }
 }
コード例 #6
0
        public virtual void AddUser(MongoUser user)
        {
            var operation = new AddUserOperation(
                _namespace,
                user.Username,
                user.PasswordHash,
                user.IsReadOnly,
                GetMessageEncoderSettings());

            ExecuteWriteOperation(operation);
        }
コード例 #7
0
#pragma warning disable 618
        private void AddUser(IClientSessionHandle session, MongoUser user)
        {
            var operation = new AddUserOperation(
                _namespace,
                user.Username,
                user.PasswordHash,
                user.IsReadOnly,
                GetMessageEncoderSettings());

            ExecuteWriteOperation(session, operation);
        }
コード例 #8
0
        /// <summary>
        /// Adds a user to this database.
        /// </summary>
        /// <param name="user">The user.</param>
        public virtual void AddUser(MongoUser user)
        {
            var users    = GetCollection("system.users");
            var document = users.FindOne(Query.EQ("user", user.Username));

            if (document == null)
            {
                document = new BsonDocument("user", user.Username);
            }
            document["readOnly"] = user.IsReadOnly;
            document["pwd"]      = user.PasswordHash;
            users.Save(document);
        }
コード例 #9
0
 public virtual void AddUser(MongoUser user)
 {
     using (RequestStart(ReadPreference.Primary))
     {
         if (_server.RequestServerInstance.Supports(FeatureId.UserManagementCommands))
         {
             AddUserWithUserManagementCommands(user);
         }
         else
         {
             AddUserWithInsert(user);
         }
     }
 }
コード例 #10
0
        /// <summary>
        /// Finds all users of this database.
        /// </summary>
        /// <returns>An array of users.</returns>
        public virtual MongoUser[] FindAllUsers()
        {
            var result = new List <MongoUser>();
            var users  = GetCollection("system.users");

            foreach (var document in users.FindAll())
            {
                var username     = document["user"].AsString;
                var passwordHash = document["pwd"].AsString;
                var readOnly     = document["readOnly"].ToBoolean();
                var user         = new MongoUser(username, passwordHash, readOnly);
                result.Add(user);
            }
            ;
            return(result.ToArray());
        }
コード例 #11
0
        #pragma warning restore

        #pragma warning disable 618
        private MongoUser[] FindAllUsersWithQuery()
        {
            var results = new List <MongoUser>();
            var users   = GetCollection("system.users");

            foreach (var document in users.FindAll())
            {
                var username     = document["user"].AsString;
                var passwordHash = document.GetValue("pwd", "").AsString;
                var readOnly     = document["readOnly"].ToBoolean();
                var user         = new MongoUser(username, passwordHash, readOnly);
                results.Add(user);
            }
            ;
            return(results.ToArray());
        }
コード例 #12
0
        protected void EnsureAccessRights()
        {
            var url = new MongoUrl(this.ConnectionString);
            MongoClientSettings settings = MongoClientSettings.FromUrl(url);
            settings.DefaultCredentials = null;

            MongoCredentials adminCredentials = this.GetAdminCredentials();
            var client = new MongoClient(settings);
            MongoServer server = client.GetServer();
            MongoDatabase adminDatabase = server.GetDatabase(this.MongoAdministratorDatabase);

            MongoUser user = adminDatabase.FindUser(this.MongoAdministratorName);
            if (IsUserAdmin(user))
            {
                return;
            }

            user = new MongoUser(adminCredentials, false);
            adminDatabase.AddUser(user);
            server.Disconnect();
        }
コード例 #13
0
        public void TestEquals()
        {
            var a1 = new MongoUser("u", "h", false);
            var a2 = new MongoUser("u", "h", false);
            var a3 = a2;
            var b = new MongoUser("x", "h", false);
            var c = new MongoUser("u", "x", false);
            var d = new MongoUser("u", "h", true);
            var null1 = (MongoUser)null;
            var null2 = (MongoUser)null;

            Assert.AreNotSame(a1, a2);
            Assert.AreSame(a2, a3);
            Assert.IsTrue(a1.Equals((object)a2));
            Assert.IsFalse(a1.Equals((object)null));
            Assert.IsFalse(a1.Equals((object)"x"));

            Assert.IsTrue(a1 == a2);
            Assert.IsTrue(a2 == a3);
            Assert.IsFalse(a1 == b);
            Assert.IsFalse(a1 == c);
            Assert.IsFalse(a1 == d);
            Assert.IsFalse(a1 == null1);
            Assert.IsFalse(null1 == a1);
            Assert.IsTrue(null1 == null2);

            Assert.IsFalse(a1 != a2);
            Assert.IsFalse(a2 != a3);
            Assert.IsTrue(a1 != b);
            Assert.IsTrue(a1 != c);
            Assert.IsTrue(a1 != d);
            Assert.IsTrue(a1 != null1);
            Assert.IsTrue(null1 != a1);
            Assert.IsFalse(null1 != null2);

            Assert.AreEqual(a1.GetHashCode(), a2.GetHashCode());
        }
コード例 #14
0
 public virtual void RemoveUser(MongoUser user)
 {
     RemoveUser(user.Username);
 }
コード例 #15
0
 public virtual void AddUser(MongoUser user)
 {
     UsingImplicitSession(session => AddUser(session, user));
 }
コード例 #16
0
 private static bool IsUserAdmin(MongoUser user)
 {
     return user != null && !user.IsReadOnly;
 }
コード例 #17
0
        /// <summary>
        /// Adds a user to this database.
        /// </summary>
        /// <param name="credentials">The user's credentials.</param>
        /// <param name="readOnly">True if the user is a read-only user.</param>
        public virtual void AddUser(MongoCredentials credentials, bool readOnly)
        {
            var user = new MongoUser(credentials, readOnly);

            AddUser(user);
        }
コード例 #18
0
        public override MembershipUser CreateUser(string username,
            string password,
            string email,
            string passwordQuestion,
            string passwordAnswer,
            bool isApproved,
            object providerUserKey,
            out MembershipCreateStatus status)
        {
            MembershipCreateStatus tempStatus = new MembershipCreateStatus();
            MembershipUser retval = null;

            ValidatePasswordEventArgs args = new ValidatePasswordEventArgs(username, password, true);
            OnValidatingPassword(args);

            if (args.Cancel)
            {
                tempStatus = MembershipCreateStatus.InvalidPassword;
            }
            else if (RequiresUniqueEmail && !string.IsNullOrWhiteSpace(GetUserNameByEmail(email)))
            {
                tempStatus = MembershipCreateStatus.DuplicateEmail;
            }
            else
            {
                MembershipUser user = GetUser(username, false);

                if (user == null)
                {
                    ObjectId id = default(ObjectId);
                    string key = providerUserKey != null ? providerUserKey.ToString() : null;
                    if (!string.IsNullOrWhiteSpace(key) && !ObjectId.TryParse(key, out id))
                    {
                        tempStatus = MembershipCreateStatus.InvalidProviderUserKey;
                    }
                    else
                    {
                        var now = DateTime.Now;
                        var newUser = new MongoUser()
                        {
                            _id = id,
                            UserName = username,
                            Password = EncodePassword(password),
                            Email = email,
                            PasswordQuestion = passwordQuestion,
                            PasswordAnswer = passwordAnswer,
                            IsApproved = isApproved,
                            CreationDate = now,
                            LastPasswordChangedDate = now,
                            LastActivityDate = now,
                            LastLockoutDate = now,
                            FailedPasswordAttemptWindowStart = now,
                            FailedPasswordAnswerAttemptWindowStart = now,
                            Comment = string.Empty,
                            ProviderName = this.Name
                        };

                        var db = MongoDatabase.Create(ConnectionString);
                        using (db.RequestStart())
                        {
                            db.GetCollection<MongoUser>(typeof(MongoUser).Name).Insert(newUser);
                        }

                        retval = newUser.ToMembershipUser();
                        tempStatus = MembershipCreateStatus.Success;
                    }
                }
                else
                {
                    tempStatus = MembershipCreateStatus.DuplicateUserName;
                }
            }

            status = tempStatus;
            return retval;
        }