我使用默认的 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
我正在尝试使用 cli 获取我的产品的订阅密钥以及默认订阅密钥。我已浏览文档https://learn.microsoft.com/en-us/cli/azure/apim/api?view=azure-cli-latest,但现在我没有看到任何命令来获取我的信息订阅密钥。
\n虽然我可以看到有 powershell 方式来获取它,但我们在 ubuntu 管道中运行任务,并且下面列出的命令在 Linux 代理中不起作用。它说 Set-AzContext 不是已知命令
\n$subscriptionId = "id"\n$RG = "rg"\n$service = "apim-name"\n\nSet-AzContext -Subscription $subscriptionId\xc2\xa0\n\n$apimContext = New-AzApiManagementContext -ResourceGroupName $RG -ServiceName $service\n\nGet-AzApiManagementSubscriptionKey -Context $apimContext -SubscriptionId "master"\nRun Code Online (Sandbox Code Playgroud)\n更新\n我能够通过 DevOps 管道中的 Azure powershell 任务获取详细信息。如果 azure cli 中没有选项,我将使用它作为解决方法。
\nazure azure-api-management azure-cli azure-devops powershell-core
仅当我通过 ARM 模板部署时,当我从门户尝试成功时,我才会遇到此问题。我认为问题可能出在我使用的版本上,所以我更改了模板中的最后 3 个版本( https://learn.microsoft.com/en-us/azure/templates/microsoft.apimanagement/service/apis ),还是同样的问题。有人可以指出我这里出了什么问题吗?我添加了最小复制。
昂首阔步:
{
"openapi": "3.0.1",
"info": {
"title": "Test",
"description": "Test",
"version": "1.0"
},
"paths": {
"/api/v1/test": {
"get": {
"tags": [
"Test"
],
"summary": "Test",
"parameters": [
{
"name": "TestName",
"in": "query",
"schema": {
"minLength": 3,
"type": "string",
"nullable": true
}
}
],
"responses": {
"500": {
"description": "Server Error",
"content": {
"text/plain": {
"schema": {
"$ref": "#/components/schemas/Error"
}
},
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
},
"text/json": {
"schema": {
"$ref": …Run Code Online (Sandbox Code Playgroud) 我正在使用淘汰赛验证.我想显示最小和最大长度的自定义消息.我尝试了一些选项而且我无法实现它.如果有人已经实现它,请分享我如何做到这一点.
这就是我现在正在尝试的.
var viewModel = {
firstName: ko.observable().extend({
minLength: [
3,
'Please enter Valid number']
, maxLength: 10
}),
}
Run Code Online (Sandbox Code Playgroud)
我是否必须真正选择RegEx.
我正在尝试使用az pipelines管道中的 cli 更新变量组中的变量,我创建了一个 PAT 并将其传递到管道,其工作正常。但我使用默认的,例如$(System.AccessToken)它能够列出变量组中的变量,但无法更新变量组。其曰
错误:您无权对变量组执行此操作。变量组管理员应将您添加到管理员角色。##[错误]脚本失败,退出代码:1
经过一番搜索,我发现我需要将 Project Collection Build Service ( name ) 添加为变量组中的管理员,然后重试。我已经添加了这一点,但我仍然遇到同样的错误。有什么建议么?
我使用的是经典管道,这是从管道导出的任务。
steps
- task: AzureCLI@2
displayName: 'Azure CLI '
inputs:
azureSubscription: 'sc'
scriptType: pscore
scriptLocation: inlineScript
inlineScript: |
az extension add --name azure-devops
az pipelines variable-group variable list --group-id id --org "orgname" --project "projectname"
az pipelines variable-group variable update --group-id id --name apim-service-name --value $(str_tf_module_containername) --org "orgname" --project "projectname"
env:
AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)
Run Code Online (Sandbox Code Playgroud) azure azure-cli azure-devops azure-pipelines azure-pipelines-yaml
azure ×3
azure-devops ×3
azure-cli ×2
c# ×1
knockout.js ×1
openapi ×1
range ×1
unit-testing ×1