Example #1
0
        public async Task <DeviceGuardConfig> SignIn(CancellationToken cancellationToken)
        {
            var    factory      = new MsixHeroClientFactory();
            string refreshToken = null;
            EventHandler <string> gotRefreshToken = (sender, s) =>
            {
                refreshToken = s;
            };

            try
            {
                factory.GotRefreshToken += gotRefreshToken;
                var clientApp = PublicClientApplicationBuilder.Create("4dd963fd-7400-4ce3-bc90-0bed2b65820d")
                                .WithRedirectUri("https://login.microsoftonline.com/common/oauth2/nativeclient")
                                .WithHttpClientFactory(factory)
                                .Build();

                await clientApp.GetAccountsAsync().ConfigureAwait(true);

                var result = await clientApp.AcquireTokenInteractive(Scope).WithPrompt(Prompt.ForceLogin).ExecuteAsync(cancellationToken).ConfigureAwait(false);

                var tokens = new DeviceGuardConfig(result.AccessToken, refreshToken);

                return(tokens);
            }
            finally
            {
                factory.GotRefreshToken -= gotRefreshToken;
            }
        }
Example #2
0
        public async Task <DeviceGuardConfig> SignIn(bool validateSubject = false, CancellationToken cancellationToken = default, IProgress <ProgressData> progress = default)
        {
            var    factory      = new MsixHeroClientFactory();
            string refreshToken = null;
            EventHandler <string> gotRefreshToken = (_, s) =>
            {
                refreshToken = s;
            };

            progress?.Report(new ProgressData(0, "Signing-in..."));

            try
            {
                factory.GotRefreshToken += gotRefreshToken;
                var clientApp = PublicClientApplicationBuilder.Create("4dd963fd-7400-4ce3-bc90-0bed2b65820d")
                                .WithRedirectUri("https://login.microsoftonline.com/common/oauth2/nativeclient")
                                .WithHttpClientFactory(factory)
                                .Build();

                await clientApp.GetAccountsAsync().ConfigureAwait(true);

                var result = await clientApp.AcquireTokenInteractive(Scope).WithPrompt(Prompt.ForceLogin).ExecuteAsync(cancellationToken).ConfigureAwait(false);

                var tokens = new DeviceGuardConfig(result.AccessToken, refreshToken);

                if (validateSubject)
                {
                    progress?.Report(new ProgressData(50, "Validating signing capabilities..."));

                    var dgh  = new DeviceGuardHelper();
                    var json = await this.CreateDeviceGuardJsonTokenFile(new DeviceGuardConfig(result.AccessToken, refreshToken), cancellationToken).ConfigureAwait(false);

                    try
                    {
                        // set the result subject.
                        tokens.Subject = await dgh.GetSubjectFromDeviceGuardSigning(json, cancellationToken).ConfigureAwait(false);
                    }
                    finally
                    {
                        if (json != null && File.Exists(json))
                        {
                            File.Delete(json);
                        }
                    }
                }

                return(tokens);
            }
            finally
            {
                factory.GotRefreshToken -= gotRefreshToken;
            }
        }
Example #3
0
 public MsixHeroDelegationHandler(MsixHeroClientFactory clientFactory)
 {
     this.InnerHandler  = new HttpClientHandler();
     this.clientFactory = clientFactory;
 }