我可以使用用户Windows登录自动登录Web应用程序吗?

时间:2009-02-01 03:37:44

标签: python authentication web-applications windows-authentication

在我的兼职工作的Intranet上(与IT无关),我们使用的各种Web应用程序不需要显式登录。我们需要显然登录到Windows,然后对我们进行如何验证。

我想知道这是怎么做到的?在不担心安全问题的情况下,如何利用Windows登录信息对用户进行Web应用程序身份验证?我将使用Python(和Django)。

这是如何实现的?例如,是否需要特定的浏览器?应用程序和Intranet后端是否必须托管在同一位置或至少必须进行通信?或者只是获取用户的Windows凭据,并将其传递给Web应用程序的身份验证软件?

3 个答案:

答案 0 :(得分:2)

曾几何时Internet Explorer支持NTLM身份验证(类似于Basic Auth,但它将缓存的凭据发送到服务器,可以使用域控制器进行验证)。它用于在Intranet中启用单点登录,其中每个人都应该登录到域。我不记得它的细节,我已经多年没用过了。如果符合您的需求,它仍然是一个选项。

也许更熟悉它的人可能会有更多细节。

请参阅:NTLM Authentication Scheme for HTTP

使用非Microsoft服务器框架的棘手部分是与必要的服务进行通信以验证凭据。

答案 1 :(得分:1)

来自here

-- Added to settings.py --

### ACTIVE DIRECTORY SETTINGS

# AD_DNS_NAME should set to the AD DNS name of the domain (ie; example.com)  
# If you are not using the AD server as your DNS, it can also be set to 
# FQDN or IP of the AD server.

AD_DNS_NAME = 'example.com'
AD_LDAP_PORT = 389

AD_SEARCH_DN = 'CN=Users,dc=example,dc=com'

# This is the NT4/Samba domain name
AD_NT4_DOMAIN = 'EXAMPLE'

AD_SEARCH_FIELDS = ['mail','givenName','sn','sAMAccountName']

AD_LDAP_URL = 'ldap://%s:%s' % (AD_DNS_NAME,AD_LDAP_PORT)


-- In the auth.py file --

from django.contrib.auth.models import User
from django.conf import settings
import ldap

class ActiveDirectoryBackend:

  def authenticate(self,username=None,password=None):
    if not self.is_valid(username,password):
      return None
    try:
      user = User.objects.get(username=username)
    except User.DoesNotExist:
      l = ldap.initialize(settings.AD_LDAP_URL)
      l.simple_bind_s(username,password)
      result = l.search_ext_s(settings.AD_SEARCH_DN,ldap.SCOPE_SUBTREE, 
               "sAMAccountName=%s" % username,settings.AD_SEARCH_FIELDS)[0][1]
      l.unbind_s()

      # givenName == First Name
      if result.has_key('givenName'):
        first_name = result['givenName'][0]
      else:
        first_name = None

      # sn == Last Name (Surname)
      if result.has_key('sn'):
        last_name = result['sn'][0]
      else:
        last_name = None

      # mail == Email Address
      if result.has_key('mail'):
        email = result['mail'][0]
      else:
        email = None

      user = User(username=username,first_name=first_name,last_name=last_name,email=email)
      user.is_staff = False
      user.is_superuser = False
      user.set_password(password)
      user.save()
    return user

  def get_user(self,user_id):
    try:
      return User.objects.get(pk=user_id)
    except User.DoesNotExist:
      return None

  def is_valid (self,username=None,password=None):
    ## Disallowing null or blank string as password
    ## as per comment: http://www.djangosnippets.org/snippets/501/#c868
    if password == None or password == '':
      return False
    binddn = "%s@%s" % (username,settings.AD_NT4_DOMAIN)
    try:
      l = ldap.initialize(settings.AD_LDAP_URL)
      l.simple_bind_s(binddn,password)
      l.unbind_s()
      return True
    except ldap.LDAPError:
      return False

答案 2 :(得分:0)

据我所知,唯一自动传递登录凭据的浏览器是Internet Explorer。要启用此功能,请在安全性部分下的高级Internet选项对话框中选择“启用集成Windows身份验证”。通常默认启用此功能。

Web服务器必须从Web应用程序中删除匿名用户权限并选中Windows身份验证选项。只需将您想要访问Web应用程序的用户添加到文件/文件夹权限即可。

我只在IIS中试过这个,所以我不确定它是否适用于其他网络服务器。