/// <summary> /// Adds a user to this database. /// </summary> /// <param name="user">The user.</param> public static void AddUserEx(MongoCollection Col, User user) { var document = Col.FindOneAs <BsonDocument>(Query.EQ("user", user.Username)); if (document == null) { document = new BsonDocument("user", user.Username); } document["roles"] = user.roles; if (document.Contains("readOnly")) { document.Remove("readOnly"); } //必须要Hash一下Password document["pwd"] = MongoUser.HashPassword(user.Username, user.Password); //OtherRole 必须放在Admin.system.users里面 if (Col.Database.Name == MongoDbHelper.DATABASE_NAME_ADMIN) { document["otherDBRoles"] = user.otherDBRoles; } if (string.IsNullOrEmpty(user.Password)) { document["userSource"] = user.userSource; } Col.Save(document); }
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); }
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); }