使用Python脚本在ADFS上进行身份验证

Psy*_*oic 6 python parsing adfs

我需要解析被ADFS服务隐藏的站点。

并为此进行身份验证。

有进入的选项吗?

我可以看到,大多数针对后端应用程序或“系统用户”(带有app_id,app_secret)的解决方案。就我而言,我只能使用登录名和密码才能使用它。

问题的示例:在“ chrome我打开”中www.example.com,它将重定向到https://login.microsoftonline.com/,然后https://federation-sts.example.com/adfs/ls/?blabla使用登录名和密码形式。

以及如何访问它python3

gdl*_*lmx 5

ADFS uses complicated redirection and CSRF protection techniques. Thus, it is better to use a browser automation tool to perform the authentication and parse the webpage afterwards. I recommend the selenium toolkit with python bindings. Here is a working example:

from selenium import webdriver
def MS_login(usrname, passwd):  # call this with username and password
    driver = webdriver.Edge()   # change to your browser (supporting Firefox, Chrome, ...)
    driver.delete_all_cookies() # clean up the prior login sessions
    driver.get('https://login.microsoftonline.com/') # change the url to your website
    time.sleep(5) # wait for redirection and rendering

    driver.find_element_by_xpath("//input[@name='loginfmt'").send_keys(usrname)
    driver.find_element_by_xpath("//input[@type='submit']").click()
    time.sleep(5)

    driver.find_element_by_xpath("//input[@name='passwd'").send_keys(passwd)
    driver.find_element_by_xpath("//input[@name='KMSI' and @type='checkbox'").click()
    driver.find_element_by_xpath("//input[@type='submit']").click()
    time.sleep(5)

    driver.find_element_by_xpath("//input[@type='submit']").click()

    # Successfully login

    # parse the site ...

    driver.close() # close the browser
    return driver
Run Code Online (Sandbox Code Playgroud)

This script calls Microsoft Edge to open the website. It injects the username and password to the correct DOM elements and then let the browser to handle the rest. It has been tested on the webpage "https://login.microsoftonline.com". You may need to modify it to suit your website.

  • 令我惊讶的是,实际上您可以在AWS Lambda中运行“硒”和无头Chrome。谷歌搜索后,我发现了几篇教程文章[[1](https://github.com/blackboard/lambda-selenium),[2](https://medium.com/@Moatazel​​debsy/ui-testing-using-selenium- webdriver-and-chrome-inside-aws-lambda-77a17ec64862),[3](https://medium.com/clog/running-selenium-and-headless-chrome-on-aws-lambda-fb350458e4df)]。这是您的目标环境吗? (2认同)
  • @Psychozoic有一个仅解决请求的解决方案,但它要求您学习所有端点并做UI正在做的任何请求准备工作(这可能并非易事,例如将CSRF令牌添加到标头等)。使用浏览器自动化更为简单。没有人会在StackOverflow上为您编写CSRF解决方法,因为这将需要打开浏览器并解析其JS和请求标头,以弄清楚如何设置浏览器将为您提供的信息。 (2认同)