1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Diagnostics.CodeAnalysis;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Security.Principal;
|
---|
6 | using System.Web;
|
---|
7 | using System.Web.Mvc;
|
---|
8 | using System.Web.Routing;
|
---|
9 | using System.Web.Security;
|
---|
10 | using HLWebPluginHost.Models;
|
---|
11 | using HLWebPluginHost.OKBQueryService;
|
---|
12 | using HLWebPluginHost.Helpers;
|
---|
13 |
|
---|
14 | namespace HLWebPluginHost.Controllers {
|
---|
15 |
|
---|
16 | [HandleError]
|
---|
17 | public class AccountController : Controller {
|
---|
18 |
|
---|
19 | public IFormsAuthenticationService FormsService { get; set; }
|
---|
20 | public IMembershipService MembershipService { get; set; }
|
---|
21 |
|
---|
22 | protected override void Initialize(RequestContext requestContext) {
|
---|
23 | if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
|
---|
24 | if (MembershipService == null) { MembershipService = new AccountMembershipService(); }
|
---|
25 |
|
---|
26 | base.Initialize(requestContext);
|
---|
27 | }
|
---|
28 |
|
---|
29 | // **************************************
|
---|
30 | // URL: /Account/LogOn
|
---|
31 | // **************************************
|
---|
32 |
|
---|
33 | public ActionResult LogOn() {
|
---|
34 | return View();
|
---|
35 | }
|
---|
36 |
|
---|
37 | [HttpPost]
|
---|
38 | public ActionResult LogOn(LogOnModel model, string returnUrl) {
|
---|
39 | if (ModelState.IsValid) {
|
---|
40 | QueryServiceClient client = Query.GetClientFactory(model.UserName, model.Password);
|
---|
41 | if (client != null) {
|
---|
42 | Session["Username"] = model.UserName;
|
---|
43 | Session["Password"] = model.Password;
|
---|
44 | FormsService.SignIn(model.UserName, model.RememberMe);
|
---|
45 | if (!String.IsNullOrEmpty(returnUrl)) {
|
---|
46 | return Redirect(returnUrl);
|
---|
47 | } else {
|
---|
48 | return RedirectToAction("Index", "Home");
|
---|
49 | }
|
---|
50 | } else {
|
---|
51 | ModelState.AddModelError("", "The user name or password provided is incorrect.");
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | // If we got this far, something failed, redisplay form
|
---|
56 | return View(model);
|
---|
57 | }
|
---|
58 |
|
---|
59 | // **************************************
|
---|
60 | // URL: /Account/LogOff
|
---|
61 | // **************************************
|
---|
62 |
|
---|
63 | public ActionResult LogOff() {
|
---|
64 | FormsService.SignOut();
|
---|
65 | Session.Remove("Username");
|
---|
66 | Session.Remove("Password");
|
---|
67 |
|
---|
68 | return RedirectToAction("Index", "Home");
|
---|
69 | }
|
---|
70 |
|
---|
71 | // **************************************
|
---|
72 | // URL: /Account/Register
|
---|
73 | // **************************************
|
---|
74 |
|
---|
75 | public ActionResult Register() {
|
---|
76 | ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
|
---|
77 | return View();
|
---|
78 | }
|
---|
79 |
|
---|
80 | [HttpPost]
|
---|
81 | public ActionResult Register(RegisterModel model) {
|
---|
82 | if (ModelState.IsValid) {
|
---|
83 | // Attempt to register the user
|
---|
84 | MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email);
|
---|
85 |
|
---|
86 | if (createStatus == MembershipCreateStatus.Success) {
|
---|
87 | FormsService.SignIn(model.UserName, false /* createPersistentCookie */);
|
---|
88 | return RedirectToAction("Index", "Home");
|
---|
89 | } else {
|
---|
90 | ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | // If we got this far, something failed, redisplay form
|
---|
95 | ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
|
---|
96 | return View(model);
|
---|
97 | }
|
---|
98 |
|
---|
99 | // **************************************
|
---|
100 | // URL: /Account/ChangePassword
|
---|
101 | // **************************************
|
---|
102 |
|
---|
103 | [Authorize]
|
---|
104 | public ActionResult ChangePassword() {
|
---|
105 | ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
|
---|
106 | return View();
|
---|
107 | }
|
---|
108 |
|
---|
109 | [Authorize]
|
---|
110 | [HttpPost]
|
---|
111 | public ActionResult ChangePassword(ChangePasswordModel model) {
|
---|
112 | if (ModelState.IsValid) {
|
---|
113 | if (MembershipService.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword)) {
|
---|
114 | return RedirectToAction("ChangePasswordSuccess");
|
---|
115 | } else {
|
---|
116 | ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | // If we got this far, something failed, redisplay form
|
---|
121 | ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
|
---|
122 | return View(model);
|
---|
123 | }
|
---|
124 |
|
---|
125 | // **************************************
|
---|
126 | // URL: /Account/ChangePasswordSuccess
|
---|
127 | // **************************************
|
---|
128 |
|
---|
129 | public ActionResult ChangePasswordSuccess() {
|
---|
130 | return View();
|
---|
131 | }
|
---|
132 |
|
---|
133 | }
|
---|
134 | }
|
---|