aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Utilities/Tango.MachineEM.UI/InputWindow.xaml
blob: 05a4b33cd270aafc447e71700efd7756c19990b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<Window x:Class="Tango.MachineEM.UI.InputWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:mahapps="http://metro.mahapps.com/winfx/xaml/controls"
        xmlns:local="clr-namespace:Tango.MachineEM.UI"
        mc:Ignorable="d"
        Title="Input" Height="250" Width="600">
    <Grid>
        <DockPanel>
            <UniformGrid Columns="2" DockPanel.Dock="Bottom" HorizontalAlignment="Right" Height="45">
                <Button x:Name="btnCancel" Width="150" Margin="5" Style="{StaticResource AccentedSquareButtonStyle}" BorderThickness="0" mahapps:ButtonHelper.PreserveTextCase="True">CANCEL</Button>
                <Button IsDefault="True" x:Name="btnOK" Width="150" Margin="5" Style="{StaticResource AccentedSquareButtonStyle}" BorderThickness="0" mahapps:ButtonHelper.PreserveTextCase="True">OK</Button>
            </UniformGrid>

            <Rectangle DockPanel.Dock="Bottom" Stroke="#494949" StrokeThickness="1" />

            <StackPanel Margin="40">
                <TextBlock x:Name="txtMessage" Text="Please enter a value" Foreground="Gainsboro"></TextBlock>
                <TextBox x:Name="txtValue" Margin="0 10 120 0"></TextBox>
            </StackPanel>
        </DockPanel>
    </Grid>
</Window>
olor: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Tango.AzureUtils.Web;

namespace Tango.AzureUtils
{
    public class AzureUtilsAuthenticationFactory
    {
        private static IAzure _azure;
        private static AzureUtilsCredentials _credentials = new AzureUtilsCredentials()
        {
            ClientID = "be33437c-5052-449f-ab9d-a88d008eae24",
            ClientSecret = "bf67fb6f-4d06-4893-988c-6b347aff23d6",
            TenantID = "2ebd63a5-bc2f-41dc-9066-4409ed5e5dd4",
            SubscriptionID = "10c8aa60-3b15-4e0d-b412-6aeef90e5e91"
        };

        public static void SetGlobalCredentials(AzureUtilsCredentials credentials)
        {
            _credentials = credentials;
        }

        public static Task<IAzure> AuthenticateOrGetAsync()
        {
            if (_credentials == null)
            {
                throw new NullReferenceException("Credentials were not set.");
            }

            if (_azure == null)
            {
                return Task.Factory.StartNew<IAzure>(() =>
                {
                    var creds = SdkContext.AzureCredentialsFactory.FromServicePrincipal(
                        _credentials.ClientID,
                        _credentials.ClientSecret,
                        _credentials.TenantID,
                        AzureEnvironment.AzureGlobalCloud);

                    _azure = Azure.Authenticate(creds).WithSubscription(_credentials.SubscriptionID);
                    return _azure;
                });
            }
            else
            {
                return Task.FromResult(_azure);
            }
        }

        public static Task<IAzure> AuthenticateOrGetAsync(AzureUtilsCredentials credentials)
        {
            return Task.Factory.StartNew<IAzure>(() =>
            {
                var creds = SdkContext.AzureCredentialsFactory.FromServicePrincipal(
                    credentials.ClientID,
                    credentials.ClientSecret,
                    credentials.TenantID,
                    AzureEnvironment.AzureGlobalCloud);

                _azure = Azure.Authenticate(creds).WithSubscription(credentials.SubscriptionID);
                return _azure;
            });
        }

        public static async Task<IAzure> AuthenticateOrGetAsync(String gatewayUrl, String email, String password)
        {
            using (var http = new HttpClient())
            {
                AzureUtilsWebClient client = new AzureUtilsWebClient(gatewayUrl, http);
                var response = await client.LoginAsync(new LoginRequest()
                {
                    Email = email,
                    Password = password
                });

                http.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", response.AccessToken);
                var c = await client.GetCredentialsAsync();
                return await AuthenticateOrGetAsync(new AzureUtilsCredentials()
                {
                    ClientID = c.ClientID,
                    ClientSecret = c.ClientSecret,
                    TenantID = c.TenantID,
                    SubscriptionID = c.SubscriptionID
                });
            }
        }

        public static AzureUtilsCredentials GetGlobalCredentials()
        {
            return _credentials;
        }
    }
}