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