[Azure] C# 透過 Microsoft.Azure.Management.Logic v4 來控制已經建立的 Azure Logic App - 開關 Azure Logic App 服務

2021-01-20

前幾天寫了一些關於 Azure Logic App 的文章,之前我也寫過裡用 C# 去做到控制 Azure 上面的服務,我想說應該 Azure Logic App 應該也是用一樣的 Microsoft.Azure.Management.Fluent 來做到,但是我找一找之後發現 竟然是用 Microsoft.Azure.Management.Logic ,再度刷新我三觀..

前置作業,你得先安裝幾個 library : Microsoft.Azure.Management.Logic  , Microsoft.Azure.Management.ComputeMicrosoft.IdentityModel.Clients.ActiveDirectory

Step1. 請參考這篇文章 ( [Azure][C#] 產生有權限的憑證控制,透過 C# 控制 Azure 上面的服務 ) 拿到你需要的資訊(憑證) , 分別有 appId , password , tenant 這些都是後面步驟需要的。

Step2. 接下來你要處理的就是  Microsoft.Azure.Management.Logic.LogicManagementClient,這東西我原本以為很簡單 但是你看他 MSDN : https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.management.logic.logicmanagementclient?view=azure-dotnet ,根本就沒有資訊阿,後來網路上看到一些神人 之後我改寫一些 code ,這樣你就可以溫馨的拿到 Ctor 需要的  Microsoft.Rest.ServiceClientCredentials

public class CustomLoginCredentials : ServiceClientCredentials { private string AuthenticationToken { get; set; } private string ClientId { get; set; } private string ClientSecret { get; set; } private string Tenant { get; set; } public CustomLoginCredentials(string appId, string password, string tenant) { ClientId = appId; ClientSecret = password; Tenant = tenant; } public override void InitializeServiceClient<T>(ServiceClient<T> client) { var authenticationContext = new AuthenticationContext("https://login.windows.net/"+ Tenant); var credential = new ClientCredential(clientId: ClientId, clientSecret: ClientSecret); var result = authenticationContext.AcquireTokenAsync(resource: "https://management.core.windows.net/", clientCredential: credential).Result; if (result == null) throw new InvalidOperationException("Failed to obtain the JWT token"); AuthenticationToken = result.AccessToken; } public override async Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken) { if (request == null) throw new ArgumentNullException("request"); if (AuthenticationToken == null) throw new InvalidOperationException("Token Provider Cannot Be Null"); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", AuthenticationToken); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); await base.ProcessHttpRequestAsync(request, cancellationToken); } }


這裡面  ClientId = appId , ClientSecret = password

Step3. 上一個步驟是重點,之後我們就是呼叫的部分,這邊有一點要注意的是 這裡面要自己補上 SubscriptionId 不然一定會錯。

/* // https://blog.no2don.com/2020/09/azurec-c-azure.html { "appId": "....", "displayName": "...", "name": "...", "password": "...", "tenant": "ab194d5d-84d4-4ba0-8f8f-ba7182d7e4d7" } */ var customLoginCredentials = new CustomLoginCredentials(appId,password,tenant); var logicAppsHelper = new Microsoft.Azure.Management.Logic.LogicManagementClient(customLoginCredentials); logicAppsHelper.SubscriptionId = "your_subscriptionid"; var wfs = logicAppsHelper.Workflows; //停用 Azure Logic Apps //var resDisableWF = wfs.DisableWithHttpMessagesAsync("SPINUS", "LOGICAPP").Result; //開啟 Azure Logic Apps var resRunWF = wfs.EnableWithHttpMessagesAsync("SPINUS", "LOGICAPP").Result;


Result:


心得,這東西我研究了一小段時間,因為其實很多地方名詞對應不一樣,思考邏輯也不一樣,重點是文件很少又是一個獨立的 library ,只是因為我們公司有一些 DevOps   的需求,所以研究了一下,希望可以幫到後面的人避免採坑..

reference :

https://stackoverflow.com/questions/35228042/how-to-create-serviceclientcredential-to-be-used-with-microsoft-azure-management


當麻許的碎念筆記 2014 | Donma Hsu Design.