/
MainPage.xaml.cs
47 lines (42 loc) · 1.56 KB
/
MainPage.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System;
using Xamarin.Forms;
namespace MFATest
{
public partial class MainPage : ContentPage
{
public static string clientId = "<<INSERT YOUR CLIENT ID HERE>>";
public static string authority = "https://login.windows.net/common";
public static string returnUri = "http://MFATestPCL-redirect";
private const string graphResourceUri = "https://graph.windows.net";
private AuthenticationResult authResult = null;
public MainPage()
{
InitializeComponent();
}
private async void btnLogin_Clicked(object sender, EventArgs e)
{
try
{
var auth = DependencyService.Get<IAuthenticator>();
authResult = await auth.Authenticate(authority, graphResourceUri, clientId, returnUri);
var userName = authResult.UserInfo.GivenName + " " + authResult.UserInfo.FamilyName;
await DisplayAlert("Token", userName, "Ok", "Cancel");
}
catch (Exception ex)
{
await DisplayAlert("Error", ex.Message, "Ok");
}
}
private void btnLogout_Clicked(object sender, EventArgs e)
{
if (authResult != null)
{
AuthenticationContext ac = new AuthenticationContext(authority);
ac.TokenCache.Clear();
var auth = DependencyService.Get<IAuthenticator>();
auth.ClearAllCookies();
}
}
}
}