1

我在使用自定义会员提供程序时遇到问题。我的类实现了 的所有方法MembershipProvider,但是当我使用 WSAT 安全部分时,我收到了以下消息:

There is a problem with your selected data store. This can be caused by an invalid server name or credentials, or by insufficient permission. It can also be caused by the role manager feature not being enabled. Click the button below to be redirected to a page where you can choose a new data store.

以下错误消息可能有助于诊断问题:The operation or method is not implemented

这是我的 web.config:

<membership defaultprovider="CustomProvider">
    <providers>
        <clear />
        <add name="CustomProvider" type="Owl.CustomMembershipProvider.UserManager" applicationname="TestApp" connectionstringname="QUESTIONNARIESSql" enablepasswordretrieval="false" enablepasswordreset="false" maxinvalidpasswordattempts="5" />
   </providers>
</membership>

这是我的 CustomMembership 类:

public class UserManager : MembershipProvider 
{
        public UserManager(): base()
        {

        }

        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {

            // Inicializar
            base.Initialize(name, config);
        }

        public MembershipUser CreateUser(string email, out MembershipCreateStatus status)
        {
            MembershipUser uInfo = new UserInfo(email, "CustomMembershipProvider", Guid.NewGuid());

            try
            {
                using (QuestionnarieEntities context = new QuestionnarieEntities())
                {
                    string auxEmail = (from u in context.UsersData
                                       where u.UserEmail == email
                                       select u.UserEmail).FirstOrDefault();

                    if (auxEmail == null)
                    {
                        UsersData userData = new UsersData();
                        userData.UserEmail = email;
                        userData.UserId = Guid.NewGuid();

                        context.AddToUsersData(userData);
                        context.SaveChanges();
                        status = MembershipCreateStatus.Success;
                        return uInfo;
                    }
                    else
                    {
                        status = MembershipCreateStatus.DuplicateEmail;
                        return uInfo;
                    }
                }

            }
            catch (Exception)
            {
                status = MembershipCreateStatus.UserRejected;
                throw;
            }

        }

        public override bool DeleteUser(string email, bool deleteAllRelatedData)
        {
            try
            {
                // implementar exclusão
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public override MembershipUser GetUser(string email, bool userIsOnline)
        {

            MembershipUser uInfo = new UserInfo(email, "CustomMembershipProvider", Guid.NewGuid()); ; 
            return uInfo;
        }

        public void UpdateUser(UsersData user)
        {

            try
            {
                using (QuestionnarieEntities context = new QuestionnarieEntities())
                {

                    UsersData userData = (from u in context.UsersData
                                          where u.UserEmail == user.UserEmail
                                          select u).FirstOrDefault();

                    userData.UF = user.UF;
                    userData.Age = user.Age;
                    userData.City = user.City;
                    userData.Color = user.Color;
                    userData.Country = user.Country;
                    userData.Gender = user.Gender;
                    userData.Occupation = user.Occupation;
                    userData.Schooling = user.Schooling;

                    context.SaveChanges();

                }

            }
            catch (Exception)
            {
                throw;
            }
        }

        public bool ValidateUser(string email)
        {

            using (QuestionnarieEntities context = new QuestionnarieEntities())
            {
                return true;
                //try
                //{
                //    UsersData userData = (from u in context.UsersData
                //                    where u.UserEmail == email
                //                    select u).FirstOrDefault();

                //    if (userData != null)
                //        return true;
                //    else
                //        throw new Exception("O usuário não foi validado!");
                //}
                //catch (Exception)
                //{
                //    return false;
                //}

            }
        }

        public override string ApplicationName
        {
            get
            {
                return "Authentication";
            }
            set
            {
                string apl= value;
            }
        }



        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            return true;
        }

        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            return true;
        }

        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            status = MembershipCreateStatus.Success;
            MembershipUser user = new UserInfo("sss","CustomProvider",Guid.NewGuid());
            return user;
        }

        public override bool EnablePasswordReset
        {
            get { return false; }
        }

        public override bool EnablePasswordRetrieval
        {
            get { return false; }
        }

        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
             MembershipUserCollection muc = new MembershipUserCollection();
            muc.Add(new UserInfo("sss","CustomProvider",Guid.NewGuid()));
            totalRecords = 1;
            return muc;
        }

        public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            MembershipUserCollection muc = new MembershipUserCollection();
            muc.Add(new UserInfo("sss", "CustomProvider", Guid.NewGuid()));
            totalRecords = 1;
            return muc;
        }

        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {
            MembershipUserCollection muc = new MembershipUserCollection();
            muc.Add(new UserInfo("sss", "CustomProvider", Guid.NewGuid()));
            totalRecords = 1;
            return muc; ;
        }

        public override int GetNumberOfUsersOnline()
        {
            return 1;
        }

        public override string GetPassword(string username, string answer)
        {
            return "teste";
        }

        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            MembershipUser uInfo = new UserInfo("luizdias@gmail.com", "CustomMembershipProvider", Guid.NewGuid()); ;
            return uInfo;
        }

        public override string GetUserNameByEmail(string email)
        {
            try
            {
                using (QuestionnarieEntities context = new QuestionnarieEntities())
                {
                   string auxEmail = (from u in context.UsersData
                                       where u.UserEmail == email
                                       select u.UserEmail).FirstOrDefault().ToString();

                   return auxEmail;

                }

            }
            catch (Exception)
            {
                return string.Empty;
                throw;
            }            
        }

        public override int MaxInvalidPasswordAttempts
        {
            get { return 3; }
        }

        public override int MinRequiredNonAlphanumericCharacters
        {
            get { return 0; }
        }

        public override int MinRequiredPasswordLength
        {
            get { return 2; }
        }

        public override int PasswordAttemptWindow
        {
            get { return 3; }
        }

        public override MembershipPasswordFormat PasswordFormat
        {
            get { return MembershipPasswordFormat.Clear; }
        }

        public override string PasswordStrengthRegularExpression
        {
            get { return ""; }
        }

        public override bool RequiresQuestionAndAnswer
        {
            get { return false; }
        }

        public override bool RequiresUniqueEmail
        {
            get { return false; }
        }

        public override string ResetPassword(string username, string answer)
        {
            return string.Empty;
        }

        public override bool UnlockUser(string userName)
        {
            return false; // implementei
        }

        public override void UpdateUser(MembershipUser user)
        {
            string s = "s" + "s";
        }

        public override bool ValidateUser(string username, string password)
        {
            return true;
        }
    }
4

0 回答 0