c# - Azure Function App 如何与 Azure 资源交互?

标签 c# azure azure-functions

我有一个 C# 函数应用程序,我想使用此应用程序对同一订阅下的 Azure 网络安全组进行自动更新。

该函数应用程序位于 Azure AD 身份验证后面,工作正常,我可以获取用户详细信息。

下一步是以某种方式访问​​ Azure 对象,我见过的所有示例都使用以下命名空间:

using Microsoft.Azure.Management.Fluent;

但是,添加此内容并编译函数应用程序时,我收到错误:

error CS0234: The type or namespace name 'Management' does not exist in the namespace 'Microsoft.Azure' (are you missing an assembly reference?)

我尝试添加引用:

#r "Microsoft.Azure.Management.Fluent"

但是这样做又给了我另一个错误:

    error CS0006: Metadata file 'Microsoft.Azure.Management.Fluent' could not be found

我见过的所有示例都使用如下语法:

var azure = Azure.Configure().WithDefaultSubscription();

但是,他们没有解释函数应用如何能够将 Azure 引用为对象。谁能解释一下吗?

最佳答案

首先,您需要在应用程序中使用相关的 Nuget 包。这里有一些可以帮助您入门,但您可能需要更多,具体取决于您想要进一步执行的操作:

另一个重要的考虑因素是如何进行身份验证

有几种方法..您可以找到很好的详细信息here

  • 通过解决方案中的信用文件(实验性的,可能会发生变化,因此不推荐)

    Azure azure = Azure.Authenticate("my.azureauth").WithDefaultSubscription();
    
  • 通过服务主体注册

    使用客户端 key

    var creds = new AzureCredentialsFactory().FromServicePrincipal(client, key, tenant, AzureEnvironment.AzureGlobalCloud);
    var azure = Azure.Authenticate(creds).WithSubscription(subscriptionId);
    

    使用证书

    var creds = new AzureCredentialsFactory().FromServicePrincipal(client, pfxCertificatePath, password, tenant, AzureEnvironment.AzureGlobalCloud);
    var azure = Azure.Authenticate(creds).WithSubscription(subscriptionId);
    
  • 使用托管服务身份或 MSI

    AzureCredentialsFactory factory = new AzureCredentialsFactory();
    AzureCredentials msiCred = factory.FromMSI(new MSILoginInformation(MSIResourceType.AppService), AzureEnvironment.AzureGlobalCloud);
    var azure = Azure.Configure().WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic).Authenticate(msiCred).WithDefaultSubscription();
    

这里是 Azure Management Libraries for .NET 的根级别文档和代码示例

以下是特定于 Azure Functions 的示例,可能会对您有所帮助:Samples

<小时/>

您提到您希望与 NSG 合作作为您职能的一部分,因此我很快就尝试了其中一个。

我使用了上面解释的第三种方法进行身份验证,首先为我的 Azure Functions 启用 MSI。以下是我必须执行的步骤:

  1. 为我的函数应用启用托管服务身份

    enter image description here

  2. 向我的 Function App 的 MSI 授予使用 NSG 的权限

    enter image description here

这是完整的(快速且肮脏的)工作代码..使用上面提到的 Nuget 包

using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Authentication;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using System.Net.Http;
using System.Threading.Tasks;

namespace RSFunctionCallingFluent
{
    public static class SimpleFunction
    {
        [FunctionName("SimpleFunction")]
        public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req, ILogger log)
        {
            AzureCredentialsFactory factory = new AzureCredentialsFactory();
            AzureCredentials msiCred = factory.FromMSI(new MSILoginInformation(MSIResourceType.AppService), AzureEnvironment.AzureGlobalCloud);
            var azure = Azure.Configure().WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic).Authenticate(msiCred).WithDefaultSubscription();

            var nsg = azure.NetworkSecurityGroups.GetByResourceGroup("TestNSGRG", "RSTestNSG1");

            return (ActionResult)new OkObjectResult(string.Format("NSG {0} found with {1} default security rules", nsg.Name, nsg.DefaultSecurityRules.Count));
        }
    }
}

最终输出

enter image description here

关于c# - Azure Function App 如何与 Azure 资源交互?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55499510/

相关文章:

c# - 如何防止 DateTime 在带有区域性信息的 XML 中序列化?

Azure RemoteApp - 运行 IE9 和 JRE

azure - 如果网站在 AWS EC2 和 Azure VM 上运行,Videojs 会记录不访问摄像头/麦克风或共享屏幕的情况

c# - 如何使用 NSubstitute 假调用当前类的方法?

c# - Nlog不写入Windows服务中的日志文件

c# - Azure WCF 服务无法连接到 SQL Azure

java - 使用 Java AzureResourceManager 进行异步模板部署

azure-functions - 来自 Az Function .net5 中查询字符串的字符串数组输入

powershell - Azure Functions 中 PowerShell 脚本的选项在哪里

c# - TextBox 中的居中占位符文本