Skip to content

Commit b6a9ab2

Browse files
Update to v1.2. Library updates, and changed auth appId.
1 parent 1d8199b commit b6a9ab2

3 files changed

Lines changed: 344 additions & 344 deletions

File tree

App.xaml.cs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Configuration;
4-
using System.Data;
5-
using System.Linq;
6-
using System.Threading.Tasks;
7-
using System.Windows;
8-
using Microsoft.Identity.Client;
9-
10-
namespace Endpoint_Manager_Scripts_Editor
11-
{
12-
/// <summary>
13-
/// Interaction logic for App.xaml
14-
/// </summary>
15-
public partial class App : Application
16-
{
17-
static App()
18-
{
19-
_clientApp = PublicClientApplicationBuilder.Create(ClientId)
20-
//.WithAuthority($"{Instance}{Tenant}")
21-
//.WithDefaultRedirectUri()
22-
.Build();
23-
//TokenCacheHelper.EnableSerialization(_clientApp.UserTokenCache);
24-
}
25-
private static string ClientId = "d1ddf0e4-d672-4dae-b554-9d5bdfd93547"; //Microsoft - Microsoft Intune PowerShell
26-
//private static string Tenant = "<tenantId here>";
27-
//private static string Instance = "https://login.microsoftonline.com/";
28-
private static IPublicClientApplication _clientApp;
29-
30-
public static IPublicClientApplication PublicClientApp { get { return _clientApp; } }
31-
}
32-
}
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Configuration;
4+
using System.Data;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
using System.Windows;
8+
using Microsoft.Identity.Client;
9+
10+
namespace Endpoint_Manager_Scripts_Editor
11+
{
12+
/// <summary>
13+
/// Interaction logic for App.xaml
14+
/// </summary>
15+
public partial class App : Application
16+
{
17+
static App()
18+
{
19+
_clientApp = PublicClientApplicationBuilder.Create(ClientId)
20+
//.WithAuthority($"{Instance}{Tenant}")
21+
.WithDefaultRedirectUri()
22+
.Build();
23+
//TokenCacheHelper.EnableSerialization(_clientApp.UserTokenCache);
24+
}
25+
private static string ClientId = "14d82eec-204b-4c2f-b7e8-296a70dab67e"; //Microsoft Graph PowerShell (Preview) / Microsoft Graph Command Line Tools //"d1ddf0e4-d672-4dae-b554-9d5bdfd93547"; //Microsoft - Microsoft Intune PowerShell
26+
//private static string Tenant = "<tenantId here>";
27+
//private static string Instance = "https://login.microsoftonline.com/";
28+
private static IPublicClientApplication _clientApp;
29+
30+
public static IPublicClientApplication PublicClientApp { get { return _clientApp; } }
31+
}
32+
}

MainWindow.xaml.cs

Lines changed: 164 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -1,164 +1,164 @@
1-
using System;
2-
using System.Collections;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
7-
using System.Windows;
8-
using System.Windows.Controls;
9-
using System.Windows.Input;
10-
using System.Windows.Interop;
11-
using Microsoft.Identity.Client;
12-
using Newtonsoft.Json;
13-
14-
namespace Endpoint_Manager_Scripts_Editor
15-
{
16-
public partial class MainWindow
17-
{
18-
readonly string[] scopes = new string[] { "DeviceManagementConfiguration.ReadWrite.All" };
19-
private RootValue scripts;
20-
private string authToken;
21-
22-
public MainWindow()
23-
{
24-
InitializeComponent();
25-
}
26-
27-
private async void Btn_ConnectIntune(object sender, RoutedEventArgs e)
28-
{
29-
AuthenticationResult authResult = null;
30-
var app = App.PublicClientApp;
31-
32-
var accounts = await app.GetAccountsAsync();
33-
var firstAccount = accounts.FirstOrDefault();
34-
35-
try
36-
{
37-
authResult = await app.AcquireTokenSilent(scopes, firstAccount)
38-
.ExecuteAsync();
39-
}
40-
catch (MsalUiRequiredException ex)
41-
{
42-
// A MsalUiRequiredException happened on AcquireTokenSilent.
43-
// This indicates you need to call AcquireTokenInteractive to acquire a token
44-
System.Diagnostics.Debug.WriteLine($"MsalUiRequiredException: {ex.Message}");
45-
46-
try
47-
{
48-
authResult = await app.AcquireTokenInteractive(scopes)
49-
.WithAccount(accounts.FirstOrDefault())
50-
.WithParentActivityOrWindow(new WindowInteropHelper(this).Handle) // optional, used to center the browser on the window
51-
.WithPrompt(Microsoft.Identity.Client.Prompt.SelectAccount)
52-
.ExecuteAsync();
53-
}
54-
catch (MsalException msalex)
55-
{
56-
Status.Text = $"Error Acquiring Token:{System.Environment.NewLine}{msalex}";
57-
}
58-
}
59-
catch (Exception ex)
60-
{
61-
Status.Text = $"Error Acquiring Token Silently:{System.Environment.NewLine}{ex}";
62-
return;
63-
}
64-
65-
if (authResult != null)
66-
{
67-
Tenant.Text = $"TenantId: {authResult.TenantId}";
68-
Account.Text = $"Account: {authResult.Account.Username}";
69-
Status.Text = "Retrieving list of scripts";
70-
authToken = authResult.AccessToken;
71-
string graphAPIEndpoint = "https://graph.microsoft.com/beta/deviceManagement/deviceManagementScripts";
72-
var result = await GetHttpContentWithToken(graphAPIEndpoint, authResult.AccessToken);
73-
74-
scripts = JsonConvert.DeserializeObject<RootValue>(result);
75-
ArrayList scriptList = new ArrayList();
76-
foreach (Script script in scripts.value)
77-
{
78-
scriptList.Add(script.displayName);
79-
};
80-
scriptList.Sort();
81-
ComboBox.ItemsSource = scriptList;
82-
Status.Text = "Select a script";
83-
}
84-
}
85-
86-
public async Task<string> GetHttpContentWithToken(string url, string token)
87-
{
88-
var httpClient = new System.Net.Http.HttpClient();
89-
System.Net.Http.HttpResponseMessage response;
90-
91-
try
92-
{
93-
var request = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, url);
94-
//Add the token in Authorization header
95-
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
96-
response = await httpClient.SendAsync(request);
97-
var content = await response.Content.ReadAsStringAsync();
98-
return content;
99-
}
100-
catch (Exception ex)
101-
{
102-
return ex.ToString();
103-
}
104-
}
105-
106-
private async void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
107-
{
108-
int index = scripts.value.FindIndex(x => x.displayName == ComboBox.SelectedItem as string);
109-
string id = scripts.value[index].id;
110-
string graphAPIEndpoint = "https://graph.microsoft.com/beta/deviceManagement/deviceManagementScripts" + "/" + id;
111-
var result = await GetHttpContentWithToken(graphAPIEndpoint, authToken);
112-
113-
Script script = JsonConvert.DeserializeObject<Script>(result);
114-
var base64 = Convert.FromBase64String(script.scriptContent);
115-
var scriptText = Encoding.UTF8.GetString(base64);
116-
ScriptWindow.Text = scriptText;
117-
FileName.IsEnabled = true;
118-
FileName.Foreground = System.Windows.Media.Brushes.AntiqueWhite;
119-
FileName.Text = $"Filename: {script.fileName} |";
120-
RunasThirtyTwo.IsEnabled = true;
121-
RunasThirtyTwo.Text = $"Run as 32-bit: {script.runAs32Bit} |";
122-
RunasThirtyTwo.Foreground = System.Windows.Media.Brushes.AntiqueWhite;
123-
SignatureCheck.IsEnabled = true;
124-
SignatureCheck.Text = $"Enforce signature check: {script.enforceSignatureCheck} |";
125-
SignatureCheck.Foreground = System.Windows.Media.Brushes.AntiqueWhite;
126-
RunasAccount.IsEnabled = true;
127-
RunasAccount.Text = $"Run as account: {script.runAsAccount} |";
128-
RunasAccount.Foreground = System.Windows.Media.Brushes.AntiqueWhite;
129-
Created.IsEnabled = true;
130-
Created.Text = $"Created: {script.createdDateTime} |";
131-
Created.Foreground = System.Windows.Media.Brushes.AntiqueWhite;
132-
Modified.IsEnabled = true;
133-
Modified.Text = $"Modified: {script.lastModifiedDateTime} |";
134-
Modified.Foreground = System.Windows.Media.Brushes.AntiqueWhite;
135-
Description.IsEnabled = true;
136-
Description.Text = $"Description: {script.description}";
137-
Description.Foreground = System.Windows.Media.Brushes.AntiqueWhite;
138-
}
139-
140-
private void MetroWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
141-
{
142-
this.DragMove();
143-
}
144-
}
145-
public class RootValue
146-
{
147-
public string odatametadata { get; set; }
148-
public List<Script> value;
149-
}
150-
public class Script
151-
{
152-
public bool enforceSignatureCheck { get; set; }
153-
public bool runAs32Bit { get; set; }
154-
public string id { get; set; }
155-
public string displayName { get; set; }
156-
public string scriptContent { get; set; }
157-
public string description { get; set; }
158-
public DateTime createdDateTime { get; set; }
159-
public DateTime lastModifiedDateTime { get; set; }
160-
public string runAsAccount { get; set; }
161-
public string fileName { get; set; }
162-
}
163-
164-
}
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using System.Windows;
8+
using System.Windows.Controls;
9+
using System.Windows.Input;
10+
using System.Windows.Interop;
11+
using Microsoft.Identity.Client;
12+
using Newtonsoft.Json;
13+
14+
namespace Endpoint_Manager_Scripts_Editor
15+
{
16+
public partial class MainWindow
17+
{
18+
readonly string[] scopes = new string[] { "DeviceManagementConfiguration.Read.All" };
19+
private RootValue scripts;
20+
private string authToken;
21+
22+
public MainWindow()
23+
{
24+
InitializeComponent();
25+
}
26+
27+
private async void Btn_ConnectIntune(object sender, RoutedEventArgs e)
28+
{
29+
AuthenticationResult authResult = null;
30+
var app = App.PublicClientApp;
31+
32+
var accounts = await app.GetAccountsAsync();
33+
var firstAccount = accounts.FirstOrDefault();
34+
35+
try
36+
{
37+
authResult = await app.AcquireTokenSilent(scopes, firstAccount)
38+
.ExecuteAsync();
39+
}
40+
catch (MsalUiRequiredException ex)
41+
{
42+
// A MsalUiRequiredException happened on AcquireTokenSilent.
43+
// This indicates you need to call AcquireTokenInteractive to acquire a token
44+
System.Diagnostics.Debug.WriteLine($"MsalUiRequiredException: {ex.Message}");
45+
46+
try
47+
{
48+
authResult = await app.AcquireTokenInteractive(scopes)
49+
.WithAccount(accounts.FirstOrDefault())
50+
.WithParentActivityOrWindow(new WindowInteropHelper(this).Handle) // optional, used to center the browser on the window
51+
.WithPrompt(Microsoft.Identity.Client.Prompt.SelectAccount)
52+
.ExecuteAsync();
53+
}
54+
catch (MsalException msalex)
55+
{
56+
Status.Text = $"Error Acquiring Token:{System.Environment.NewLine}{msalex}";
57+
}
58+
}
59+
catch (Exception ex)
60+
{
61+
Status.Text = $"Error Acquiring Token Silently:{System.Environment.NewLine}{ex}";
62+
return;
63+
}
64+
65+
if (authResult != null)
66+
{
67+
Tenant.Text = $"TenantId: {authResult.TenantId}";
68+
Account.Text = $"Account: {authResult.Account.Username}";
69+
Status.Text = "Retrieving list of scripts";
70+
authToken = authResult.AccessToken;
71+
string graphAPIEndpoint = "https://graph.microsoft.com/beta/deviceManagement/deviceManagementScripts";
72+
var result = await GetHttpContentWithToken(graphAPIEndpoint, authResult.AccessToken);
73+
74+
scripts = JsonConvert.DeserializeObject<RootValue>(result);
75+
ArrayList scriptList = new ArrayList();
76+
foreach (Script script in scripts.value)
77+
{
78+
scriptList.Add(script.displayName);
79+
};
80+
scriptList.Sort();
81+
ComboBox.ItemsSource = scriptList;
82+
Status.Text = "Select a script";
83+
}
84+
}
85+
86+
public async Task<string> GetHttpContentWithToken(string url, string token)
87+
{
88+
var httpClient = new System.Net.Http.HttpClient();
89+
System.Net.Http.HttpResponseMessage response;
90+
91+
try
92+
{
93+
var request = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, url);
94+
//Add the token in Authorization header
95+
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
96+
response = await httpClient.SendAsync(request);
97+
var content = await response.Content.ReadAsStringAsync();
98+
return content;
99+
}
100+
catch (Exception ex)
101+
{
102+
return ex.ToString();
103+
}
104+
}
105+
106+
private async void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
107+
{
108+
int index = scripts.value.FindIndex(x => x.displayName == ComboBox.SelectedItem as string);
109+
string id = scripts.value[index].id;
110+
string graphAPIEndpoint = "https://graph.microsoft.com/beta/deviceManagement/deviceManagementScripts" + "/" + id;
111+
var result = await GetHttpContentWithToken(graphAPIEndpoint, authToken);
112+
113+
Script script = JsonConvert.DeserializeObject<Script>(result);
114+
var base64 = Convert.FromBase64String(script.scriptContent);
115+
var scriptText = Encoding.UTF8.GetString(base64);
116+
ScriptWindow.Text = scriptText;
117+
FileName.IsEnabled = true;
118+
FileName.Foreground = System.Windows.Media.Brushes.AntiqueWhite;
119+
FileName.Text = $"Filename: {script.fileName} |";
120+
RunasThirtyTwo.IsEnabled = true;
121+
RunasThirtyTwo.Text = $"Run as 32-bit: {script.runAs32Bit} |";
122+
RunasThirtyTwo.Foreground = System.Windows.Media.Brushes.AntiqueWhite;
123+
SignatureCheck.IsEnabled = true;
124+
SignatureCheck.Text = $"Enforce signature check: {script.enforceSignatureCheck} |";
125+
SignatureCheck.Foreground = System.Windows.Media.Brushes.AntiqueWhite;
126+
RunasAccount.IsEnabled = true;
127+
RunasAccount.Text = $"Run as account: {script.runAsAccount} |";
128+
RunasAccount.Foreground = System.Windows.Media.Brushes.AntiqueWhite;
129+
Created.IsEnabled = true;
130+
Created.Text = $"Created: {script.createdDateTime} |";
131+
Created.Foreground = System.Windows.Media.Brushes.AntiqueWhite;
132+
Modified.IsEnabled = true;
133+
Modified.Text = $"Modified: {script.lastModifiedDateTime} |";
134+
Modified.Foreground = System.Windows.Media.Brushes.AntiqueWhite;
135+
Description.IsEnabled = true;
136+
Description.Text = $"Description: {script.description}";
137+
Description.Foreground = System.Windows.Media.Brushes.AntiqueWhite;
138+
}
139+
140+
private void MetroWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
141+
{
142+
this.DragMove();
143+
}
144+
}
145+
public class RootValue
146+
{
147+
public string odatametadata { get; set; }
148+
public List<Script> value;
149+
}
150+
public class Script
151+
{
152+
public bool enforceSignatureCheck { get; set; }
153+
public bool runAs32Bit { get; set; }
154+
public string id { get; set; }
155+
public string displayName { get; set; }
156+
public string scriptContent { get; set; }
157+
public string description { get; set; }
158+
public DateTime createdDateTime { get; set; }
159+
public DateTime lastModifiedDateTime { get; set; }
160+
public string runAsAccount { get; set; }
161+
public string fileName { get; set; }
162+
}
163+
164+
}

0 commit comments

Comments
 (0)