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