简体   繁体   English

将 loginWithUsernamePassword 迁移到 @azure/identity

[英]Migrate loginWithUsernamePassword to @azure/identity

I migrate some code from @azure/ms-node-auth to @azure/identity which authenticates against Azure Active Directory (AAD) via loginWithUsernamePassword .我将一些代码从@azure/ms-node-auth迁移到@azure/identity ,它通过loginWithUsernamePassword对 Azure Active Directory (AAD) 进行身份验证。 The migration guide points to UsernamePasswordCredential class, but it denies my request. 迁移指南指向UsernamePasswordCredential class,但它拒绝了我的请求。

// Working @azure/ms-node-auth snippet
async function getTokenLegacy(): Promise<string> {
  const credentials = await msRestNodeAuth.loginWithUsernamePassword(
    USERNAME,
    PASSWORD, {
      domain: AAD_TENANT_ID,
      clientId: CUSTOM_CLIENT_ID,
      tokenAudience: CUSTOM_APP_ID,
    },
  );
  return (await credentials.getToken()).accessToken;
}

// Non-working migrated @azure/identity version
async function getTokenMigrated(): Promise<string> {
  const credentials = new UsernamePasswordCredential(
    AAD_TENANT_ID, 
    CUSTOM_CLIENT_ID, 
    USERNAME, 
    PASSWORD);
  return (await credentials.getToken(CUSTOM_APP_ID)).token;
}

The following error occurs.发生以下错误。

AADSTS50126: Error validating credentials due to invalid username or password.

The user has a federated account and @azure/ms-rest-nodeauth verifies the credentials via the SAML protocol.用户有一个federated帐户, @azure/ms-rest-nodeauth通过SAML协议验证凭据。

UserRealm: VERBOSE: UserRealm response:
UserRealm: VERBOSE:  AccountType:             federated
UserRealm: VERBOSE:  FederationProtocol:      wstrust
TokenRequest: VERBOSE: Acquiring token with username password for federated user
...
WSTrustResponse: INFO: Found token of type: urn:oasis:names:tc:SAML:1.0:assertion
TokenRequest: VERBOSE: Performing OAuth assertion grant type exchange.
  • Please try resetting the password, otherwise you can use the below workaround请尝试重置密码,否则您可以使用以下解决方法

  • As you are using federated authentication you will get redirected to the federated identity for verification.当您使用联合身份验证时,您将被重定向联合身份进行验证。 When you are using ropc flow by passing the username and password the redirection does not happen and gives an error of invalid username or password .当您通过传递用户名和密码来使用ropc流时,不会发生重定向,并给出invalid username or password的错误。

In order to make this work you need to disable federated authentication and use manage authentication from AAD site so that no redirection is required.为了完成这项工作,您需要禁用联合身份验证并使用AAD 站点的管理身份验证,这样就不需要重定向。 So you will need to create a user from AAD site.因此,您需要从 AAD 站点创建用户。

For more information please refer this MS Q&A answer suggested by @amanpreetsingh-msft & @MarileeTurscak-MSFT.有关更多信息,请参阅@amanpreetsingh-msft 和 @MarileeTurscak-MSFT 建议的MS Q&A答案。

Still, if you want to use federated authentication you can refer this blog .不过,如果您想使用联合身份验证,您可以参考此博客

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

相关问题 无法使用Azure上的Knex迁移生产数据库 - Unable to migrate production database with Knex on Azure 将用户从 Firebase Auth 复制/迁移到 Google Identity Platform - Copy / migrate users from Firebase Auth to Google Identity Platform Azure Sql 连接使用来自 Azure 的访问令牌与 nodejs 的身份 - Azure Sql connection using access token from Azure Identity with nodejs 如何使用 node-pg-migrate 定义“INT GENERATED ALWAYS AS IDENTITY”列? - How to define "INT GENERATED ALWAYS AS IDENTITY" column with node-pg-migrate? 如何在节点中使用 Azure 托管服务标识访问 Key Vault? - How to access Key Vault with Azure Managed Service Identity in node? 如何使用 node.js 中的 azure/identity 模块进行身份验证? - How do I authenticate with azure/identity module in node.js? 从身份提供商和Azure EasyAPI获取用户信息 - Getting User information from identity provider and Azure EasyAPI 将Azure移动服务表脚本(Blob上传)迁移到Azure App Service - Migrate azure mobile service table script (Blob upload) to azure app service Azure 虚拟下游设备的物联网边缘模块身份转换 - Azure IoT Edge Module Identity Translation for virtual Downstream Devices 为什么在使用 @azure/keyvault-keys 和 @azure/identity 时需要租户,但在使用 azure-keyvault 时不需要? - Why do I need a tenant when using @azure/keyvault-keys with @azure/identity, but not when using azure-keyvault?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM