コード例 #1
0
        private static async Task <AuthenticationResult> CreateAuthResult(IPublicClientApplication app)
        {
            AuthenticationResult authResult = null;
            // This opens a browser window and ask for a microsoft account the frist time
            var accounts = await app.GetAccountsAsync();

            try
            {
                authResult = await app.AcquireTokenSilentAsync(scopes, accounts.FirstOrDefault());
            }
            catch (MsalUiRequiredException ex)
            {
                System.Diagnostics.Debug.WriteLine($"MsalUiRequiredException: {ex.Message}");
                try
                {
                    throw new NotSupportedException("Plase wait microsoft solved the issue https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/issues/728");
                    // Todo: implement device code flow instead of username and passwort if this isse is solved: https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/issues/728
                    // we now must use device authentification!
                    authResult = await app.AcquireTokenWithDeviceCodeAsync(scopes, (result) =>
                    {
                        Console.WriteLine("Login success with message: " + result.Message);
                        return(Task.FromResult(0));
                    }, CancellationToken.None);

                    //Console.WriteLine("Input your OneDrive username");
                    //var user = Console.ReadLine();
                    //Console.WriteLine("Input your OneDrive passwort");
                    //var password = GetPassword();
                    //authResult = await app.AcquireTokenByUsernamePasswordAsync(scopes, user, password);
                }
                catch (MsalException msalex)
                {
                    throw new Exception($"Error Acquiring Token:{System.Environment.NewLine}{msalex}");
                }
            }
            catch (Exception ex)
            {
                throw new Exception($"Error Acquiring Token Silently:{System.Environment.NewLine}{ex}");
            }
            if (authResult != null)
            {
                return(authResult);
            }
            throw new Exception("Error while creating a token.");
        }