How to not use ASP.Net Membership Security Question and Answer for custom password recovery?

随声附和 提交于 2019-12-06 10:04:49

问题


I don't want to have the security question and answer feature that ASP.Net Membership Provider gives, but I DO want to enable a lost/forgotten password page.

This page would be where a user would enter his/her email address and an email would be sent to that address if the user was registered for them to reset their password via a link sent to that registered email address

I've created the custom table to track such requests, the random key assigned to the request as well as an expiry date on the request. However in writing the code to actually reset the password, I realised that there doesn't seem to be a method that does something like ResetPassword(email, newPassword) without needing to use the Security Q&A bit (which I don't have).

Is there any way to simply reset a user's password via a built in Membership function?

If not, how would I need to get this done?

Thanks in advance for any help given. -Nissan


回答1:


What I ended up doing was the following

public string ResetPassword(string email)
        {
            var m_userName = Membership.GetUserNameByEmail(email);
            var m_user = Membership.GetUser(m_userName);
            return m_user.ResetPassword();
        }

then I added a new method to use this value to change the password

public bool ChangeLostPassword(string email, string newPassword)
    {
        var resetPassword = ResetPassword(email);
        var currentUser = Membership.GetUser(Membership.GetUserNameByEmail(email), true);
        return currentUser.ChangePassword(resetPassword, newPassword);

    }



回答2:


Why don't you change this options in web.config?

         enablePasswordRetrieval="false"
         enablePasswordReset="true"
         requiresQuestionAndAnswer="false"

in

<membership>
   <providers>
      <clear/>
      <add name="AspNetSqlMembershipProvider" ...
      ..........


来源:https://stackoverflow.com/questions/920334/how-to-not-use-asp-net-membership-security-question-and-answer-for-custom-passwo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!