forked from Slazanger/SMT
-
Notifications
You must be signed in to change notification settings - Fork 1
/
LogonWindow.xaml.cs
81 lines (72 loc) · 2.68 KB
/
LogonWindow.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
using System;
using System.Net;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
namespace SMT
{
/// <summary>
/// Interaction logic for LogonWindow.xaml
/// </summary>
public partial class LogonWindow : Window
{
private HttpListener listener;
public LogonWindow()
{
InitializeComponent();
new Task(StartServer).Start();
}
private void StartServer()
{
// create the http Server
listener = new HttpListener();
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
string challengeCode = Utils.RandomString(32);
string esiLogonURL = EVEData.EveManager.Instance.GetESILogonURL(challengeCode);
System.Diagnostics.Process.Start(esiLogonURL);
try
{
listener.Prefixes.Add(EVEData.EveAppConfig.CallbackURL);
listener.Start();
Console.WriteLine("Listening...");
// Note: The GetContext method blocks while waiting for a request.
HttpListenerContext context = listener.GetContext();
HttpListenerRequest request = context.Request;
EVEData.EveManager.Instance.HandleEveAuthSMTUri(request.Url, challengeCode);
// Obtain a response object.
HttpListenerResponse response = context.Response;
// Construct a response.
string responseString = "<HTML><BODY>SMT Character Added, please close</BODY></HTML>";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
// Get a response stream and write the response to it.
response.ContentLength64 = buffer.Length;
System.IO.Stream output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
// You must close the output stream.
output.Close();
listener.Stop();
Application.Current.Dispatcher.Invoke((Action)(() =>
{
// now close the window
Close();
}), DispatcherPriority.Normal, null);
}
catch
{
}
}
private void Window_Closed(object sender, EventArgs e)
{
try
{
if (listener != null && listener.IsListening)
{
listener.Stop();
}
}
catch
{
}
}
}
}