Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
126 lines (117 sloc) 4.73 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net.Http;
using System.Globalization;
using System.Net.Http.Headers;
using System.Windows.Interop;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Azure.ActiveDirectory.GraphClient;
namespace WpfAppGoAzure
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
// Client ID
// クライアントIDを記入
string clientId = "XXXXX-XXXX-XXXX-XXXX-XXXX";
string commonAuthority = "https://login.windows.net/common";
// Redirect URIs
// リダイレクトURIを記入
Uri returnUri = new Uri("http://appgoazure");
string graphResourceUri = "https://graph.windows.net";
string graphApiVersion = "1.5";
AuthenticationResult authResult = null;
AuthenticationContext authContext = null;
int maxcount = 10;
public MainWindow()
{
InitializeComponent();
authContext = new AuthenticationContext(commonAuthority);
}
async Task<AuthenticationResult> AuthenticateUser()
{
if (authContext.TokenCache.ReadItems().Count() > 0)
{
authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority);
}
authResult = await authContext.AcquireTokenAsync(graphResourceUri, clientId, returnUri, new AuthorizationParameters(PromptBehavior.Auto, new WindowInteropHelper(this).Handle));
return authResult;
}
async Task<string> GetToken()
{
return authResult.AccessToken;
}
async Task<ActiveDirectoryClient> GetActiveDirectoryClient()
{
Uri servicePointUri = new Uri(graphResourceUri);
Uri serviceRoot = new Uri(servicePointUri, authResult.TenantId);
ActiveDirectoryClient activeDirectoryClient = new ActiveDirectoryClient(serviceRoot, async () => await GetToken());
return activeDirectoryClient;
}
private async void ButtonUsersRequest_Click(object sender, RoutedEventArgs e)
{
try
{
await AuthenticateUser();
string graphRequest = String.Format(CultureInfo.InvariantCulture, "{0}/{1}/users?api-version={2}", graphResourceUri, authResult.TenantId, graphApiVersion);
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, graphRequest);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
HttpResponseMessage response = await client.SendAsync(request);
string content = await response.Content.ReadAsStringAsync();
TextBoxResult.Text = content;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private async void ButtonGroupsRequest_Click(object sender, RoutedEventArgs e)
{
try
{
await AuthenticateUser();
ActiveDirectoryClient adc = await GetActiveDirectoryClient();
var groups = from u in adc.Groups.Take(maxcount).ExecuteAsync().Result.CurrentPage
select u.DisplayName;
string content = string.Join("\r\n", groups);
TextBoxResult.Text = content;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private async void ButtonApplicationsRequest_Click(object sender, RoutedEventArgs e)
{
try
{
await AuthenticateUser();
//GET /<Tenant-Id>/applications()?$filter=startswith(displayName,'W')&$top=10&api-version=1.5 HTTP/1.1
ActiveDirectoryClient adc = await GetActiveDirectoryClient();
var Apps = adc.Applications.Where(App => App.DisplayName.StartsWith("W"))
.Take(maxcount).ExecuteAsync().Result.CurrentPage.Select(App => App.DisplayName);
string content = string.Join("\r\n", Apps);
TextBoxResult.Text = content;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}