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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
//-----------------------------------------------------------------------
// <copyright file="MainWindow.xaml.cs" company="Outercurve Foundation">
// Copyright (c) Outercurve Foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.OpenIdOfflineProvider {
using System;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http.Headers;
using System.Runtime.InteropServices;
using System.ServiceModel;
using System.Threading.Tasks;
using System.Web;
using System.Windows;
using System.Windows.Input;
using DotNetOpenAuth.Logging;
using Microsoft.Owin.Hosting;
using Validation;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, IDisposable {
/// <summary>
/// The main window for the app.
/// </summary>
internal static MainWindow Instance;
/// <summary>
/// The logger the application may use.
/// </summary>
private ILog _logger;
private IDisposable hostServer;
/// <summary>
/// Initializes a new instance of the <see cref="MainWindow"/> class.
/// </summary>
public MainWindow() {
this.InitializeComponent();
LogProvider.SetCurrentLogProvider(new TextWriterLogProvider(new TextBoxTextWriter(this.logBox)));
this._logger = LogProvider.GetLogger(typeof(MainWindow));
Instance = this;
this.StartProviderAsync();
}
#region IDisposable Members
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose() {
this.Dispose(true);
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing) {
if (disposing) {
if (this.hostServer != null) {
this.hostServer.Dispose();
}
this.hostServer = null;
}
}
#endregion
/// <summary>
/// Raises the <see cref="E:Closing"/> event.
/// </summary>
/// <param name="e">The <see cref="System.ComponentModel.CancelEventArgs"/> instance containing the event data.</param>
protected override void OnClosing(CancelEventArgs e) {
this.StopProviderAsync();
base.OnClosing(e);
}
/// <summary>
/// Adds a set of HTTP headers to an <see cref="HttpResponse"/> instance,
/// taking care to set some headers to the appropriate properties of
/// <see cref="HttpResponse" />
/// </summary>
/// <param name="headers">The headers to add.</param>
/// <param name="response">The <see cref="HttpListenerResponse"/> instance to set the appropriate values to.</param>
private static void ApplyHeadersToResponse(HttpResponseHeaders headers, HttpListenerResponse response) {
Requires.NotNull(headers, "headers");
Requires.NotNull(response, "response");
foreach (var header in headers) {
switch (header.Key) {
case "Content-Type":
response.ContentType = header.Value.First();
break;
// Add more special cases here as necessary.
default:
response.AddHeader(header.Key, header.Value.First());
break;
}
}
}
/// <summary>
/// Handles the MouseDown event of the opIdentifierLabel control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
private void opIdentifierLabel_MouseDown(object sender, MouseButtonEventArgs e) {
try {
Clipboard.SetText(this.opIdentifierLabel.Content.ToString());
} catch (COMException ex) {
MessageBox.Show(this, ex.Message, "Error while copying OP Identifier to the clipboard", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
/// <summary>
/// Handles the Click event of the ClearLogButton control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
private void ClearLogButton_Click(object sender, RoutedEventArgs e) {
this.logBox.Clear();
}
/// <summary>
/// Starts the provider.
/// </summary>
/// <returns>A task that completes when the asynchronous operation is finished.</returns>
private async Task StartProviderAsync() {
Exception exception = null;
try {
Verify.Operation(this.hostServer == null, "Server already started.");
int port = 45235;
try {
this.hostServer = WebApp.Start<Startup>(url: string.Format("http://localhost:{0}", port));
this._logger.Info("Server Started");
} catch (AddressAccessDeniedException ex) {
// If this throws an exception, use an elevated command prompt and execute:
// netsh http add urlacl url=http://+:45235/ user=YOUR_USERNAME_HERE
string message = string.Format(
CultureInfo.CurrentCulture,
"Use an elevated command prompt and execute: \nnetsh http add urlacl url=http://+:{0}/ user={1}\\{2}",
port,
Environment.UserDomainName,
Environment.UserName);
throw new InvalidOperationException(message, ex);
}
this.opIdentifierLabel.Content = string.Format("http://localhost:{0}", port);
} catch (InvalidOperationException ex) {
exception = ex;
}
if (exception != null) {
if (MessageBox.Show(exception.Message, "Configuration error", MessageBoxButton.OKCancel, MessageBoxImage.Error)
== MessageBoxResult.OK) {
await this.StartProviderAsync();
return;
} else {
this.Close();
}
}
}
/// <summary>
/// Stops the provider.
/// </summary>
/// <returns>A task that completes when the asynchronous operation is finished.</returns>
private async Task StopProviderAsync() {
if (this.hostServer != null) {
this.hostServer.Dispose();
this.hostServer = null;
}
this.opIdentifierLabel.Content = string.Empty;
}
}
}
|