Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

April 26, 2004 12:00 AM

Q. How can I create a Web page where users can change their passwords?

Windows IT Pro
InstantDoc ID #42425
Rating: (7)

A. You can write an Active Server Pages (ASP) script that creates a password-change Web page. ASP gives you complete access to Microsoft Active Directory Service Interfaces (ADSI), which lets you perform a variety of functions, such as changing passwords or creating accounts. When you write such a script, you must consider factors such as the user account under which the script will run and the permissions you want to use when the script runs. The basic ADSI command to change a user's password is

set usr = GetObject("LDAP://CN=John

Savill,CN=Users,DC=savilltech,DC=com")

usr.put "userPassword", NewPassword

The first line (shown as two lines because of space constraints) assigns a handle to user John Savill in domain savilltech.com. The next line puts the text NewPassword into the userPassword attribute.

I've written a short ASP script called Changepass.asp that prompts the user to enter a username and password (remember to change the domain from savilltech.com to your domain). Changepass.asp, which is available at code , is listed below.

&lt%
strUserCN = request.form("cn")
strNewPassword = request.form("newpass")
strPassVerify = request.form("passverify")

if strUserCN="" then
    response.write "&lthtml&gt&lthead&gt&lttitle&gtChange Password&lt/title&gt&lt/head&gt&ltbody&gt"
    response.write "&ltcenter&gt&lth1&gtWeb Password Reset&lt/h1&gt&lt/center&gt"
    response.write "&lthr&gt&ltbr&gt&ltbr&gt&ltform method=post action=changepass.asp&gt&lttable&gt"
    response.write "&lttr&gt&lttd&gtCN: &lt/td&gt&lttd&gt&ltinput type=text name=cn&gt&lt/td&gt&lttr&gt"
    response.write "&lttr&gt&lttd&gtNew Password: &lt/td&gt&lttd&gt&ltinput type=password name=newpass&gt&lt/td&gt&lt/tr&gt"
    response.write "&lttr&gt&lttd&gtVerify Password: &lt/td&gt&lttd&gt&ltinput type=password name=passverify&gt&lt/td&gt&lt/tr&gt"
    response.write "&lttr&gt&lttd colspan=2 align=center&gt&ltinput type=submit value='Reset Password'&gt&lt/td&gt&lt/tr&gt"
    response.write "&lt/table&gt&lt/body&gt&lt/html&gt"
    response.end
else

if strNewPassword = strPassVerify then

set usr = GetObject("LDAP://CN=" & strUserCN & ",CN=Users,DC=savilltech,DC=com")

usr.put "userPassword", strNewPassword

response.write "&lthtml&gt&lthead&gt&lttitle&gtResults&lt/title&gt&lt/head&gt&ltcenter&gt&lth1&gtUpdate Results&lt/h1&gt&lt/center&gt&lthr&gt&ltbr&gt&ltbr&gt"
response.write strUserCN & ": password was successfully updated"
response.end

else

    response.write "&lthtml&gt&lthead&gt&lttitle&gtError!&lt/title&gt&lt/head&gt&ltbody&gt"
    response.write "&ltcenter&gt&lth1&gtAn Error Has Occurred!&lt/h1&gt&lt/center&gt"
    response.write "&lthr&gt&ltbr&gt&ltbr&gt"
    response.write "The password and confirmation do not match. Please go back and try again."
    response.end

end if
end if
%&gt

Windows Server 2003 provides its own Web pages for password changes, which I discuss in the FAQ "Does Windows Server 2003 provide a way to let users change their passwords remotely on the Web?". However, you might find the sample ASP script useful for creating password-change interfaces on your own Web pages or sites.

Related Content:

ARTICLE TOOLS

Comments
  • rm.fish
    6 years ago
    May 31, 2006

    I copy a asp code that works bwlow. However, I'm seeking for a code that user need to type there old password for verify the user. Hope anyone could tell what was wrong here?!
    Thanks.


    <%
    strUserCN = request.form("cn")
    strOpassword = request.form("oldpass")
    strNewPassword = request.form("newpass")
    strPassVerify = request.form("passverify")

    if strUserCN="" then
    response.write "Change Password"
    response.write "

    Web Password Reset

    "
    response.write "


    "
    response.write ""
    response.write ""
    response.write ""
    response.write ""
    response.write ""
    response.write "
    CN:
    Old Password:
    New Password:
    Verify Password:
    "
    response.end
    else

    set usr = GetObject("LDAP://CN=" & strUserCN & ",CN=Users,DC=savilltech,DC=com")

    if strNewPassword = strPassVerify then
    if usr.userPassword <> strOpassword then
    usr.put "userPassword", strNewPassword

    response.write "Results

    Update Results




    "
    response.write strUserCN & ": password was successfully updated"
    response.end

    else

    response.write "Error!"
    response.write "

    An Error Has Occurred!

    "
    response.write "


    "
    response.write "The password and confirmation do not match. Please go back and try again."
    response.end

    end if
    end if
    end if
    %>

    if strNewPassword = strPassVerify then
    if usr.userPassword <> strOpassword then
    usr.put "userPassword", strNewPassword

  • Richard
    6 years ago
    Jan 03, 2006

    I get an error at the set usr = ... line - is this down to permissions?

  • Anonymous User
    7 years ago
    Jul 19, 2005

    IT DOES NOT WORK!! PLEASE TELL ME WHAT TO DO TO MAKE IT WORK

  • briantrebs
    8 years ago
    Oct 11, 2004

    Or better still a dropdown list of users in the domain?

  • briantrebs
    8 years ago
    Oct 11, 2004

    This is great however, i would like to change the passwords for a user that can be within any OU. is there anyway of getting the users OU from the username entered on the web page?

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.