annotate Agendas/trunk/src/Agendas.Web/Controllers/AccountController.cs @ 10:c62b77fc33f4

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