This repository has been archived by the owner on Dec 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
CodiesLoginWindow.xaml.cs
97 lines (84 loc) · 3.82 KB
/
CodiesLoginWindow.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
using MahApps.Metro.Controls;
using System;
using System.Net;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
namespace DR2Rallymaster
{
// Displays a browser window with the Codemasters login page
// Once the user logs in and closes the window, grab the authetication
// cookies to use in the client we get the rest of the data with
public partial class CodiesLoginWindow : MetroWindow
{
[DllImport("wininet.dll", SetLastError = true)]
public static extern bool InternetGetCookieEx(
string url,
string cookieName,
StringBuilder cookieData,
ref int size,
Int32 dwFlags,
IntPtr lpReserved);
private const Int32 InternetCookieHttponly = 0x2000;
private string baseUrl = Properties.Settings.Default.BaseUrl;
public CodiesLoginWindow()
{
InitializeComponent();
var sourceUri = new Uri(baseUrl + "/account/login?returnUri=%2Fclubs%2F");
racenetLoginBrowser.Source = sourceUri;
}
// when the browser closes, grab the cookie container and pass it back to the main window for reuse
private void MetroWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// get the cookie container from the WebBrowser before it closes
var cookieContainer = GetUriCookieContainer(new Uri(baseUrl));
((MainWindow)Application.Current.MainWindow).SharedCookieContainer = cookieContainer;
}
// this gets the cookie container from the web browser control,
// the cookie container will have the authentication data from the racenet login
// from: https://stackoverflow.com/questions/3382498/is-it-possible-to-transfer-authentication-from-webbrowser-to-webrequest
public static CookieContainer GetUriCookieContainer(Uri uri)
{
CookieContainer cookies = null;
// Determine the size of the cookie
int datasize = 8192 * 16;
StringBuilder cookieData = new StringBuilder(datasize);
if (!InternetGetCookieEx(uri.ToString(), null, cookieData, ref datasize, InternetCookieHttponly, IntPtr.Zero))
{
if (datasize < 0)
return null;
// Allocate stringbuilder large enough to hold the cookie
cookieData = new StringBuilder(datasize);
if (!InternetGetCookieEx(
uri.ToString(),
null, cookieData,
ref datasize,
InternetCookieHttponly,
IntPtr.Zero))
return null;
}
if (cookieData.Length > 0)
{
cookies = new CookieContainer();
cookies.SetCookies(uri, cookieData.ToString().Replace(';', ','));
}
return cookies;
}
// uses reflection to suppress the script errors on the page
// from https://stackoverflow.com/questions/6138199/wpf-webbrowser-control-how-to-suppress-script-errors/18289217#18289217
private void RacenetLoginBrowser_Navigating(object sender, System.Windows.Navigation.NavigatingCancelEventArgs e)
{
dynamic activeX = this.racenetLoginBrowser.GetType().InvokeMember("ActiveXInstance",
BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
null, this.racenetLoginBrowser, new object[] { });
activeX.Silent = true;
}
private void RacenetLoginBrowser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
System.Diagnostics.Debug.WriteLine(e.Uri);
if (e.Uri.ToString() == baseUrl + "/clubs/")
this.Close();
}
}
}