예제 #1
0
        public static void Main(string[] args)
        {
            string appPath = ApplicationBase.GetEntryApplicationPath();

            using (var context = new CommandContext(appPath))
                using (var app = new Application(context))
                {
                    // Workaround for https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/issues/2560
                    if (MicrosoftAuthentication.CanUseBroker(context))
                    {
                        try
                        {
                            MicrosoftAuthentication.InitializeBroker();
                        }
                        catch (Exception ex)
                        {
                            context.Streams.Error.WriteLine(
                                "warning: broker initialization failed{0}{1}",
                                Environment.NewLine, ex.Message
                                );
                        }
                    }

                    // Register all supported host providers at the normal priority.
                    // The generic provider should never win against a more specific one, so register it with low priority.
                    app.RegisterProvider(new AzureReposHostProvider(context), HostProviderPriority.Normal);
                    app.RegisterProvider(new BitbucketHostProvider(context), HostProviderPriority.Normal);
                    app.RegisterProvider(new GitHubHostProvider(context), HostProviderPriority.Normal);
                    app.RegisterProvider(new GitLabHostProvider(context), HostProviderPriority.Normal);
                    app.RegisterProvider(new GenericHostProvider(context), HostProviderPriority.Low);

                    int exitCode = app.RunAsync(args)
                                   .ConfigureAwait(false)
                                   .GetAwaiter()
                                   .GetResult();

                    Environment.Exit(exitCode);
                }
        }
        protected override async Task <bool> RunInternalAsync(StringBuilder log, IList <string> additionalFiles)
        {
            if (MicrosoftAuthentication.CanUseBroker(_context))
            {
                log.Append("Checking broker initialization state...");
                if (MicrosoftAuthentication.IsBrokerInitialized)
                {
                    log.AppendLine(" Initialized");
                }
                else
                {
                    log.AppendLine("  Not initialized");
                    log.Append("Initializing broker...");
                    MicrosoftAuthentication.InitializeBroker();
                    log.AppendLine("OK");
                }
            }
            else
            {
                log.AppendLine("Broker not supported.");
            }

            var msAuth = new MicrosoftAuthentication(_context);

            log.AppendLine($"Flow type is: {msAuth.GetFlowType()}");

            log.Append("Gathering MSAL token cache data...");
            StorageCreationProperties cacheProps = msAuth.CreateTokenCacheProps(true);

            log.AppendLine(" OK");
            log.AppendLine($"CacheDirectory: {cacheProps.CacheDirectory}");
            log.AppendLine($"CacheFileName: {cacheProps.CacheFileName}");
            log.AppendLine($"CacheFilePath: {cacheProps.CacheFilePath}");

            if (PlatformUtils.IsMacOS())
            {
                log.AppendLine($"MacKeyChainAccountName: {cacheProps.MacKeyChainAccountName}");
                log.AppendLine($"MacKeyChainServiceName: {cacheProps.MacKeyChainServiceName}");
            }
            else if (PlatformUtils.IsLinux())
            {
                log.AppendLine($"KeyringCollection: {cacheProps.KeyringCollection}");
                log.AppendLine($"KeyringSchemaName: {cacheProps.KeyringSchemaName}");
                log.AppendLine($"KeyringSecretLabel: {cacheProps.KeyringSecretLabel}");
                log.AppendLine($"KeyringAttribute1: ({cacheProps.KeyringAttribute1.Key},{cacheProps.KeyringAttribute1.Value})");
                log.AppendLine($"KeyringAttribute2: ({cacheProps.KeyringAttribute2.Key},{cacheProps.KeyringAttribute2.Value})");
            }

            log.Append("Creating cache helper...");
            var cacheHelper = await MsalCacheHelper.CreateAsync(cacheProps);

            log.AppendLine(" OK");
            try
            {
                log.Append("Verifying MSAL token cache persistence...");
                cacheHelper.VerifyPersistence();
                log.AppendLine(" OK");
            }
            catch (Exception)
            {
                log.AppendLine(" Failed");
                throw;
            }

            return(true);
        }