简体   繁体   English

在 (Azure) AD 帐户下运行的服务能否透明地获取 OAuth2 令牌?

[英]Can OAuth2 tokens be obtained transparently by a service running under an (Azure) AD account?

Apologies if this is actually a naive question.如果这实际上是一个天真的问题,我们深表歉意。 I've searched on the topic but can only find two choices: use a client app registration per client, or a user-password flow that requires a browser window.我搜索了该主题,但只能找到两个选择:每个客户端使用客户端应用程序注册,或者需要浏览器 window 的用户密码流程。

Scenario: develop a REST API hosted in IIS, protect it with token authorization via an app registration in Azure AD.场景:开发一个 REST API 托管在 IIS 中,通过在 Azure AD 中的应用程序注册使用令牌授权保护它。 Textbook solution says you also create a client app registration in Azure AD, and callers then use the client app's client ID & secret to obtain tokens from login.microsoftonline.com.教科书解决方案说您还在 Azure AD 中创建客户端应用程序注册,然后调用者使用客户端应用程序的客户端 ID 和密码从 login.microsoftonline.com 获取令牌。 MSAL code in the API takes care of validation for you. API 中的 MSAL 代码负责为您进行验证。 So far so good.到目前为止,一切都很好。

This means creating a client app registration per potential caller though, especially if you want to authorize for different privileges in the API.这意味着为每个潜在调用者创建一个客户端应用程序注册,特别是如果你想在 API 中授权不同的权限。

My question is: is it possible to have calling applications that run as Windows services say, under domain accounts that are synced to Azure AD, and have those domain accounts added as 'users' in the API app registration?我的问题是:是否有可能在同步到 Azure AD 的域帐户下调用作为 Windows 服务运行的应用程序,并在 API 应用程序注册中将这些域帐户添加为“用户”? Can the calling application obtain tokens from login.microsoftonline.com transparently, in an NTLM-like way?调用应用程序能否以类似 NTLM 的方式透明地从 login.microsoftonline.com 获取令牌? Or is it impossible without actually storing either a client secret or the domain account's username/password in the client service's app.config?或者如果不在客户端服务的 app.config 中实际存储客户端密码或域帐户的用户名/密码是不可能的?

Yes but the token is an AzureAD token.是,但令牌是 AzureAD 令牌。 Not an NTLM or domain account.不是 NTLM 或域帐户。 The thing that makes this behave the way you want is AzureAD sync to AD.使它按照您想要的方式运行的是 AzureAD 同步到 AD。

Main library documentation: https://learn.microsoft.com/en-us/do.net/api/microsoft.identity.client?view=azure-do.net主库文档: https://learn.microsoft.com/en-us/do.net/api/microsoft.identity.client?view=azure-do.net

To pull the token for the user account from the OS you need to use a technology called "WAM".要从操作系统中提取用户帐户的令牌,您需要使用一种称为“WAM”的技术。 Simply add some broker options in your PublicClientApplicationBuilder to enable WAM.只需在您的PublicClientApplicationBuilder中添加一些代理选项即可启用 WAM。

EXAMPLE:例子:

_clientApp = PublicClientApplicationBuilder.Create(_nativeClientAppID)
    // Broker pulls the AAD account from the OS.
    .WithBroker(true)
    .WithWindowsBrokerOptions(new WindowsBrokerOptions()
    {
        // GetAccounts will return Work and School accounts from Windows.
        ListWindowsWorkAndSchoolAccounts = true,

        // Legacy support for 1st party apps only.
        MsaPassthrough = true
    }).Build();

For a service account you may need to log on locally with the service account and authenticate to AzureAD so the OS has the token available for your code to pull from.对于服务帐户,您可能需要使用该服务帐户在本地登录并向 AzureAD 进行身份验证,以便操作系统拥有可供您的代码提取的令牌。

NOTE: When AcquireTokenSilent fails you need to be able to AcquireTokenInteractive and a service account has no ability to do so.注意:当AcquireTokenSilent失败时,您需要能够AcquireTokenInteractive并且服务帐户无法这样做。 This means that you need to ensure that the token for the service account never expires.这意味着您需要确保服务帐户的令牌永不过期。 This is a reduction in security to enable your desired workflow.这会降低安全性以启用您所需的工作流程。

Details on WAM: https://learn.microsoft.com/en-us/azure/active-directory/develop/scenario-desktop-acquire-token-wam WAM 详情: https://learn.microsoft.com/en-us/azure/active-directory/develop/scenario-desktop-acquire-token-wam

Finally, your code should have some way of notifying you if it fails to get the token.最后,如果您的代码未能获得令牌,您的代码应该有某种方式通知您。 This will alert you to the need to enter credentials for the service.这将提醒您需要输入该服务的凭据。 I would make an interactive mode, maybe invoked by a -login command line argument, that allows you to provide credentials to the service.我会创建一个交互模式,可能由-login命令行参数调用,允许您向服务提供凭据。

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

相关问题 Azure AD 在交换访问令牌的 OAuth2 代码时返回非 JWT 令牌 - Azure AD returns non JWT Tokens when exchanging the OAuth2 code for an access token 似乎无法使用服务帐户在 python 中模拟用户或无法通过 google oauth2 获取令牌 - Can't seem to impersonate user or not getting token with google oauth2 in python using a service account Golang Oauth2 服务帐户返回空刷新令牌字符串 - Golang Oauth2 Service account returns empty refresh token string Azure 解析 OAuth2 回调时出现 AD 错误:invalid_client - Azure AD error while parsing OAuth2 callback: invalid_client 如何配置 Azure AD Oauth2 在登录时返回所有用户组 - How to configure Azure AD Oauth2 to return all user groups on login 如何在运行应用程序作为 windows 服务时登录到 azure 帐户? - How to login to azure account while running the application as windows service? Django 应用程序与 Azure AD 帐户身份提供商部署到 Azure Web 应用程序服务失败 - Django app with Azure AD account identity provider deployment to Azure Web app service fails 如何创建 OAuth2 不透明(参考)访问令牌 - How to create OAuth2 Opaque(Reference) Access Tokens WSO2 - 之前发布的 oAuth2 令牌即使没有过期也无效 - WSO2 - Previously issued oAuth2 tokens invalid even if not expired 如何使用主目录中的服务主体管理 Azure AD B2C - How can I manage Azure AD B2C with a service principal from the main directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM