/
MicrosoftAuthentication.cs
103 lines (90 loc) · 4.44 KB
/
MicrosoftAuthentication.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using Microsoft.Identity.Client;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace Libraries.Auth.Microsoft
{
public class MicrosoftAuthentication
{
#region Properties
private AuthenticationSettings AuthSettings { get; }
public IPublicClientApplication PublicClientApp { get; }
protected readonly string[] Scopes = new string[] { "XboxLive.signin", "XboxLive.offline_access" };
#endregion
public MicrosoftAuthentication(AuthenticationSettings authSettings)
{
AuthSettings = authSettings;
PublicClientApp = PublicClientApplicationBuilder.Create(AuthSettings.ClientId).WithAuthority(AzureCloudInstance.AzurePublic, AuthSettings.Tenant, true).WithRedirectUri("http://localhost").Build();
}
#region SignIn
/// <summary>
/// Sign in with your Microsoft Account<br/>
/// When calling it, a new web browser tab will open in your default browser that will prompt you to log in with your Microsoft Account. After it, if everything went well, you'll be prompted to close the tab and the process finished successfully.
/// When that happens, this method will return with <see cref="AuthenticationResult"/>.
/// </summary>
/// <returns>With <see cref="AuthenticationResult"/> or will throw an <see cref="Exception"/> if a problem occurred during the authentication process.</returns>
public async Task<AuthenticationResult> SignIn()
{
AuthenticationResult authResult;
var app = PublicClientApp;
var accounts = await app.GetAccountsAsync();
var firstAccount = accounts.FirstOrDefault();
try
{
authResult = await app.AcquireTokenSilent(Scopes, firstAccount).ExecuteAsync();
}
catch (MsalUiRequiredException ex)
{
// A MsalUiRequiredException happened on AcquireTokenSilent.
// This indicates you need to call AcquireTokenInteractive to acquire a token
System.Diagnostics.Debug.WriteLine($"MsalUiRequiredException: {ex.Message}");
try
{
var swvo = new SystemWebViewOptions();
if (AuthSettings.BrowserRedirectSuccess != null) swvo.BrowserRedirectSuccess = AuthSettings.BrowserRedirectSuccess;
if (AuthSettings.BrowserRedirectError != null) swvo.BrowserRedirectError = AuthSettings.BrowserRedirectError;
var ewvo = new EmbeddedWebViewOptions();
ewvo.Title = "HunLux Launcher internal authenticator";
//authResult = await app.AcquireTokenInteractive(Scopes).WithAccount(accounts.FirstOrDefault()).WithPrompt(Prompt.SelectAccount).WithSystemWebViewOptions(swvo).ExecuteAsync();
authResult = await app.AcquireTokenInteractive(Scopes).WithAccount(accounts.FirstOrDefault()).WithPrompt(Prompt.SelectAccount).WithEmbeddedWebViewOptions(ewvo).ExecuteAsync();
}
catch (MsalException msalex)
{
System.Diagnostics.Debug.WriteLine($"Error Acquiring Token: {System.Environment.NewLine}{msalex}");
throw;
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error Acquiring Token Silently: {System.Environment.NewLine}{ex}");
throw;
}
return authResult;
}
#endregion
#region SignOut
/// <summary>
/// Sign out your logged in account
/// </summary>
/// <returns>With <b>true</b> if logged out successfully, or returns with <b>false</b> if no account was logged in or if a problem occurs during the logout process, it will throw an <see cref="MsalException"/>.</returns>
public async Task<bool> SignOut()
{
var accounts = await PublicClientApp.GetAccountsAsync();
if (accounts.Any())
{
try
{
await PublicClientApp.RemoveAsync(accounts.FirstOrDefault());
return true;
}
catch (MsalException ex)
{
Console.WriteLine($"Error signing-out user: {ex.Message}");
throw;
}
}
return false;
}
#endregion
}
}