0

I'm working on a small app to change user password in AD using ldap3 and flask as a front end.

It works perfectly in the desktop IDLE and the password gets changed in AD

I'm able to make it work successfully without using Flask, but wen I move the code into flask I get a Indexerror: List index out of range

Error:

Line 61 entry = conn.entries[0] user_dn = str(entry.distinguishedName)

r = conn.extend.microsoft.modify_password(user_dn, pwd)
IndexError: list index out of range






from ldap3 import Server, Connection, ALL, NTLM
import ldap3
from flask import render_template, redirect, Flask, request, url_for
import time
import datetime, os
import smtplib

timestr = time.strftime("%Y-%m-%d")
app = Flask(__name__)
app.config['SECRET_KEY'] = os.urandom(24).hex()

def get_user_email(user):
    user = user

    #Connect to Server
    server = Server('server.com', use_ssl=True, get_info=ALL)
    conn = Connection(server, user='domain\\adminuser', password='adminpassword', authentication=NTLM, auto_bind=True)
    dn = "OU=CON1,OU=CON2,OU=CONT3,DC=domain,DC=suf1,DC=ct,DC=suf2"
    conn.search(dn, '(&(objectClass=user)(userPrincipalName='+user+'@domain.suf1.suf2))',attributes=['*'])
    entry = conn.entries[0]
    user_dn = entry.distinguishedName
    return entry.mail

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/myaccount', methods = ['POST', 'GET'])
def myaccount():

if request.method == 'POST':
    user_name = request.form['user_name']
    return render_template('found.html',user_name=get_user_email(user_name))

 return 'Account Not Found'

@app.route('/changepw', methods = ['POST', 'GET'])
def changepw():
    if request.method == 'POST':
    user_name = request.form['user_name']
    pwd = request.form['pwd']
    print(user_name)
    change_pw(user_name,pwd)
    return 'Success'
return redirect(url_for('index'))

if __name__ == "__main__":
    app.run(debug=True, host='0.0.0.0', port=8080)
ivon pint
  • 3
  • 3
  • 1
    Welcome to stack overflow! It would help a lot if you would edit your question to include the full error traceback – G. Anderson Aug 30 '19 at 19:38
  • Edited... sorry my first time on stackoverflow – ivon pint Aug 30 '19 at 20:19
  • No need to apologize, everyone was new once! – G. Anderson Aug 30 '19 at 20:22
  • Based on that error, your `conn.entries` list is empty or `None`, so it doesn't have a `0`th item to check against. Given that, it may be environment specific as to why the connection is not returning what you think it should be. – G. Anderson Aug 30 '19 at 20:23
  • I agree, I'm trying to figure out why it works on the desktop successfully and I'm able to access all the .values in conn.entries[0]. if i print(entry) I can see all the data – ivon pint Aug 30 '19 at 20:25

0 Answers0