Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebApplication/MVC2/HeuristicLabWeb.PluginHost/HLWebPluginHost/Controllers/AccountController.cs @ 5988

Last change on this file since 5988 was 5988, checked in by dkahn, 12 years ago

#1198 restoring membership provider and removing Query references.

File size: 4.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Diagnostics.CodeAnalysis;
4using System.Linq;
5using System.Security.Principal;
6using System.Web;
7using System.Web.Mvc;
8using System.Web.Routing;
9using System.Web.Security;
10using HLWebPluginHost.Models;
11using HLWebPluginHost.OKBQueryService;
12using HLWebPluginHost.Helpers;
13
14namespace 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
42
43
44        if (MembershipService.ValidateUser(model.UserName, model.Password)) {
45
46          FormsService.SignIn(model.UserName, model.RememberMe);
47          if (!String.IsNullOrEmpty(returnUrl)) {
48            return Redirect(returnUrl);
49          } else {
50            return RedirectToAction("Index", "Home");
51          }
52        } else {
53          ModelState.AddModelError("", "The user name or password provided is incorrect.");
54        }
55      }
56
57      // If we got this far, something failed, redisplay form
58      return View(model);
59    }
60
61    // **************************************
62    // URL: /Account/LogOff
63    // **************************************
64
65    public ActionResult LogOff() {
66      FormsService.SignOut();
67      Session.Remove("Username");
68      Session.Remove("Password");
69
70      return RedirectToAction("Index", "Home");
71    }
72
73    // **************************************
74    // URL: /Account/Register
75    // **************************************
76
77    public ActionResult Register() {
78      ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
79      return View();
80    }
81
82    [HttpPost]
83    public ActionResult Register(RegisterModel model) {
84      if (ModelState.IsValid) {
85        // Attempt to register the user
86        MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email);
87
88        if (createStatus == MembershipCreateStatus.Success) {
89          FormsService.SignIn(model.UserName, false /* createPersistentCookie */);
90          return RedirectToAction("Index", "Home");
91        } else {
92          ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
93        }
94      }
95
96      // If we got this far, something failed, redisplay form
97      ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
98      return View(model);
99    }
100
101    // **************************************
102    // URL: /Account/ChangePassword
103    // **************************************
104
105    [Authorize]
106    public ActionResult ChangePassword() {
107      ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
108      return View();
109    }
110
111    [Authorize]
112    [HttpPost]
113    public ActionResult ChangePassword(ChangePasswordModel model) {
114      if (ModelState.IsValid) {
115        if (MembershipService.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword)) {
116          return RedirectToAction("ChangePasswordSuccess");
117        } else {
118          ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
119        }
120      }
121
122      // If we got this far, something failed, redisplay form
123      ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
124      return View(model);
125    }
126
127    // **************************************
128    // URL: /Account/ChangePasswordSuccess
129    // **************************************
130
131    public ActionResult ChangePasswordSuccess() {
132      return View();
133    }
134
135  }
136}
Note: See TracBrowser for help on using the repository browser.