简体   繁体   English

django 通过 LDAP 进行用户身份验证

[英]django User authentication via LDAP

I'm trying to check the user login in my django project against our AD via ldap.我正在尝试通过 ldap 针对我们的 AD 检查我的 django 项目中的用户登录。 I found a lot of tutorials online that I've tried so far.我在网上找到了很多我到目前为止尝试过的教程。

For some reason the authenticate(username, password) -function returns None .由于某种原因, authenticate(username, password) -函数返回None

Here is my Code so far:到目前为止,这是我的代码:

views.py (login) views.py(登录)

def login_view(request):
    if not request.user.is_authenticated:
        if request.method == 'POST':
            login_form = Login_form(request.POST)
            if login_form.is_valid():
                username = login_form.data.get('username')
                password = login_form.data.get('password')
                domain_name = "@my.domain.com"
                if domain_name not in username:
                    username += domain_name
                try:
                    user = authenticate(username=username, password=password)
                    print(user) # this gives me None
                    if user is not None:
                        if user.is_active:
                            login(request=request, user=user)
                            return redirect('index')
                    else:
                        form = AuthenticationForm()
                        messages.error(request, 'Try again!')
                        return render(request, 'myapp/login.html', {'form': form})
                except ldap.LDAPError as e:
                    print(e) # no error is displayed here
                    form = AuthenticationForm()
                    messages.error(request, 'Try again!')
                    return render(request, 'myapp/login.html', {'form': form})
          ### Some more funcs to  
          ### redirect to login.html
          ### if the login fails

settings.py:设置.py:

AUTHENTICATION_BACKENDS = (
    'django_auth_ldap.backend.LDAPBackend',
)

AUTH_LDAP_SERVER_URI = "ldap://my.domain.com:389"
AUTH_LDAP_BIND_DN = "CN=Users,DC=my,DC=domain,DC=com"
AUTH_LDAP_BIND_PASSWORD = ""    # I tried with blank password for anonymous bind or
                                # with "%(password)s" as template but I don't know if that's possible
                                # and also without the AUTH_LDAP_BIND_PASSWORD setting
AUTH_LDAP_CONNECTION_OPTIONS = {ldap.OPT_REFERRALS: 0}
AUTH_LDAP_USER_ATTR_MAP = {'group': "memberof", "first_name": "givenName", "last_name": "sn"}
AUTH_LDAP_USER_SEARCH = LDAPSearch("DC=my,DC=domain,DC=com,CN=Users",
                                   ldap.SCOPE_SUBTREE, "(sAMAccountName=%(user)s)") 
# since we need to login via username i need to search for username@my.domain.com 
# so I try to search for sAMAccountName

A while ago i wrote a LDAP-login-script in PHP with works like a charm and all the DN, bindings and search are the same.不久前,我在 PHP 中编写了一个 LDAP 登录脚本,其工作原理类似于魅力,所有 DN、绑定和搜索都是相同的。

So my question is:所以我的问题是:

Where is it going wrong or what did I miss?哪里出了问题或者我错过了什么?

I would highly recommend using django-python3-ldap .我强烈推荐使用django-python3-ldap We have used this package in production for years after trying the others, it works, and is written entirely in Python 3: https://github.com/etianen/django-python3-ldap在尝试其他产品多年后,我们在生产中使用了这个 package 多年,它可以工作,并且完全用 Python 3 编写: httpspython: //3-ldap/djangoetian/github-ldap/

We use it on port 636 and ldaps and it works as well.我们在端口 636 和ldaps上使用它,它也能正常工作。

It keeps us from having to write our own custom backend or login method;它使我们不必编写自己的自定义后端或登录方法; all we had to do were change some settings and write the format_username function.我们所要做的就是更改一些设置并写入format_username function。 The README has good information about hooking into Active Directory: I'd start with that configuration, and see how it works.自述文件有关于连接到 Active Directory 的很好信息:我将从该配置开始,看看它是如何工作的。 Good luck!祝你好运!

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

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