AzureDNSManager.AzureDNSViewModel.AzureDNSViewModel C# (CSharp) Method

AzureDNSViewModel() public method

public AzureDNSViewModel ( ) : Microsoft.Azure.Common.Authentication
return Microsoft.Azure.Common.Authentication
        public AzureDNSViewModel()
        {
            AddZoneCommand = new Command(this.AddZone);
            AddRecordCommand = new Command(this.AddRecord);
            DeleteRecordCommand = new Command(this.DeleteRecord);
            CommitRecordCommand = new Command(this.CommitRecord);
            AddRecordEntryCommand = new Command(this.AddRecordEntry);
            DeleteRecordEntryCommand = new Command(this.DeleteRecordEntry);
            CopyRecordSetCommand = new Command(this.CopyRecordSet);
            PasteRecordSetCommand = new Command(this.PasteRecordSet);

            string tenant = "Common";
            try
            {
                var azureEnv = AzureEnvironment.PublicEnvironments["AzureCloud"];
                var azureAccount = new AzureAccount()
                {
                    Id = null,
                    Type = AzureAccount.AccountType.User,
                    Properties = new Dictionary<AzureAccount.Property, string>()
                        {
                            {
                                AzureAccount.Property.Tenants, tenant
                            }
                        }
                };
                var azureSub = new AzureSubscription()
                {
                    Account = azureAccount.Id,
                    Environment = azureEnv.Name,
                    Properties = new Dictionary<AzureSubscription.Property, string>()
                    { { AzureSubscription.Property.Tenants, tenant }}

                };

                _azureContext = new AzureContext(azureSub, azureAccount, azureEnv);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to setup azure account credentials!");
                Debug.WriteLine(ex.Message);
                return;
            }

            try
            {
                IAccessToken token = AzureSession.AuthenticationFactory.Authenticate(_azureContext.Account, _azureContext.Environment, tenant, null, Microsoft.Azure.Common.Authentication.ShowDialog.Always);
                switch(token.LoginType)
                {
                    case LoginType.OrgId:
                        Debug.WriteLine("Connecting with 'OrgId' account");
                        break;
                    case LoginType.LiveId:
                        Debug.WriteLine("Connecting with 'LiveId' account");
                        break;
                    default:
                        Debug.WriteLine("Connecting with unknown account type");
                        break;
                }
                //_azureContext.Subscription.Account = _azureContext.Account.Id;
            } catch (Exception ex)
            {
                MessageBox.Show("Failed to authenticate with Azure!");
                Debug.WriteLine(ex.Message);
                return;
            }

            try
            {
                var token = AzureSession.AuthenticationFactory.Authenticate(_azureContext.Account, _azureContext.Environment, tenant, null, Microsoft.Azure.Common.Authentication.ShowDialog.Auto, AzureEnvironment.Endpoint.ResourceManager);

                _tokenCreds = new Microsoft.Rest.TokenCredentials(token.AccessToken);

                _subscriptionClient = new SubscriptionClient(_tokenCreds);
                _subscriptions = _subscriptionClient.Subscriptions.List().ToList();
                if (_subscriptions?.Count >= 1)
                {
                    ActiveSubscription = _subscriptions[0].SubscriptionId;
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
        }