azure - 如何设置 Azure AD 和 Cypress 以便 SSO 登录工作

标签 azure azure-active-directory cypress

我尝试使用 cypress 进行自动化测试。 为了使应用程序正常工作,用户需要登录。 所以我尝试自动化这一步。

我编写了以下命令,并在其中传递了用户名密码。

Cypress.Commands.add('login', ( userId, pwd ) => { 
    const domain = 'XXXX'
    const clientId = 'XXXX'
    const clientSecret = 'XXXX'
    const scope = 'XXXX'
    cy.request({
        method:'POST',
        url:`https://login.microsoftonline.com/${domain}/oauth2/v2.0/token`,
        header:
        {
           'cache-control': 'no-cache',
           'Content-Type': 'application/x-www-form-urlencoded'
        },
        form: true,
        body: {
           grant_type: 'password',
           client_id: clientId,
           client_secret: clientSecret,
           password: pwd,
           scope: scope,
           username: userId
        }
 })
 .then((resp)=>{
     const token = resp.body['access_token']
     console.log(token)
     //cy.visit('http://{URL}/auth/#access_token='+token)
 })
})

这样我就得到了错误消息。用户名或密码错误。

有人知道如何解决这个问题吗?或者知道使用 azure AD 的 SSO 设置 Cypress 的更好方法吗?

最佳答案

Azure AD ROPC flow应该可以,使用时请确保您使用的工作帐户格式为 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9de5e5e5dde5e5e5b3f2f3f0f4feeff2eef2fbe9b3fef2f0" rel="noreferrer noopener nofollow">[email protected]</a> ,并且未启用 MFA,详细信息请参见下面的屏幕截图。如果您没有这样的账户,请关注this创建一个。

enter image description here

根据您的情况,请仔细检查用户名和密码,确保其正确。(如果正确,请确保实际请求中不存在转义问题。)

我在 postman 中测试了流程,工作正常。

enter image description here

关于azure - 如何设置 Azure AD 和 Cypress 以便 SSO 登录工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63775475/

相关文章:

azure - 将 Windows Azure AD 与 Windows ACS 集成

azure - 如何将多域用户电子邮件添加到 Azure AD

Azure WebApp SNAT 耗尽 - 私有(private)终结点能否改进

node.js - 我可以仅使用 Dialogfow 的默认履行 Webhook 代码作为 Azure 上的起点吗?

azure - 用户关闭并重新打开浏览器后,需要使用 Azure AD B2C 重新进行身份验证

cypress - 使用 cypress 提示登录

sql-server - 将 Guid 主键和聚集索引迁移到 INT (Azure SQL)

Azure Function 访问私有(private)资源

cypress - 自定义事件和 cy.get

Cucumber *.feature 和 cypress *.cy.ts 在同一个存储库中运行