-
Notifications
You must be signed in to change notification settings - Fork 4
/
DlgAuthorization.cs
92 lines (85 loc) · 3.65 KB
/
DlgAuthorization.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
using System;
using System.Collections.Specialized;
using System.Linq;
using System.Windows.Forms;
namespace LinkedInDesktopUI
{
// The code in this method extracted from the LinkedIn.NET's LNTest DlgAuthorization.cs.
// Windows form with WebBrowser control - wbAuth - for performing authorization on LinkedIn
public partial class DlgAuthorization : Form
{
public DlgAuthorization(string authLink)
{
InitializeComponent();
// parse authorization link
var qs = parseResponse(authLink);
// store state for further use
if (qs["state"] != null)
{
_State = qs["state"];
}
// store redirect URL for further use
if (qs["redirect_uri"] != null)
{
_RedirectUri = new Uri(qs["redirect_uri"]);
}
// navigate to authorization link
wbAuth.Navigate(new Uri(authLink));
}
private readonly string _State;
private readonly Uri _RedirectUri;
// property for handling authorization code
public string AuthorizationCode { get; private set; }
// properties for handling possible errors
public string OauthError { get; private set; }
public string OauthErrorDescription { get; private set; }
private void DlgAuthorization_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape) DialogResult = DialogResult.Cancel;
}
private NameValueCollection parseResponse(string response)
{
var nvc = new NameValueCollection();
if (response.StartsWith("?")) response = response.Substring(1);
var arr1 = response.Split('&');
foreach (var arr2 in arr1.Select(s => s.Split('=')).Where(arr2 => arr2.Length == 2))
{
nvc.Add(arr2[0].Trim(), arr2[1].Trim());
}
return nvc;
}
private void wbAuth_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
// resize broser control in order to to force the windows to show vertical scrollbar
// LinkedIn authorization always fill the whole browser, so this is the only way to show all fields and buttons in window of suitable size without stretching it to screen height
wbAuth.Height = ClientSize.Height * 2;
// chack whether we are in needed end point
if (e.Url.Scheme != _RedirectUri.Scheme || e.Url.Host != _RedirectUri.Host ||
e.Url.AbsolutePath != _RedirectUri.AbsolutePath) return;
var queryParams = e.Url.Query;
if (queryParams.Length <= 1) return;
// parse query
var qs = parseResponse(queryParams);
// check state parameter
if (qs["state"] == null) DialogResult = DialogResult.Cancel;
if (qs["state"] != _State) DialogResult = DialogResult.Cancel;
// check code parameter
if (qs["code"] != null)
{
// store code parameter and close the window
AuthorizationCode = qs["code"];
DialogResult = DialogResult.OK;
}
// check for possible errors
else if (qs["error"] != null)
{
OauthError = qs["error"];
if (qs["error_description"] != null)
{
OauthErrorDescription = qs["error_description"];
}
DialogResult = DialogResult.Cancel;
}
}
}
}