示例#1
0
        async void OnLoginButtonClicked(object sender, EventArgs args)
        {
            //var response = await DependencyService.Get<IAuthService>().Authenticate(Settings.TenantId, Settings.graphResourceUri, Settings.clientId, Settings.returnUri, Settings.ClientSecret);

            var opcVaultOptions = new OpcVaultApiOptions();
            var azureADOptions  = new OpcVaultAzureADOptions();

            using (var cts = new CancellationTokenSource())
            {
                var Settings = await ConfigurationManager.Instance.GetAsync(cts.Token);

                opcVaultOptions.BaseAddress = Settings.AppServiceURL;
                opcVaultOptions.ResourceId  = Settings.graphResourceUri;
                azureADOptions.ClientId     = Settings.clientId;
                azureADOptions.ClientSecret = Settings.ClientSecret;
                azureADOptions.Authority    = Settings.commonAuthority;
                azureADOptions.TenantId     = Settings.TenantId;
            }



            var       serviceClient         = new OpcVaultLoginCredentials(opcVaultOptions, azureADOptions);
            IOpcVault opcVaultServiceClient = new Microsoft.Azure.IIoT.OpcUa.Api.Vault.OpcVault(new Uri(opcVaultOptions.BaseAddress), serviceClient);

            //var opcVaultHandler = new OpcVaultClientHandler(opcVaultServiceClient);
            //Application.Current.MainPage = new XamarinClient.XAML.BasePage(opcVaultServiceClient);
            Application.Current.MainPage = new BasePage(opcVaultServiceClient);
        }
 protected void AuthorizeClient()
 {
     if (_opcVault == null)
     {
         ServiceClientCredentials serviceClientCredentials =
             new OpcVaultLoginCredentials(_opcVaultOptions, _azureADOptions, _tokenCacheService, User);
         _opcVault = new OpcVault(new Uri(_opcVaultOptions.BaseAddress), serviceClientCredentials);
     }
 }
        private async Task ConsoleGlobalDiscoveryServer(
            OpcVaultApiOptions opcVaultOptions,
            OpcVaultAzureADOptions azureADOptions)
        {
            ApplicationInstance.MessageDlg = new ApplicationMessageDlg();
            ApplicationInstance application = new ApplicationInstance
            {
                ApplicationName   = Program.Name,
                ApplicationType   = ApplicationType.Server,
                ConfigSectionName = "Microsoft.Azure.IIoT.OpcUa.Modules.Vault"
            };

            // load the application configuration.
            ApplicationConfiguration config = await application.LoadApplicationConfiguration(false);

            // check the application certificate.
            bool haveAppCertificate = await application.CheckApplicationInstanceCertificate(false, 0);

            if (!haveAppCertificate)
            {
                throw new Exception("Application instance certificate invalid!");
            }

            if (!config.SecurityConfiguration.AutoAcceptUntrustedCertificates)
            {
                config.CertificateValidator.CertificateValidation += new CertificateValidationEventHandler(CertificateValidator_CertificateValidation);
            }

            // get the DatabaseStorePath configuration parameter.
            GlobalDiscoveryServerConfiguration opcVaultConfiguration = config.ParseExtension <GlobalDiscoveryServerConfiguration>();

            // extract appId and vault name from database storage path
            string[] keyVaultConfig = opcVaultConfiguration.DatabaseStorePath?.Split(',');
            if (keyVaultConfig != null)
            {
                if (String.IsNullOrEmpty(opcVaultOptions.BaseAddress))
                {
                    // try configuration using XML config
                    opcVaultOptions.BaseAddress = keyVaultConfig[0];
                }

                if (String.IsNullOrEmpty(opcVaultOptions.ResourceId))
                {
                    if (keyVaultConfig.Length > 1 && !String.IsNullOrEmpty(keyVaultConfig[1]))
                    {
                        opcVaultOptions.ResourceId = keyVaultConfig[1];
                    }
                }

                if (String.IsNullOrEmpty(azureADOptions.ClientId))
                {
                    if (keyVaultConfig.Length > 2 && !String.IsNullOrEmpty(keyVaultConfig[2]))
                    {
                        azureADOptions.ClientId = keyVaultConfig[2];
                    }
                }

                if (String.IsNullOrEmpty(azureADOptions.ClientSecret))
                {
                    if (keyVaultConfig.Length > 3 && !String.IsNullOrEmpty(keyVaultConfig[3]))
                    {
                        azureADOptions.ClientSecret = keyVaultConfig[3];
                    }
                }

                if (String.IsNullOrEmpty(azureADOptions.TenantId))
                {
                    if (keyVaultConfig.Length > 4 && !String.IsNullOrEmpty(keyVaultConfig[4]))
                    {
                        azureADOptions.TenantId = keyVaultConfig[4];
                    }
                }

                if (String.IsNullOrEmpty(azureADOptions.Authority))
                {
                    if (keyVaultConfig.Length > 5 && !String.IsNullOrEmpty(keyVaultConfig[5]))
                    {
                        azureADOptions.Authority = keyVaultConfig[5];
                    }
                }
            }

            var       serviceClient         = new OpcVaultLoginCredentials(opcVaultOptions, azureADOptions);
            IOpcVault opcVaultServiceClient = new Microsoft.Azure.IIoT.OpcUa.Api.Vault.OpcVault(new Uri(opcVaultOptions.BaseAddress), serviceClient);
            var       opcVaultHandler       = new OpcVaultClientHandler(opcVaultServiceClient);

            // read configurations from OpcVault secret
            opcVaultConfiguration.CertificateGroups = await opcVaultHandler.GetCertificateConfigurationGroupsAsync(opcVaultConfiguration.BaseCertificateGroupStorePath);

            UpdateGDSConfigurationDocument(config.Extensions, opcVaultConfiguration);

            var certGroup = new OpcVaultCertificateGroup(opcVaultHandler);
            var requestDB = new OpcVaultCertificateRequest(opcVaultServiceClient);
            var appDB     = new OpcVaultApplicationsDatabase(opcVaultServiceClient);

            requestDB.Initialize();
            // for UNITTEST set auto approve true
            server = new OpcVaultGlobalDiscoveryServer(appDB, requestDB, certGroup, false);

            // start the server.
            await application.Start(server);

            // print endpoint info
            var endpoints = application.Server.GetEndpoints().Select(e => e.EndpointUrl).Distinct();

            foreach (var endpoint in endpoints)
            {
                Console.WriteLine(endpoint);
            }

            // start the status thread
            status = Task.Run(new Action(StatusThread));

            // print notification on session events
            server.CurrentInstance.SessionManager.SessionActivated += EventStatus;
            server.CurrentInstance.SessionManager.SessionClosing   += EventStatus;
            server.CurrentInstance.SessionManager.SessionCreated   += EventStatus;
        }