From f1d1d4adc09b3c30af501b93a992685c38b1f6cc Mon Sep 17 00:00:00 2001 From: www159 <2107795244@qq.com> Date: Mon, 4 Jul 2022 22:47:15 +0800 Subject: [PATCH] ' --- .../azure_m.Android/BrowserActivity.cs | 28 ++++++++++++++++ .../azure_m/azure_m/Services/AzureQuery.cs | 29 +++++++++++----- .../azure_m/Views/AddVMDetailsPage~.xaml | 33 ------------------- .../azure_m/Views/AddVMDetailsPage~.xaml.cs | 26 --------------- .../azure_m/azure_m/Views/LoginPage.xaml.cs | 2 +- azure_m/azure_m/azure_m/azure_m.csproj | 6 ---- 6 files changed, 50 insertions(+), 74 deletions(-) create mode 100644 azure_m/azure_m/azure_m.Android/BrowserActivity.cs delete mode 100644 azure_m/azure_m/azure_m/Views/AddVMDetailsPage~.xaml delete mode 100644 azure_m/azure_m/azure_m/Views/AddVMDetailsPage~.xaml.cs diff --git a/azure_m/azure_m/azure_m.Android/BrowserActivity.cs b/azure_m/azure_m/azure_m.Android/BrowserActivity.cs new file mode 100644 index 00000000..ff6a4c27 --- /dev/null +++ b/azure_m/azure_m/azure_m.Android/BrowserActivity.cs @@ -0,0 +1,28 @@ +using Android.App; +using Android.Content; +using Android.OS; +using Android.Runtime; +using Android.Views; +using Android.Widget; +using Microsoft.Identity.Client; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace azure_m.Droid +{ + [Activity] + [IntentFilter(new[] { Intent.ActionView }, + Categories = new[] { Intent.CategoryBrowsable, Intent.CategoryDefault }, + DataHost = "localhost", + DataScheme = "http")] + public class BrowserActivity : BrowserTabActivity + { + //protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data) + //{ + // base.OnActivityResult(requestCode, resultCode, data); + // AuthenticationContinuationHelper.SetAuthenticationContinuationEventArgs(requestCode, resultCode, data); + //} + } +} \ No newline at end of file diff --git a/azure_m/azure_m/azure_m/Services/AzureQuery.cs b/azure_m/azure_m/azure_m/Services/AzureQuery.cs index 894fb947..786c5d42 100644 --- a/azure_m/azure_m/azure_m/Services/AzureQuery.cs +++ b/azure_m/azure_m/azure_m/Services/AzureQuery.cs @@ -19,6 +19,7 @@ namespace azure_m.Services { private static string token { get; set; } private static string baseUri { get; set; } = "https://management.azure.com/"; + private static string subscriptionSql { get; set; } = "resourcecontainers\n | where type == \"microsoft.resources/subscriptions\"\n | join kind=leftouter (securityresources \n | where type == \"microsoft.security/securescores\"\n | where properties.environment == \"Azure\" and properties.displayName == \"ASC score\"\n ) on subscriptionId\n | extend secureScore=properties1.score.percentage,\n managementGroup=properties.managementGroupAncestorsChain,\n subscriptionName=name,\n status=properties.state\n | project id, subscriptionId, subscriptionName, status, managementGroup, secureScore"; public static string subscriptionId { get; set; } = "219b2431-594f-47fa-8e85-664196aa3f92"; public static string baseStrUrl { @@ -51,7 +52,9 @@ namespace azure_m.Services try { - authResult = await app.AcquireTokenSilent(scopes, accounts.FirstOrDefault()).ExecuteAsync(); + authResult = await app + .AcquireTokenSilent(scopes, accounts.FirstOrDefault()) + .ExecuteAsync(); } catch (MsalUiRequiredException ex) { @@ -79,24 +82,34 @@ namespace azure_m.Services token = authResult.AccessToken; baseRequest = new Url(baseStrUrl) .WithOAuthBearerToken(token); + + await getSubscriptionsAsync(token); } } - public static void setSubscriptions(string subscriptionId) - { - baseRequest = baseRequest.AppendPathSegment(subscriptionId); - } + //public static void setSubscriptions(string subscriptionId) + //{ + // baseRequest = baseRequest.AppendPathSegment(subscriptionId); + //} - public static async Task getSubscriptionsAsync() + private static async Task getSubscriptionsAsync(string token) { //https://management.azure.com/providers/Microsoft.ResourceGraph/resources?api-version=2021-03-01 var url = new Url(baseUri) .AppendPathSegments(new[] { "providers", "Microsoft.ResourceGraph", - "resurces" + "resources" }); - var res = await Utils.withApiVersion(url, "2021-03-01").GetJsonAsync(); + + var res = await Utils + .withApiVersion(url, "2021-03-01") + .WithOAuthBearerToken(token) + .PostJsonAsync(new + { + query = subscriptionSql, + }) + .ReceiveJson(); subscriptionId = res.data[0].subscriptionId; } diff --git a/azure_m/azure_m/azure_m/Views/AddVMDetailsPage~.xaml b/azure_m/azure_m/azure_m/Views/AddVMDetailsPage~.xaml deleted file mode 100644 index 681bc2d1..00000000 --- a/azure_m/azure_m/azure_m/Views/AddVMDetailsPage~.xaml +++ /dev/null @@ -1,33 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/azure_m/azure_m/azure_m/Views/AddVMDetailsPage~.xaml.cs b/azure_m/azure_m/azure_m/Views/AddVMDetailsPage~.xaml.cs deleted file mode 100644 index be971044..00000000 --- a/azure_m/azure_m/azure_m/Views/AddVMDetailsPage~.xaml.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -using Xamarin.Forms; -using Xamarin.Forms.Xaml; -using azure_m.ViewModels; -using azure_m.Views; - -namespace azure_m.Views -{ - [XamlCompilation(XamlCompilationOptions.Compile)] - public partial class AddVMDetailsPage : ContentPage - { - - public AddVMDetailsPage() - { - InitializeComponent(); - - } - - - } -} \ No newline at end of file diff --git a/azure_m/azure_m/azure_m/Views/LoginPage.xaml.cs b/azure_m/azure_m/azure_m/Views/LoginPage.xaml.cs index 3dd41648..0c1315db 100644 --- a/azure_m/azure_m/azure_m/Views/LoginPage.xaml.cs +++ b/azure_m/azure_m/azure_m/Views/LoginPage.xaml.cs @@ -26,7 +26,7 @@ namespace azure_m.Views { try { - //await QueryInfo.getTokenAsync(); + await QueryInfo.getTokenAsync(); LoginComplete?.Invoke(this, EventArgs.Empty); } catch(Exception ex) diff --git a/azure_m/azure_m/azure_m/azure_m.csproj b/azure_m/azure_m/azure_m/azure_m.csproj index 01b87484..bc2099bd 100644 --- a/azure_m/azure_m/azure_m/azure_m.csproj +++ b/azure_m/azure_m/azure_m/azure_m.csproj @@ -24,9 +24,6 @@ - - AddVMDetailsPage~.xaml - VirtualMachinePage.xaml @@ -36,9 +33,6 @@ MSBuild:UpdateDesignTimeXaml - - MSBuild:UpdateDesignTimeXaml - MSBuild:UpdateDesignTimeXaml -- GitLab