使用MembershipCreateStatus时的提供程序错误

时间:2017-01-03 09:20:43

标签: c# asp.net-mvc-4

我在堆栈溢出中搜索了这个问题,并尝试了其中一些,但它没有解决,我的项​​目昨天工作正常,但今天我想注册一个新用户,我得到提供商错误! 这是我在帐户控制器中的注册方法:

 public ActionResult Register(RegisterModel model)
    {
        var _userdata = new UsersDataAccess();
        var _u = _userdata.FindUser(model.UserName);
        if (_u)
        {
            ModelState.AddModelError("UserName", " کد کاربری قبلا استفاده شده است.");
        }
        else
        {
            if (ModelState.IsValid)
            {
                var samaMembershipProvider = Membership.Provider as SamaMembershipProvider;
                MembershipCreateStatus createStatus = new MembershipCreateStatus();
                if (model.ConfirmPassword==model.Password)
                {
              samaMembershipProvider.CreateUser(
                        model.UserName,
                        model.Password,
                        model.Email,
                        true,
                        model.FirstName,
                        model.LastName,
                        model.gender,
                        model.Mobile,
                        new string[] { RoleEnum.Users.ToString() },
                         out createStatus
                        );
                    switch (createStatus)
                    {
                        case MembershipCreateStatus.Success:
                            FormsAuthentication.SetAuthCookie(model.Email, false);
                            return RedirectToAction("Login", "Account");

                        case MembershipCreateStatus.DuplicateEmail:
                            ModelState.AddModelError("Email", " آدرس ایمیل قبلا در سامانه ثبت شده است");
                            break;
                        case MembershipCreateStatus.DuplicateUserName:
                            ModelState.AddModelError("UserName", "  کد کاربری قبلا در سامانه ثبت شده است");
                            break;
                        case MembershipCreateStatus.InvalidPassword:
                            ModelState.AddModelError("Password", "کلمه عبور وارد شده معتبر نیست");
                            break;
                        //case MembershipCreateStatus.InvalidAnswer:
                        //    TempData["Alert"] = " خطا در ذخیره اطلاعات " + " نقش دانشجو وجود ندارد " + "مجددا تلاش کنید. در صورتی که موفق نشدید با مدیر سیستم تماس بگیرید";
                        //    break;

                        default:
                            TempData["Alert"] = " خطا در ذخیره اطلاعات " + createStatus.ToString();
                            break;
                    }

                }

                else
                {
                   ModelState.AddModelError("ConfirmPassword","کلمه عبور و تکرار کلمه عبور با هم برابر نیستند");
                }



            }
        }


        return View();

    }

但是在samaMembershipProvider.CreateUser中,createStatus返回providererror。

这是我在MembershipProvider

中更改的唯一方法
//CreateUser by Golnaz
    public  MembershipUser CreateUser
       (string username, string password, string email, bool isApproved, string FirstName, string LastName,
        int gender, string Mobile, string[] Role, out MembershipCreateStatus status)
    {
        ValidatePasswordEventArgs args = new ValidatePasswordEventArgs(username, password, true);

        OnValidatingPassword(args);

        if (args.Cancel)
        {
            status = MembershipCreateStatus.InvalidPassword;
            return null;
        }

        if (RequiresUniqueEmail && GetUserNameByEmail(email) != "")
        {
            status = MembershipCreateStatus.DuplicateEmail;
            return null;
        }

            DateTime createDate = DateTime.Now;
            int rowsAffected = 0;
            //int rowsAffected1 = 0; // GOLNAZ

            User user = db.Users.SingleOrDefault(u => u.UserName == username);

            if (user != null)
            {
                status = MembershipCreateStatus.DuplicateUserName;
                return null;
            }

            user = new User();
            user.UserName = username;
            user.Password = EncodePassword(password);
            user.PasswordSalt = "";
            user.Email = email;
            user.PasswordQuestion = null;
            user.PasswordAnswer = null;
            user.IsApproved = isApproved;
            user.IsLockedOut = false;
            user.LastActivityDate = createDate;
            user.CreateDate = createDate;
            user.LastLoginDate = createDate;
            user.LastLockedOutDate = createDate;
            user.LastPasswordChangedDate = createDate;
            user.FailedPasswordAttemptCount = 0;
            user.FailedPasswordAttemptWindowStart = createDate;
            user.FailedPasswordAnswerAttemptCount = 0;
            user.FailedPasswordAnswerAttemptWindowStart = createDate;
            user.Comment = "";

            //GOLNAZ
            user.Person = new Person();
            user.Person.FirstName = FirstName;
            user.Person.LastName = LastName;
            user.Person.GenderId = gender;
            user.Person.CellPhoneNumber = Mobile;

            //GOLNAZ

            foreach (var roleName in Role)
            {

                var role = db.Roles.FirstOrDefault(r => r.RoleName == roleName);

                if (role == null)
                {
                    // return invalid answer for invalid role
                    status = MembershipCreateStatus.InvalidAnswer;
                    return null;
                }

                user.Roles.Add(role);
            }



            try
            {
                db.Users.Add(user);//AddToUsers(user);
                rowsAffected = db.SaveChanges();

                if (rowsAffected > 0)
                    status = MembershipCreateStatus.Success;
                else
                    status = MembershipCreateStatus.UserRejected;
            }
            catch (Exception ex)
            {
                status = MembershipCreateStatus.ProviderError;

                if (WriteExceptionsToEventLog)
                    WriteToEventLog(ex, "CreateUser");
            }



            return GetUser(username, false);
        }

当我运行我的项目并在我的视图中使用帐户控制器中的断点填写注册表时,这就是我所看到的

enter image description here

0 个答案:

没有答案