1
 public ActionResult Register(RegisterModel RegisterModel, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                MembershipCreateStatus createStatus;

                System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
                Byte[] Password = encoding.GetBytes(RegisterModel.Password);
                var EncryptedPass = MembershipProvider.EncryptPassword(Password);

                Membership.CreateUser(RegisterModel.UserName, RegisterModel.Password, RegisterModel.Email, null, null, true, null, out createStatus);

                if (createStatus == MembershipCreateStatus.Success)
                {
                    FormsAuthentication.SetAuthCookie(RegisterModel.UserName, false /* createPersistentCookie */);
                    return Redirect(returnUrl ?? Url.Action("Index", "Education"));
                }
                else
                {
                    ModelState.AddModelError("", ErrorCodeToString(createStatus));
                }
            }

            // If we got this far, something failed, redisplay form
            return View(RegisterModel);
        }

尝试在使用 EncryptPassword 方法将密码存储到数据库之前对其进行加密,然后使用 MembershipProvider.DecryptPassword 方法对其进行 Dycrypt,但收到“由于其保护级别而无法访问”错误警告。

4

1 回答 1

1

因为MembershipProvider.EncryptPasswordMembershipProvider.DecryptPassword方法都受到保护。

因此,您将无法在类或其派生类定义之外访问它们。

并且在通过Membership.CreateUser它创建任何用户时会自动加密密码。

于 2012-10-24T05:47:46.027 回答