简体   繁体   English

是否应将托管服务标识用于从Console App进行Azure App Service访问

[英]Should Managed Service Identities be used for Azure App Service access from Console App

I have a console app that is running inside our enterprise that needs to access as App Service Web API. 我有一个在我们的企业内运行的控制台应用程序,需要作为App Service Web API访问。 What is the best way to handle authentication. 处理身份验证的最佳方法是什么。 I tried registering the App with AD, but it still seems like it cant't see the App Service. 我尝试用AD注册应用程序,但似乎仍然看不到App Service。 I tried the following code, but I am not sure this is even the right API to use. 我尝试了以下代码,但我不确定这是否是正确的API。

var App = ConfidentialClientApplicationBuilder.Create(CoreConstants.Auth_ClientId)
.WithAuthority(CoreConstants.Auth_Authority)
                .WithClientSecret("xxxxxxxxxxxxxx")
                .Build();

var token =  App.AcquireTokenForClient(scopes).ExecuteAsync();
token.Wait();

This fails saying the scope is not defined. 这无法说明范围未定义。 It looks like it is in Azure. 它看起来像是在Azure中。

First of all, you need to create role assignments for your App identity. 首先,您需要为App身份创建角色分配。 And then you can get the access tokens from the identity. 然后,您可以从身份获取访问令牌。 The code will like this: 代码将是这样的:

using Microsoft.Azure.Services.AppAuthentication;
using Microsoft.Azure.KeyVault;
// ...
var azureServiceTokenProvider = new AzureServiceTokenProvider();
string accessToken = await azureServiceTokenProvider.GetAccessTokenAsync("https://vault.azure.net");
// OR
var kv = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));

Do not forget to add references to the Microsoft.Azure.Services.AppAuthentication and any other necessary NuGet packages to your application. 不要忘记将Microsoft.Azure.Services.AppAuthentication和任何其他必需的NuGet包的引用添加到您的应用程序。 For more details, see Obtaining tokens for Azure resources with App MSI . 有关更多详细信息,请参阅使用App MSI获取Azure资源的令牌

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

相关问题 如何使用 Service Principal/Managed Identity 访问 Azure App Configuration? - How to use Service Principal/Managed Identity to access Azure App Configuration? 从 Azure Web 应用托管服务标识创建资源组 - Creating resource groups from Azure Web App Managed Service Identity 使用托管标识从 Azure 应用服务调用图 - Call Graph from Azure App Service using Managed Identity 从 Azure 应用服务访问 Blob 存储 - Blob storage access from Azure App Service 从另一个应用程序服务访问应用程序服务 - Access App Service from another App Service 从 Azure 应用服务连接到 Azure Sql 数据库适用于系统分配但不是用户分配的托管标识 - Connecting from Azure App Service to Azure Sql Database works for System Assigned but not User Assigned managed identity 从 Windows 服务启动的控制台应用程序没有文件系统访问权限 - Console App launched from windows service has no file system access 具有用户分配的托管标识的 Azure 应用服务使应用程序崩溃 - Azure App Service with User-Assigned Managed Identity crashes application Azure 应用服务可以从 github 存储库访问文件吗? - Can Azure app service access files from github repository? 将 Azure 托管标识用于未授权新 SDK 的应用服务 - Using Azure managed identity for App Service not authorising for new SDK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM