Wednesday, 22 May 2013

Change user password using changepassword control in asp.net membership provider


Change user password using changepassword control in asp.net membership provider


Introduction: 

In this article I will explain 
how to use changepassword control to change password in asp.net membership provider. 

Description: 

In previous posts I explained clearly how to create users using createuserwizard or programmatically create users using asp.net membership and I explained  how to get forgot password through email. In previous post Get forgot password through mail it will reset our password and sent randomly generated new password through mail. It’s very hard to remember randomly generated password. If we allow users to change their passwords directly that is very convenient. We can achieve this one easily by usingchangepassword control asp.net membership. 

changepassword control allows currently logged in users to change their password. If we want to change another user password then it’s not possible because every time it will take only currently logged in user. 

If we want change password for other users we need to set property DisplayUserName= "True" inchangepassword control (right click on changepassword >> change DisplayUserName="True"). It will add other control UserName in changepassword before set DisplayUserName property it contains only 3 controls those are old password, new password, confirm new password.

Now open your aspx drag and drop changePassword control from Login Controls section after that our page code will be like this  


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Change Password</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ChangePassword ID="ChangePassword1" runat="server" DisplayUserName="True">
</asp:ChangePassword>
</div>
</form>
</body>
</html>
Here don’t forgot to set database connection settings in web.config 

Write the following code in system.web section

<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider"connectionStringName="dbconnection" applicationName="SampleApplication"/>
</providers>
</membership>
<profile>
<providers>
<clear/>
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider"connectionStringName="dbconnection" applicationName="SampleApplication"/>
</providers>
</profile>
<roleManager enabled="true">
<providers>
<clear/>
<add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider"connectionStringName="dbconnection" applicationName="SampleApplication"/>
</providers>
</roleManager>
Now run your application and enter username, old password and new password after successful change of password we will get success message.

Demo


No comments:

Post a Comment