解锁并重置Active Directory密码Coldfusion

时间:2017-02-10 21:08:32

标签: java coldfusion active-directory

我使用CFLDAP创建了一个下拉列表。下拉列表是用户名,我将允许密码解锁和重置。

<div id="DESDD" class="form-group" style="display:none;">
                    <select name="DES" id="DES" class="form-control">
                        <option value="" selected>Please select user</option>
                        <cfoutput query="CreateDESDropdown">
                            <option value="#samaccountname#">#cn#</option>
                        </cfoutput>
                    </select>
                </div>

<div id="pass" class="form-group" style="display:none;">
                    <input type="password" name="user_pass" id="user_pass" class="form-control" placeholder="Password" required="">
                </div>
                <div id="sub" class="form-group" style="display:none;">
                    <button type="submit" name="login_user" class="btn btn-primary block full-width m-b">Reset</button>
                </div>

我被困在这里我无法弄清楚如何让用户选择那里的名字,并能够输入一个新密码,并在提交时让它在活动目录中更改密码。

所以基本上我有一个登录页面,只允许管理员进入。然后它将它们带到这个页面,这是一个名称和密码输入文本框的下拉列表。使用重置提交按钮。有人请告诉我如何允许用户从下拉列表中选择一个名称,然后输入密码并重置提交?

我无法弄清楚如何从这里开始。

(显然在这里张贴图片已被破坏) enter image description here

我尝试过:

<cftry>
    <cfscript>
        // You are going to use  the user's credentials to login to LDAP
        // Assuming your LDAP is set up to do so

        // Set up variables
        newPassword = '"thenewpassword!"';
        oldPassword = '"oldpassword"';
        // You would probably pass in a variable here, I typed it out so you would ss the format its expecting
        newUnicodePassword = newPassword.getBytes("UnicodeLittleUnmarked");
        oldUnicodePassword = oldPassword.getBytes("UnicodeLittleUnmarked");
        ldapsURL = "servername:portnumber";

        // Create a Java Hashtable
        javaEnv = CreateObject("java", "java.util.Hashtable").Init();

        // Put stuff in the Hashtable
        javaEnv.put("java.naming.provider.url", ldapsURL);
        // The user's Full DN and Password
        javaEnv.put("java.naming.security.principal", "#distinguishedName#");
        javaEnv.put("java.naming.security.credentials", "#currentPassword#");
        javaEnv.put("java.naming.security.authentication", "simple");
        javaEnv.put("java.naming.security.protocol", "ssl");
        javaEnv.put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory");

        // Create a Java InitialDirContext
        javaCtx = CreateObject("java", "javax.naming.directory.InitialDirContext").Init(javaEnv);

        // Create two Java BasicAttributes
        oldBA = CreateObject("java", "javax.naming.directory.BasicAttribute").Init("unicodePwd", oldUnicodePassword);
        newBA = CreateObject("java", "javax.naming.directory.BasicAttribute").Init("unicodePwd", newUnicodePassword);

        /***********************************************
        *   Stick the attributes into an Java Array and tell it what to do with them
        *   Guess what? A CF Array = a Java Array
        *   1 = DirContext.ADD_ATTRIBUTE
        *   2 = DirContext.REPLACE_ATTRIBUTE
        *   3 = DirContext.REMOVE_ATTRIBUTE
        *  This is the big trick 
        *   If you login above as an admin then you only need to do a 2 Replace but will not run LDAP passoword policy (lenght, complexity, history... etc.)
        *       It will let you change password to anything
        *   If you want to check the LDAP password policy then you need to create the array and first Remove (3) then Add (1)
        *       Error Code 19 means something in the LDAP password policy was violated
        *           I haven't figured out how to read what the error is (like "password length too short" or "you have used this password in the past")
        *       Error Code 49 means invalid username/password
        ************************************************/
        mods = [
            createObject( "java", "javax.naming.directory.ModificationItem").init(3, oldBA),
            createObject( "java", "javax.naming.directory.ModificationItem").init(1, newBA)
        ]; 
        // Run it
        javaCtx.modifyAttributes(distinguishedName,mods);
        javaCtx.close();
    </cfscript>
    // Yeah! I could have scripted the cfcatch but this was easier.
    <cfcatch>
        <cfif find('error code 19',cfcatch.message)>
            <!--- I am using cfwheels so this just displays a nice error message on the next page --->
            <cfset flashInsert(error="New password does not meet requirements defined in the password rules.")>
        <cfelseif isDefined('cfcatch.RootCause.cause.Explanation') and find('error code 49', cfcatch.RootCause.cause.Explanation)>
            <!--- I am using cfwheels so this just displays a nice error message on the next page --->
            <cfset flashInsert(error="Current Password IS incorrect.")>
        <cfelse>    
            <!--- This just pukes the error up hard and uncaught --->
            <cfrethrow>
        </cfif>
        <cfset hasError = true>
    </cfcatch>  
</cftry>

<cfset new_password = '"thenewpassword"' />
<cfset unicodePwd = new_password.getBytes("UnicodeLittleUnmarked") />

<cfset javaEnv = CreateObject("java", "java.util.Hashtable").Init() />

<cfset ldapsURL = "servername:serverport" />
<cfset javaEnv.put("java.naming.provider.url", ldapsURL) />
<cfset javaEnv.put("java.naming.security.credentials", "oldpassword") />
<cfset javaEnv.put("java.naming.security.authentication", "simple") />
<cfset javaEnv.put("java.naming.security.protocol", "ssl") />
<cfset javaEnv.put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory") />

<cfset javaCtx = CreateObject("java", "javax.naming.directory.InitialDirContext").Init(javaEnv) />
<cfset javaAttr = CreateObject("java", "javax.naming.directory.BasicAttributes").Init("unicodePwd", unicodePwd) />


<cfset javaCtx.close() />

0 个答案:

没有答案