简体   繁体   English

手动密码更新以明文形式存储新密码,而不是哈希

[英]Manual Password Update Stores the New Password in Clear Text Instead of Hashed

I want to update the password for a single user when he tries to reset his password. 我想为单个用户尝试重设密码时更新密码。

Instead of using Membership.Provider.ResetPassword function, I want to have more control on the password that is being generated. 我不想使用Membership.Provider.ResetPassword函数,而是希望对正在生成的密码有更多控制。 The generated password may only contain upper and lowercase letters and numbers. 生成的密码只能包含大写和小写字母和数字。

So I tried: 所以我尝试了:

web.config web.config中

  <providers>
    <clear />
    <add name="MyMembershipProvider" 
         type="Sample.MyMembershipProvider" 
         connectionStringName="conn1" 
         requiresQuestionAndAnswer="false" 
         applicationName="/wedding" 
         minRequiredPasswordLength="6" 
         enablePasswordRetrieval="false" 
         enablePasswordReset="true" 
         requiresUniqueEmail="true" 
         minRequiredNonalphanumericCharacters="0" passwordFormat="Hashed" />
  </providers>

resetpassword.aspx.vb resetpassword.aspx.vb

Dim newpassword As String = GlobalFunctions.CreateNewPassword(8)'this generated the password in the format I want

Dim myConnection As SqlConnection = GetConnection()

            Dim cmd As New SqlCommand("SELECT au.username, aa.ApplicationName, password, passwordformat, passwordsalt " & _
                "FROM aspnet_membership am " & _
                "INNER JOIN aspnet_users au " & _
                "ON (au.userid = am.userid) " & _
                "INNER JOIN aspnet_applications aa " & _
                "ON (au.applicationId = aa.applicationid) " & _
                "WHERE am.email=@email", myConnection)
            cmd.Parameters.Add(New SqlParameter("@email", Email))

Then use this to update the password using a standard ASP.NET stored procedure: 然后使用此命令通过标准ASP.NET存储过程更新密码:

    Public Shared Function SetMemberPassword(ByVal ApplicationName As String, ByVal UserName As String, _
                    ByVal NewPassword As String, ByVal PasswordSalt As String, ByVal CurrentTimeUtc As DateTime, _
                    ByVal PasswordFormat As Integer) As Boolean
        Dim result As Boolean
        Dim myConnection As SqlConnection = GetConnection()
        Dim cmd As New SqlCommand("aspnet_Membership_SetPassword", myConnection)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.Add(New SqlParameter("@ApplicationName", ApplicationName))
        cmd.Parameters.Add(New SqlParameter("@UserName", UserName))
        cmd.Parameters.Add(New SqlParameter("@NewPassword", NewPassword))
        cmd.Parameters.Add(New SqlParameter("@PasswordSalt", PasswordSalt))
        cmd.Parameters.Add(New SqlParameter("@CurrentTimeUtc", CurrentTimeUtc))
        cmd.Parameters.Add(New SqlParameter("@PasswordFormat", PasswordFormat))
        Try
            myConnection.Open()
            result = CBool(cmd.ExecuteScalar)
        Catch ex As Exception

    Finally
            myConnection.Close()
        End Try
        Return result
    End Function

Besides that the new password does not even work (probably because I have password format "Hashed" in my membershiprovider in web.config), the password is also stored in clear text, which I don't want. 除此之外,新密码甚至不起作用(可能是因为我在web.config的Membershiprovider中具有密码格式“ Hashed”),而且密码也以明文形式存储,这是我所不希望的。

So, how can I update the user's password and store the password hashed? 那么,如何更新用户密码并存储散列的密码?

Why you need to call direct stored procedure? 为什么需要调用直接存储过程? ASP.NET Membership provider takes care of everything and you need to just use update user method of asp.net provider for updating everything except password. ASP.NET成员资格提供程序负责所有事务,您只需要使用asp.net提供程序的更新用户方法来更新除密码以外的所有内容。

See all methods available in ASP.NET and SQL Membership provider in following link. 在下面的链接中查看ASP.NET和SQL成员资格提供程序中可用的所有方法。 http://msdn.microsoft.com/en-us/library/ff648345.aspx http://msdn.microsoft.com/en-us/library/ff648345.aspx

You just need to use following code to update user information method. 您只需要使用以下代码来更新用户信息方法。 with other information required. 并需要其他信息。

Membership.UpdateUser

For updating password method. 用于更新密码的方法。 You can use change password method. 您可以使用更改密码方法。

http://msdn.microsoft.com/en-us/library/system.web.security.sqlmembershipprovider.changepassword(v=vs.110).aspx http://msdn.microsoft.com/en-us/library/system.web.security.sqlmembershipprovider.changepassword(v=vs.110).aspx

Membership.Provider.ChangePassword(User.Identity.Name, "OldPassword", "NewPassword");

Right now its happening because you are directly calling asp.net membership provider stored procedure so it will directly update password without encrypting it. 现在发生这种情况是因为您直接调用asp.net成员资格提供程序存储过程,因此它将直接更新密码而不进行加密。

While inbuilt method will encrypt it and then it will stored this. 而内置方法将对其加密,然后将其存储。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM