1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Web;
|
---|
5 | using System.Web.Mvc;
|
---|
6 | using System.Web.Routing;
|
---|
7 | using System.Web.Security;
|
---|
8 | using HeuristicLab.Services.Optimization.Web.Models;
|
---|
9 | using System.ServiceModel;
|
---|
10 | using HeuristicLab.Services.Optimization.ControllerService;
|
---|
11 | using System.ServiceModel.Description;
|
---|
12 |
|
---|
13 | namespace HeuristicLab.Services.Optimization.Web.Controllers {
|
---|
14 | public class AccountController : Controller {
|
---|
15 |
|
---|
16 | //
|
---|
17 | // GET: /Account/LogOn
|
---|
18 |
|
---|
19 | public ActionResult LogOn() {
|
---|
20 | return View();
|
---|
21 | }
|
---|
22 |
|
---|
23 | //
|
---|
24 | // POST: /Account/LogOn
|
---|
25 |
|
---|
26 | [HttpPost]
|
---|
27 | public ActionResult LogOn(LogOnModel model, string returnUrl) {
|
---|
28 | if (ModelState.IsValid) {
|
---|
29 | if (Membership.ValidateUser(model.UserName, model.Password)) {
|
---|
30 | FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
|
---|
31 | Session["pw"] = model.Password;
|
---|
32 | if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
|
---|
33 | && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\")) {
|
---|
34 | return Redirect(returnUrl);
|
---|
35 | }
|
---|
36 | else {
|
---|
37 | return RedirectToAction("Index", "Home");
|
---|
38 | }
|
---|
39 | }
|
---|
40 | else {
|
---|
41 | ModelState.AddModelError("", "The user name or password provided is incorrect.");
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | // If we got this far, something failed, redisplay form
|
---|
46 | return View(model);
|
---|
47 | }
|
---|
48 |
|
---|
49 | //
|
---|
50 | // GET: /Account/LogOff
|
---|
51 |
|
---|
52 | public ActionResult LogOff() {
|
---|
53 | FormsAuthentication.SignOut();
|
---|
54 |
|
---|
55 | return RedirectToAction("Index", "Home");
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 | //
|
---|
60 | // GET: /Account/Register
|
---|
61 |
|
---|
62 | public ActionResult Register() {
|
---|
63 | return View();
|
---|
64 | }
|
---|
65 |
|
---|
66 | //
|
---|
67 | // POST: /Account/Register
|
---|
68 |
|
---|
69 | [HttpPost]
|
---|
70 | public ActionResult Register(RegisterModel model) {
|
---|
71 | if (ModelState.IsValid) {
|
---|
72 | // Attempt to register the user
|
---|
73 | MembershipCreateStatus createStatus;
|
---|
74 | Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);
|
---|
75 | if (createStatus == MembershipCreateStatus.Success) {
|
---|
76 | Roles.AddUserToRole(model.UserName, "Web User");
|
---|
77 | Roles.AddUserToRole(model.UserName, "Hive User");
|
---|
78 | FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
|
---|
79 | return RedirectToAction("Index", "Home");
|
---|
80 | }
|
---|
81 | else {
|
---|
82 | ModelState.AddModelError("", ErrorCodeToString(createStatus));
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | // If we got this far, something failed, redisplay form
|
---|
87 | return View(model);
|
---|
88 | }
|
---|
89 |
|
---|
90 | //
|
---|
91 | // GET: /Account/ChangePassword
|
---|
92 |
|
---|
93 | [Authorize]
|
---|
94 | public ActionResult ChangePassword() {
|
---|
95 | return View();
|
---|
96 | }
|
---|
97 |
|
---|
98 | //
|
---|
99 | // POST: /Account/ChangePassword
|
---|
100 |
|
---|
101 | [Authorize]
|
---|
102 | [HttpPost]
|
---|
103 | public ActionResult ChangePassword(ChangePasswordModel model) {
|
---|
104 | if (ModelState.IsValid) {
|
---|
105 |
|
---|
106 | // ChangePassword will throw an exception rather
|
---|
107 | // than return false in certain failure scenarios.
|
---|
108 | bool changePasswordSucceeded;
|
---|
109 | try {
|
---|
110 | MembershipUser currentUser = Membership.GetUser(User.Identity.Name, true /* userIsOnline */);
|
---|
111 | changePasswordSucceeded = currentUser.ChangePassword(model.OldPassword, model.NewPassword);
|
---|
112 | }
|
---|
113 | catch (Exception) {
|
---|
114 | changePasswordSucceeded = false;
|
---|
115 | }
|
---|
116 |
|
---|
117 | if (changePasswordSucceeded) {
|
---|
118 | return RedirectToAction("ChangePasswordSuccess");
|
---|
119 | }
|
---|
120 | else {
|
---|
121 | ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | // If we got this far, something failed, redisplay form
|
---|
126 | return View(model);
|
---|
127 | }
|
---|
128 |
|
---|
129 | //
|
---|
130 | // GET: /Account/ChangePasswordSuccess
|
---|
131 |
|
---|
132 | public ActionResult ChangePasswordSuccess() {
|
---|
133 | return View();
|
---|
134 | }
|
---|
135 |
|
---|
136 | #region Status Codes
|
---|
137 | private static string ErrorCodeToString(MembershipCreateStatus createStatus) {
|
---|
138 | // See http://go.microsoft.com/fwlink/?LinkID=177550 for
|
---|
139 | // a full list of status codes.
|
---|
140 | switch (createStatus) {
|
---|
141 | case MembershipCreateStatus.DuplicateUserName:
|
---|
142 | return "User name already exists. Please enter a different user name.";
|
---|
143 |
|
---|
144 | case MembershipCreateStatus.DuplicateEmail:
|
---|
145 | return "A user name for that e-mail address already exists. Please enter a different e-mail address.";
|
---|
146 |
|
---|
147 | case MembershipCreateStatus.InvalidPassword:
|
---|
148 | return "The password provided is invalid. Please enter a valid password value.";
|
---|
149 |
|
---|
150 | case MembershipCreateStatus.InvalidEmail:
|
---|
151 | return "The e-mail address provided is invalid. Please check the value and try again.";
|
---|
152 |
|
---|
153 | case MembershipCreateStatus.InvalidAnswer:
|
---|
154 | return "The password retrieval answer provided is invalid. Please check the value and try again.";
|
---|
155 |
|
---|
156 | case MembershipCreateStatus.InvalidQuestion:
|
---|
157 | return "The password retrieval question provided is invalid. Please check the value and try again.";
|
---|
158 |
|
---|
159 | case MembershipCreateStatus.InvalidUserName:
|
---|
160 | return "The user name provided is invalid. Please check the value and try again.";
|
---|
161 |
|
---|
162 | case MembershipCreateStatus.ProviderError:
|
---|
163 | return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
|
---|
164 |
|
---|
165 | case MembershipCreateStatus.UserRejected:
|
---|
166 | return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
|
---|
167 |
|
---|
168 | default:
|
---|
169 | return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
|
---|
170 | }
|
---|
171 | }
|
---|
172 | #endregion
|
---|
173 | }
|
---|
174 | }
|
---|