/
HomeController.cs
290 lines (249 loc) · 9.04 KB
/
HomeController.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebMatrix.WebData;
using SimpleBlogDemo.Helpers;
using System.Globalization;
using SimpleBlogDemo.Models;
using System.Web.Configuration;
using System.Threading.Tasks;
using System.Net.Http;
using System.Web.Script.Serialization;
using Dropbox.Api;
namespace SimpleBlogDemo.Controllers
{
[RequireHttpsOrXForwarded]
public partial class HomeController : AsyncController
{
private UsersStore store = new UsersStore();
private UserProfile currentUser;
private string RedirectUri
{
get
{
if (this.Request.Url.Host.ToLowerInvariant() == "localhost")
{
return string.Format("http://{0}:{1}/Home/Auth", this.Request.Url.Host, this.Request.Url.Port);
}
var builder = new UriBuilder(
Uri.UriSchemeHttps,
this.Request.Url.Host);
builder.Path = "/Home/Auth";
return builder.ToString();
}
}
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
this.currentUser = this.store.CurrentUser();
base.Initialize(requestContext);
}
// GET: /Home
public async Task<ActionResult> IndexAsync()
{
if (WebSecurity.IsAuthenticated)
{
return this.RedirectToAction("Profile");
}
var blogs = new List<Blog>();
foreach (var user in store.Users)
{
var blog = await Blog.FromUserAsync(user);
if (blog != null)
{
blogs.Add(blog);
}
}
blogs.Sort((l, r) => r.MostRecent.CompareTo(l.MostRecent));
return View(blogs);
}
// GET: /Home/Register
public ActionResult Register()
{
if (WebSecurity.IsAuthenticated)
{
return this.RedirectToAction("Profile");
}
return View();
}
// POST: /Home/Register
[HttpPost, ValidateAntiForgeryToken]
public ActionResult Register(string username, string password1, string password2, string blogname, bool remember = false)
{
if (string.IsNullOrWhiteSpace(username))
{
this.Flash("Username must not be empty.", FlashLevel.Warning);
}
else if (string.IsNullOrWhiteSpace(password1) ||
string.IsNullOrWhiteSpace(password2) ||
!string.Equals(password1, password2, StringComparison.Ordinal))
{
this.Flash("Password must not be empty and both passwords must match.", FlashLevel.Warning);
}
else if (WebSecurity.UserExists(username))
{
this.Flash(string.Format(
CultureInfo.InvariantCulture,
"Username '{0}' already exists, choose something else.",
username),
FlashLevel.Warning);
}
else
{
var parameters = new Dictionary<string, object>();
if (!string.IsNullOrEmpty(blogname))
{
parameters["BlogName"] = blogname;
}
WebSecurity.CreateUserAndAccount(username, password1, parameters, requireConfirmationToken: false);
WebSecurity.Login(username, password1, persistCookie: remember);
return this.RedirectToAction("Profile");
}
return View();
}
// Get /Home/Profile
[Authorize]
public async Task<ActionResult> ProfileAsync()
{
await Task.Delay(0);
return View(this.currentUser);
}
// Post /Home/Profile
[HttpPost, ValidateAntiForgeryToken, Authorize]
public async Task<ActionResult> ProfileAsync(UserProfile profile)
{
if (this.currentUser.ID != profile.ID)
{
return this.RedirectToAction("Index");
}
this.currentUser.BlogName = profile.BlogName;
await this.store.SaveChangesAsync();
return View(this.currentUser);
}
// POST : /Home/Disconnect
[HttpPost, ValidateAntiForgeryToken, Authorize]
public async Task<ActionResult> DisconnectAsync()
{
if (!string.IsNullOrWhiteSpace(this.currentUser.BlogName))
{
this.FlushCache(this.currentUser.BlogName);
}
this.currentUser.DropboxAccessToken = string.Empty;
await store.SaveChangesAsync();
this.Flash("This account has been disconnected from Dropbox.", FlashLevel.Success);
return this.RedirectToAction("Profile");
}
// GET: /Home/Connect
[Authorize]
public ActionResult Connect()
{
if (!string.IsNullOrWhiteSpace(this.currentUser.DropboxAccessToken))
{
return this.RedirectToAction("Index");
}
this.currentUser.ConnectState = Guid.NewGuid().ToString("N");
store.SaveChanges();
var redirect = DropboxOAuth2Helper.GetAuthorizeUri(
OAuthResponseType.Code,
MvcApplication.AppKey,
RedirectUri,
this.currentUser.ConnectState);
return Redirect(redirect.ToString());
}
// GET: /Home/Auth
[Authorize]
public async Task<ActionResult> AuthAsync(string code, string state)
{
try
{
if (this.currentUser.ConnectState != state)
{
this.Flash("There was an error connecting to Dropbox.");
return this.RedirectToAction("Index");
}
var response = await DropboxOAuth2Helper.ProcessCodeFlowAsync(
code,
MvcApplication.AppKey,
MvcApplication.AppSecret,
this.RedirectUri);
this.currentUser.DropboxAccessToken = response.AccessToken;
this.currentUser.ConnectState = string.Empty;
await this.store.SaveChangesAsync();
this.Flash("This account has been connected to Dropbox.", FlashLevel.Success);
return RedirectToAction("Profile");
}
catch(Exception e)
{
var message = string.Format(
"code: {0}\nAppKey: {1}\nAppSecret: {2}\nRedirectUri: {3}\nException : {4}",
code,
MvcApplication.AppKey,
MvcApplication.AppSecret,
this.RedirectUri,
e);
this.Flash(message, FlashLevel.Danger);
return RedirectToAction("Profile");
}
}
// POST: /Home/LogOff
[HttpPost, ValidateAntiForgeryToken]
public ActionResult LogOff()
{
if (WebSecurity.IsAuthenticated)
{
WebSecurity.Logout();
}
return RedirectToAction("Index");
}
// GET: /Home/SignIn
public ActionResult SignIn(string returnUrl=null)
{
if (WebSecurity.IsAuthenticated)
{
if (!string.IsNullOrEmpty(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Profile");
}
}
ViewBag.ReturnUrl = returnUrl;
return View();
}
[HttpPost, ValidateAntiForgeryToken]
public ActionResult SignIn(string username, string password, string returnUrl = null, bool remember = false)
{
if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password))
{
this.Flash("Incorrect username or password.", FlashLevel.Danger);
ViewBag.ReturnUrl = returnUrl;
return View();
}
if (!WebSecurity.IsAuthenticated)
{
var result = WebSecurity.Login(username, password, persistCookie: remember);
if (!result)
{
this.Flash("Incorrect username or password.", FlashLevel.Warning);
ViewBag.ReturnUrl = returnUrl;
return View();
}
else if (remember)
{
Response.Cookies.Get(".ASPXAUTH").Expires = DateTime.UtcNow.AddDays(30);
}
}
if (!string.IsNullOrEmpty(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Profile");
}
}
}
}