-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConnectForm.xaml.cs
135 lines (118 loc) · 4.78 KB
/
ConnectForm.xaml.cs
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using HGM.Hotbird64.Vlmcs;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows;
// ReSharper disable once CheckNamespace
namespace HGM.Hotbird64.LicenseManager
{
public partial class ConnectForm
{
private readonly LicenseMachine machine;
private bool showUI;
private readonly NativeMethods.AuthPrompt auth = new NativeMethods.AuthPrompt();
private NativeMethods.AuthPrompt.CredUiReturnCodes rc;
private readonly MainWindow parent;
private string ComputerName
{
get { return Dispatcher.Invoke(() => Kms.Idn.GetAscii(TextBoxComputername.Text)); }
set { Dispatcher.InvokeAsync(() => TextBoxComputername.Text = Kms.Idn.GetUnicode(value)); }
}
public ConnectForm(MainWindow parent)
{
this.parent = parent;
machine = this.parent.Machine;
InitializeComponent();
TopElement.LayoutTransform = Scaler;
parent.LabelStatus.Text = "Getting Computer Name";
ButtonConnectLocal.IsEnabled = machine.ComputerName != ".";
}
private async Task Connect_Worker()
{
try
{
MainGrid.IsEnabled = false;
await Task.Run(() =>
{
machine.Connect(ComputerName, auth.User, auth.SecurePassword, machine.IncludeInactiveLicenses);
});
DialogResult = true;
rc = auth.ConfirmCredentials(true);
}
catch (UnauthorizedAccessException ex)
{
parent.IsProgressBarRunning = false;
rc = auth.ConfirmCredentials(false);
if (showUI)
{
parent.LabelStatus.Text = "Access denied";
MessageBox.Show(ex.Message, "Access denied", MessageBoxButton.OK, MessageBoxImage.Error);
parent.LabelStatus.Text = "Getting Computer Name";
}
else
{
showUI = true;
}
MainGrid.IsEnabled = true;
}
catch (COMException ex)
{
parent.IsProgressBarRunning = false;
parent.LabelStatus.Text = "DCOM Error";
switch ((uint)ex.ErrorCode)
{
case 0x80070776:
MessageBox.Show(ex.Message, "Microsoft Is Lame", MessageBoxButton.OK, MessageBoxImage.Error);
break;
default:
MessageBox.Show(ex.Message, "DCOM Error", MessageBoxButton.OK, MessageBoxImage.Error);
break;
}
parent.LabelStatus.Text = "Getting Computer Name";
MainGrid.IsEnabled = true;
}
catch (Exception ex)
{
parent.IsProgressBarRunning = false;
parent.LabelStatus.Text = "Error";
MessageBox.Show("The following error occured: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
parent.LabelStatus.Text = "Getting Computer Name";
MainGrid.IsEnabled = true;
}
}
[SuppressMessage("ReSharper", "PossibleInvalidOperationException")]
public async void button_Connect_Click(object sender, RoutedEventArgs e)
{
if (ComputerName == "")
{
MessageBox.Show("Try entering something in the field Computername!", "Noob Alert!!!", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
parent.LabelStatus.Text = "Connecting...";
MainGrid.IsEnabled = false;
if (ComputerName != ".")
{
auth.ServerName = ComputerName;
if (!showUI) showUI = CheckBoxShowUi.IsChecked.Value;
MainGrid.IsEnabled = false;
rc = auth.PromptForPassword(showUI,
"Enter username and password of an account with administrative privileges on "
+ ComputerName,
"Authentication Required");
parent.IsProgressBarRunning = true;
if (rc == NativeMethods.AuthPrompt.CredUiReturnCodes.ERROR_CANCELLED)
{
DialogResult = false;
return;
}
}
await Connect_Worker();
}
private void button_Local_Click(object sender, RoutedEventArgs e)
{
ComputerName = ".";
button_Connect_Click(sender, e);
}
}
}