在Python / C中验证Windows服务器用户

时间:2014-03-19 10:40:20

标签: python c windows

我想用Python(2.6或2.7)/ C代码验证用户身份。

我必须创建一个简单的身份验证模块,它将username and password作为输入,并将验证用户(如UNIX的{​​{3}}模块。)

有什么办法吗?

1 个答案:

答案 0 :(得分:3)

您是否正在寻找类似LogonUser?的内容来验证本地用户,例如:

#!python
# -*- coding: utf-8 -*-

from os import environ
from ctypes import windll, byref, POINTER, FormatError, GetLastError
from ctypes.wintypes import (
  LPCWSTR, WCHAR, DWORD, HANDLE, BOOL
)

MAX_COMPUTERNAME_LENGTH = 15
LOGON32_LOGON_NETWORK_CLEARTEXT = 8
LOGON32_PROVIDER_DEFAULT = 0

CloseHandle = windll.Kernel32.CloseHandle
CloseHandle.argtypes = [HANDLE]
CloseHandle.restype = BOOL

LogonUser = windll.Advapi32.LogonUserW
LogonUser.argtypes = [LPCWSTR, LPCWSTR, LPCWSTR, DWORD, DWORD, POINTER(HANDLE)]
LogonUser.restype = BOOL

if __name__ == '__main__':
  username = 'MyUsername'
  password = 'MyPassword'

  token = HANDLE()
  status = LogonUser(username,
                     environ['COMPUTERNAME'],
                     password,
                     LOGON32_LOGON_NETWORK_CLEARTEXT,
                     LOGON32_PROVIDER_DEFAULT,
                     byref(token))
  error = GetLastError()

  if status:
    print('OK, successes')
    CloseHandle(token)
  else:
    print(FormatError(error))