我创建了 2 个 Azure 函数应用程序,都设置了身份验证/授权,因此为两者创建了一个 AD 应用程序。我想使用 MSI 设置从一个功能到另一个功能的 AD 身份验证。我使用 ARM 模板设置了带有托管服务标识的客户端功能。我创建了一个简单的测试函数来获取访问令牌,它返回:Microsoft.Azure.Services.AppAuthentication: 令牌响应不是预期的格式。
try {
var azureServiceTokenProvider = new AzureServiceTokenProvider();
string accessToken = await azureServiceTokenProvider.GetAccessTokenAsync("https://myapp-registration-westus-dev.azurewebsites.net/");
log.Info($"Access Token: {accessToken}");
return req.CreateResponse(new {token = accessToken});
}
catch(Exception ex) {
log.Error("Error", ex);
throw;
}
Run Code Online (Sandbox Code Playgroud) 在启用的逻辑应用上部署具有托管标识访问策略的 KeyVault 服务时,它会失败,因为它尚不存在。我确实为逻辑应用程序添加了依赖项。
奇怪的是,这个模板已经工作了数周,现在每次都失败,所以我有点困惑。我从 MS 的快速入门模板复制了这个。但这不是问题,因为如果您查看错误,它会指向正确的目标资源。如果我在失败后单击重新部署,这个模板也可以工作,因为当时托管标识已经存在。我测试了它,无论如何它失败了。
这是我的 ARM 模板
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"logicAppName": {
"type": "string",
"minLength": 1,
"metadata": {
"description": "Describes the name of the Logic App resource"
},
"defaultValue": "demo"
},
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]",
"metadata": {
"description": "Specifies the Azure location where the key vault should be created."
}
}
},
"variables": {
"keyVaultName": "[concat('eakeyvault', uniquestring(resourceGroup().id))]",
"logicAppName": "[parameters('logicAppName')]"
},
"resources": [
{
"type": "Microsoft.KeyVault/vaults",
"name": "[variables('keyVaultName')]",
"apiVersion": "2018-02-14",
"location": "[parameters('location')]",
"dependsOn": …Run Code Online (Sandbox Code Playgroud) azure azure-resource-manager azure-rm-template azure-managed-identity
我有一个 Azure Function 应用程序,它使用通过 Azure 门户配置的集成 Azure AD 身份验证。
通过“快速”模式设置,这将创建 Azure 应用程序注册以及企业应用程序。
默认情况下,此企业应用程序接受所有用户。因此,这意味着租户中的所有用户都可以触发受保护的 Azure Functions,这不是我想要的。
我的目标是允许租户中具有托管标识(例如应用服务)的某些用户和某些 Azure 资源触发该功能。
因此,我转到 Azure Function 应用程序的企业应用程序设置,将其属性更改为“需要用户分配”。然后在“用户/组”下,我可以添加允许进行身份验证的用户/组。
这里我发现我只能添加普通AAD用户。托管身份服务主体(即系统为我的应用服务分配的托管身份主体)未显示在列表中。
我还没有尝试过用户分配的托管身份。但我更喜欢使用系统分配的托管身份。
这是受支持的场景吗?
azure-active-directory azure-functions azure-authentication azure-managed-identity
我使用默认的 azure 凭据方法获取访问令牌,同时使用函数应用程序的托管标识获取访问令牌。我能够获取令牌。但现在我不确定如何对该方法进行单元测试。这是当前状态
private async Task RefreshTokenCache()
{
var tokenCredential = new DefaultAzureCredential();
var accessToken = await tokenCredential.GetTokenAsync(
new TokenRequestContext(scopes: new string[] { _config[AppConstants.ADAPIAppId] + "/.default" }) { });
accessTokenCache.Set(AppConstants.AccessToken, accessToken.Token, DateTimeOffset.Now.AddMinutes(55));
}
Run Code Online (Sandbox Code Playgroud)
是否有像 httpclientfactory 这样的东西,我可以在其中注入或传递一些连接字符串,以便我告诉 DefaultAzureCredential 不要调用 Azure?
更新
我正在添加更多上下文。我使用它从 azure 获取函数应用程序中的访问令牌,以向 APIM 进行自身身份验证。所以我使用 HttpMessageHandler 来检查令牌是否不存在,调用 Azure 并获取令牌。
在功能应用程序中启动。
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddHttpClient(AppConstants.ReplyService)
//Adding token handler middleware to add authentication related details.
.AddHttpMessageHandler<AccessTokenHandler>();
}
Run Code Online (Sandbox Code Playgroud)
处理程序文件:
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
// Use …Run Code Online (Sandbox Code Playgroud) c# unit-testing azure azure-managed-identity defaultazurecredential
我正在尝试在 Azure Pipelines 中运行Azure 资源组部署任务。我已在运行 Windows 的 Azure VM 上部署了 Azure Pipelines 自托管代理,并且在我的 Azure DevOps 组织中,我已使用托管服务标识设置了与 VM 的 Azure 资源管理器服务连接。
但是,当我尝试使用托管标识的服务连接配置 Azure 资源组部署任务时,出现以下错误:
GetUserAccessToken: 获取identity的访问令牌失败。AAD 返回无声失败。
截屏:
我已经验证我已向目标资源组授予了对 VM 托管身份的访问权限(贡献者):
服务连接的范围也仅限于 Azure 订阅:
感谢您对诊断此问题的任何帮助。谢谢!
azure azure-resource-manager azure-devops azure-pipelines azure-managed-identity
我正在尝试授予应用程序访问数据库的权限。其中一个步骤要求必须在数据库上运行创建用户的脚本。我通过 azureSqlAzureDacpacDeployment@1 任务的管道执行此操作。
使用托管标识保护来自应用服务的 Azure SQL 数据库连接
- task: SqlAzureDacpacDeployment@1
inputs:
azureSubscription: 'xxxxxxxx (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)'
AuthenticationType: 'aadAuthenticationIntegrated'
ServerName: '$(SqlServerName)'
DatabaseName: '$(SqlDatabaseName)'
deployType: 'InlineSqlTask'
SqlInline: |
CREATE USER [$(AppName)] FROM EXTERNAL PROVIDER;
ALTER ROLE db_datareader ADD MEMBER [$(AppName)];
ALTER ROLE db_datawriter ADD MEMBER [$(AppName)];
GO
InlineAdditionalArguments: '-v $(ApiAppName)'
IpDetectionMethod: 'AutoDetect'
ApiAppName = 'AppName=MyApplication'
##[error]The format used to define the new variable for Invoke-Sqlcmd cmdlet is invalid.
Please use the 'var=value' format for defining a new variable.Check out how to troubleshoot
failures at …Run Code Online (Sandbox Code Playgroud) azure azure-sql-database azure-devops azure-pipelines azure-managed-identity
设想
您好,我想创建Logic App一个从Azure KeyVault保管库获取秘密并向 API 发送经过身份验证的请求的方法。
问题
我收到:The workflow connection parameter 'keyvault' is not valid. The API connection 'keyvault' is not configured to support managed identity.在 ARM 部署期间。如何Microsoft.Web/Connections使用 ARM 模板中的托管身份进行创建。文档中没有有关它的信息:apiConnectionlogicapp -MSI
重现
{
"type": "Microsoft.Web/connections",
"apiVersion": "2016-06-01",
"name": "[variables('KeyVault_Connection_Name')]",
"location": "[variables('location')]",
"kind": "V1",
"properties": {
"api": {
"id": "[concat('/subscriptions/', variables('subscriptionId'), '/providers/Microsoft.Web/locations/', variables('location'), '/managedApis/', 'keyvault')]"
},
"parameterValues": {
"vaultName": "[variables('keyVaultName')]"
},
"displayName": "[variables('KeyVault_Display_Connection_Name')]"
}
},
{
"type": "Microsoft.Logic/workflows",
"apiVersion": "2017-07-01",
"name": "[variables('logicAppName')]", …Run Code Online (Sandbox Code Playgroud) azure-resource-manager azure-logic-apps azure-keyvault azure-managed-identity
我的目标是部署一个具有系统管理标识和该标识的角色分配的逻辑应用。优选地,这在一个 ARM 模板中完成。
我的设置第一次运行失败,但连续运行成功。如果我错了,请纠正我,但我认为其原因是角色分配的部署发生在逻辑应用的托管标识“准备好”之前,因此我第一次部署时收到以下错误模板。第二次部署模板时,我没有收到此错误,可能是因为当时身份已经存在。
{
"code": "DeploymentFailed",
"message": "At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/DeployOperations for usage details.",
"details": [
{
"code": "PrincipalNotFound",
"message": "Principal *** does not exist in the directory ***."
}
]
}
Run Code Online (Sandbox Code Playgroud)
我的模板(为简洁起见删除了逻辑应用定义)。在这种情况下,逻辑应用的标识需要访问位于另一个资源组中的存储帐户。
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"logicAppName": {
"type": "string"
},
"storageAccountResourceGroup": {
"type": "String"
},
"storageAccountName": {
"type": "String"
}
},
"variables": {
"logicAppResourceId": "[resourceId('Microsoft.Logic/workflows', parameters('logicAppName'))]",
"Storage Blob Data Contributor": …Run Code Online (Sandbox Code Playgroud) User Assigned Managed Identity我正在尝试在我们的一个应用程序中使用 a 。我还了解了 之间的差异System Assigned Managed Identity and User Assigned Managed Identity。
我很清楚,aSystem Assigned Managed Identity不能在本地使用,因为您要为 Azure 资源分配身份。
不过我不清楚 a 是否User Assigned Managed Identity可以在本地使用。我唯一能找到的是以下内容:
在我的场景中,我想从 Azure Key Vault 读取一些机密。我创建了一个用户分配的托管身份,并在 Key Vault 上配置了访问策略,以向该身份授予必要的权限。考虑到我使用此身份来访问 Azure Key Vault(这是一种 Azure 资源),我的期望是,无论我的代码在何处运行(使用 JetBrains Rider 作为我的 IDE),它都应该可以工作。
但是,当我尝试做类似的事情时:
var managedIdentityCredential = new ManagedIdentityCredential("managed-identity-id");
SecretClient secretClient = new(new Uri("https://mykeyvault.vault.azure.net/"), managedIdentityCredential);
KeyVaultSecret secret = await secretClient.GetSecretAsync(key);
Run Code Online (Sandbox Code Playgroud)
当我在本地运行代码时,收到错误Azure.Identity.CredentialUnavailableException消息:ManagedIdentityCredential authentication unavailable. No Managed Identity endpoint found
Azure.Identity.CredentialUnavailableException: …Run Code Online (Sandbox Code Playgroud) 提前致谢