comparison 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
comparison
equal deleted inserted replaced
9:c90492faf268 10:c62b77fc33f4
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 Agendas.Web.Models;
11
12 namespace Agendas.Web.Controllers
13 {
14 public class AccountController : Controller
15 {
16
17 public IFormsAuthenticationService FormsService { get; set; }
18 public IMembershipService MembershipService { get; set; }
19
20 protected override void Initialize(RequestContext requestContext)
21 {
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 {
34 return View();
35 }
36
37 [HttpPost]
38 public ActionResult LogOn(LogOnModel model, string returnUrl)
39 {
40 if (ModelState.IsValid)
41 {
42 if (MembershipService.ValidateUser(model.UserName, model.Password))
43 {
44 FormsService.SignIn(model.UserName, model.RememberMe);
45 if (Url.IsLocalUrl(returnUrl))
46 {
47 return Redirect(returnUrl);
48 }
49 else
50 {
51 return RedirectToAction("Index", "Home");
52 }
53 }
54 else
55 {
56 ModelState.AddModelError("", "The user name or password provided is incorrect.");
57 }
58 }
59
60 // If we got this far, something failed, redisplay form
61 return View(model);
62 }
63
64 // **************************************
65 // URL: /Account/LogOff
66 // **************************************
67
68 public ActionResult LogOff()
69 {
70 FormsService.SignOut();
71
72 return RedirectToAction("Index", "Home");
73 }
74
75 // **************************************
76 // URL: /Account/Register
77 // **************************************
78
79 public ActionResult Register()
80 {
81 ViewBag.PasswordLength = MembershipService.MinPasswordLength;
82 return View();
83 }
84
85 [HttpPost]
86 public ActionResult Register(RegisterModel model)
87 {
88 if (ModelState.IsValid)
89 {
90 // Attempt to register the user
91 MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email);
92
93 if (createStatus == MembershipCreateStatus.Success)
94 {
95 FormsService.SignIn(model.UserName, false /* createPersistentCookie */);
96 return RedirectToAction("Index", "Home");
97 }
98 else
99 {
100 ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
101 }
102 }
103
104 // If we got this far, something failed, redisplay form
105 ViewBag.PasswordLength = MembershipService.MinPasswordLength;
106 return View(model);
107 }
108
109 // **************************************
110 // URL: /Account/ChangePassword
111 // **************************************
112
113 [Authorize]
114 public ActionResult ChangePassword()
115 {
116 ViewBag.PasswordLength = MembershipService.MinPasswordLength;
117 return View();
118 }
119
120 [Authorize]
121 [HttpPost]
122 public ActionResult ChangePassword(ChangePasswordModel model)
123 {
124 if (ModelState.IsValid)
125 {
126 if (MembershipService.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword))
127 {
128 return RedirectToAction("ChangePasswordSuccess");
129 }
130 else
131 {
132 ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
133 }
134 }
135
136 // If we got this far, something failed, redisplay form
137 ViewBag.PasswordLength = MembershipService.MinPasswordLength;
138 return View(model);
139 }
140
141 // **************************************
142 // URL: /Account/ChangePasswordSuccess
143 // **************************************
144
145 public ActionResult ChangePasswordSuccess()
146 {
147 return View();
148 }
149
150 }
151 }