Beispiel #1
0
        private static int FindRoleId(MySqlSecurityDbContext db, string roleName)
        {
            var role = db.Roles.SingleOrDefault(x => x.RoleName == roleName);

            if (role != null)
            {
                return(role.RoleId);
            }
            else
            {
                return(-1);
            }
        }
Beispiel #2
0
        private static List <int> GetRoleIdsFromNames(MySqlSecurityDbContext db, string[] roleNames)
        {
            List <int> roleIds = new List <int>(roleNames.Length);

            foreach (string role in roleNames)
            {
                int id = FindRoleId(db, role);
                if (id == -1)
                {
                    throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Resources.SimpleRoleProvider_NoRoleFound, role));
                }
                roleIds.Add(id);
            }
            return(roleIds);
        }
Beispiel #3
0
        private List <int> GetUserIdsFromNames(MySqlSecurityDbContext db, string[] usernames)
        {
            List <int> userIds = new List <int>(usernames.Length);

            foreach (string username in usernames)
            {
                int id = MySqlSimpleMembershipProvider.GetUserId(db, username);
                if (id == -1)
                {
                    throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Resources.Security_NoUserFound, username));
                }
                userIds.Add(id);
            }
            return(userIds);
        }
        // Ensures the user exists in the accounts table
        private int VerifyUserNameHasConfirmedAccount(MySqlSecurityDbContext db, string userName, bool throwException)
        {
            int userId = GetUserId(db, userName);
            if (userId == -1)
            {
                if (throwException)
                {
                    throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Resources.Security_NoUserFound, userName));
                }
                else
                {
                    return -1;
                }
            }

            int result = db.Memberships.Count(x => x.UserId == userId && x.IsConfirmed == true);

            if (result == 0)
            {
                if (throwException)
                {
                    throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Resources.Security_NoAccountFound, userName));
                }
                else
                {
                    return -1;
                }
            }
            return userId;
        }
        private string GetHashedPassword(MySqlSecurityDbContext db, int userId)
        {
            var membership = db.Memberships.SingleOrDefault(x => x.UserId == userId);

            if (membership != null)
                return membership.Password;
            else
                return null;
        }
        // Not used but CreateUser direct to database
        private void CreateUserRowByDatabase(MySqlSecurityDbContext db, string userName, IDictionary<string, object> values)
        {
            var newUserProfile = new UserProfile
            {
                UserName = userName,
            };
            int rows = -1;

            using (TransactionScope scope = new TransactionScope())
            {
                db.UserProfiles.Add(newUserProfile);
                rows = db.SaveChanges();

                if (values != null && values.Count > 0)
                {
                    var user = db.UserProfiles
                        .OrderByDescending(x => x.UserName)
                        .First(x => x.UserName == userName);
                    StringBuilder sql = new StringBuilder("UPDATE UserProfile SET ");

                    foreach (var key in values.Keys)
                    {
                        object value = values[key];

                        if (value == null)
                            continue;

                        if (value is String)
                            sql.AppendFormat("{0} = '{1}' ,", key, value);
                        else
                            sql.AppendFormat("{0} = {1} ,", key, value);
                    }

                    string sqlCommand = string.Format("{0} WHERE UserId = {1}", sql.ToString(0, sql.Length - 1), user.UserId);
                    rows += db.Database.ExecuteSqlCommand(sqlCommand);
                }

                scope.Complete();
            }

            if (rows == 0)
            {
                throw new MembershipCreateUserException(MembershipCreateStatus.ProviderError);
            }
        }
        private void CreateUserRow(MySqlSecurityDbContext db, string userName, IDictionary<string, object> values)
        {
            var newUserProfile = new UserProfile
            {
                UserName = userName,
            };

            if (values != null && values.Count > 0)
            {
                var type = Type.GetType(ConfigUtil.MySqlSecurityInheritedContextType, false, true);
                var contextAssembly = Assembly.GetAssembly(type);
                var userProfileExType = contextAssembly.GetTypes().FirstOrDefault(x => x.BaseType == typeof(MySql.Web.Security.UserProfile));

                if (userProfileExType != null)
                {
                    object userProfileEx = Activator.CreateInstance(userProfileExType);
                    var userNamePi = userProfileEx.GetType().GetProperty("UserName");

                    userNamePi.SetValue(userProfileEx, userName);

                    foreach (var key in values.Keys)
                    {
                        var pi = userProfileExType.GetProperty(key);

                        if (pi != null && pi.CanWrite == true)
                        {
                            object value = values[key];

                            if (value == null)
                                value = DBNull.Value;

                            pi.SetValue(userProfileEx, value);
                        }
                    }

                    var userProfileExDbSet = EntryBy(db, userProfileExType.FullName);	// get DbSet<UserProfile inherited class>
                    var addMethod = userProfileExDbSet.GetType().GetMethod("Add");		// get Add method info
                    addMethod.Invoke(userProfileExDbSet, new object[] { userProfileEx });		// invoke add UserProfile inherited class object
                }
            }
            else
                db.UserProfiles.Add(newUserProfile);

            int rows = db.SaveChanges();

            if (rows != 1)
            {
                throw new MembershipCreateUserException(MembershipCreateStatus.ProviderError);
            }
        }
        private bool CheckPassword(MySqlSecurityDbContext db, int userId, string password)
        {
            string hashedPassword = GetHashedPassword(db, userId);
            bool verificationSucceeded = (hashedPassword != null && Crypto.VerifyHashedPassword(hashedPassword, password));
            var membership = db.Memberships.SingleOrDefault(x => x.UserId == userId);

            if (verificationSucceeded)
            {
                if (membership != null)
                    membership.PasswordFailuresSinceLastSuccess = 0;
            }
            else
            {
                int failures = GetPasswordFailuresSinceLastSuccess(db, userId);
                if (failures != -1)
                {
                    if (membership != null)
                    {
                        membership.PasswordFailuresSinceLastSuccess = failures + 1;
                        membership.LastPasswordFailureDate = DateTime.Now;
                    }
                }
            }

            db.SaveChanges();

            return verificationSucceeded;
        }
        private static bool SetPassword(MySqlSecurityDbContext db, int userId, string newPassword)
        {
            string hashedPassword = Crypto.HashPassword(newPassword);
            if (hashedPassword.Length > 128)
            {
                throw new ArgumentException(Resources.SimpleMembership_PasswordTooLong);
            }

            // Update new password
            var membership = db.Memberships.SingleOrDefault(x => x.UserId == userId);

            if (membership == null)
                return false;

            membership.Password = hashedPassword;
            membership.PasswordSalt = string.Empty;
            membership.PasswordChangedDate = DateTime.Now;

            return (db.SaveChanges() > 0);
        }
        private static int GetPasswordFailuresSinceLastSuccess(MySqlSecurityDbContext db, int userId)
        {
            var membership = db.Memberships.SingleOrDefault(x => x.UserId == userId);

            if (membership != null)
                return membership.PasswordFailuresSinceLastSuccess;
            else
                return -1;
        }
 private static void CreateOAuthTokenTableIfNeeded(MySqlSecurityDbContext db)
 {
     // NOTICE: It does not needed in Entity Framework Code First
     // by X10-MOBILE\xyz37(Kim Ki Won) in Sunday, April 14, 2013 12:03 AM
 }
        internal static int GetUserId(MySqlSecurityDbContext db, string userName)
        {
            var result = db.UserProfiles.FirstOrDefault(x => x.UserName == userName);

            if (result != null)
                return result.UserId;
            else
                return -1;
        }
 internal static bool CheckTableExists(MySqlSecurityDbContext db, string tableName)
 {
     // NOTICE: It does not needed in Entity Framework Code First
     // by Kim Ki Won in Saturday, April 13, 2013 11:59 PM
     return true;
 }
 private List<int> GetUserIdsFromNames(MySqlSecurityDbContext db, string[] usernames)
 {
     List<int> userIds = new List<int>(usernames.Length);
     foreach (string username in usernames)
     {
         int id = MySqlSimpleMembershipProvider.GetUserId(db, username);
         if (id == -1)
         {
             throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Resources.Security_NoUserFound, username));
         }
         userIds.Add(id);
     }
     return userIds;
 }
 private static List<int> GetRoleIdsFromNames(MySqlSecurityDbContext db, string[] roleNames)
 {
     List<int> roleIds = new List<int>(roleNames.Length);
     foreach (string role in roleNames)
     {
         int id = FindRoleId(db, role);
         if (id == -1)
         {
             throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Resources.SimpleRoleProvider_NoRoleFound, role));
         }
         roleIds.Add(id);
     }
     return roleIds;
 }
        private static int FindRoleId(MySqlSecurityDbContext db, string roleName)
        {
            var role = db.Roles.SingleOrDefault(x => x.RoleName == roleName);

            if (role != null)
                return role.RoleId;
            else
                return -1;
        }