diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2010-07-31 22:01:16 -0700 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2010-07-31 22:01:16 -0700 |
commit | c94c7f8197eda673947a9d1e0c0b3f3c4efca94f (patch) | |
tree | 0f978cfc2de70c54ac81e11d4339da04dff9f27f /samples/OAuthAuthorizationServer/Controllers/AccountController.cs | |
parent | 7d38eefb65928a1e80036ec006b0e129dc2cface (diff) | |
download | DotNetOpenAuth-c94c7f8197eda673947a9d1e0c0b3f3c4efca94f.zip DotNetOpenAuth-c94c7f8197eda673947a9d1e0c0b3f3c4efca94f.tar.gz DotNetOpenAuth-c94c7f8197eda673947a9d1e0c0b3f3c4efca94f.tar.bz2 |
Split the OAuthServiceProvider sample into two samples: OAuthAuthorizationServer and OAuthResourceServer.
Renamed OAuthConsumer to OAuthClient.
Diffstat (limited to 'samples/OAuthAuthorizationServer/Controllers/AccountController.cs')
-rw-r--r-- | samples/OAuthAuthorizationServer/Controllers/AccountController.cs | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/samples/OAuthAuthorizationServer/Controllers/AccountController.cs b/samples/OAuthAuthorizationServer/Controllers/AccountController.cs new file mode 100644 index 0000000..a62258b --- /dev/null +++ b/samples/OAuthAuthorizationServer/Controllers/AccountController.cs @@ -0,0 +1,127 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Security.Principal; +using System.Web; +using System.Web.Mvc; +using System.Web.Routing; +using System.Web.Security; +using OAuthAuthorizationServer.Models; + +namespace OAuthAuthorizationServer.Controllers { + + [HandleError] + public class AccountController : Controller { + + public IFormsAuthenticationService FormsService { get; set; } + public IMembershipService MembershipService { get; set; } + + protected override void Initialize(RequestContext requestContext) { + if (FormsService == null) { FormsService = new FormsAuthenticationService(); } + if (MembershipService == null) { MembershipService = new AccountMembershipService(); } + + base.Initialize(requestContext); + } + + // ************************************** + // URL: /Account/LogOn + // ************************************** + + public ActionResult LogOn() { + return View(); + } + + [HttpPost] + public ActionResult LogOn(LogOnModel model, string returnUrl) { + if (ModelState.IsValid) { + if (MembershipService.ValidateUser(model.UserName, model.Password)) { + FormsService.SignIn(model.UserName, model.RememberMe); + if (!String.IsNullOrEmpty(returnUrl)) { + return Redirect(returnUrl); + } else { + return RedirectToAction("Index", "Home"); + } + } else { + ModelState.AddModelError("", "The user name or password provided is incorrect."); + } + } + + // If we got this far, something failed, redisplay form + return View(model); + } + + // ************************************** + // URL: /Account/LogOff + // ************************************** + + public ActionResult LogOff() { + FormsService.SignOut(); + + return RedirectToAction("Index", "Home"); + } + + // ************************************** + // URL: /Account/Register + // ************************************** + + public ActionResult Register() { + ViewData["PasswordLength"] = MembershipService.MinPasswordLength; + return View(); + } + + [HttpPost] + public ActionResult Register(RegisterModel model) { + if (ModelState.IsValid) { + // Attempt to register the user + MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email); + + if (createStatus == MembershipCreateStatus.Success) { + FormsService.SignIn(model.UserName, false /* createPersistentCookie */); + return RedirectToAction("Index", "Home"); + } else { + ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus)); + } + } + + // If we got this far, something failed, redisplay form + ViewData["PasswordLength"] = MembershipService.MinPasswordLength; + return View(model); + } + + // ************************************** + // URL: /Account/ChangePassword + // ************************************** + + [Authorize] + public ActionResult ChangePassword() { + ViewData["PasswordLength"] = MembershipService.MinPasswordLength; + return View(); + } + + [Authorize] + [HttpPost] + public ActionResult ChangePassword(ChangePasswordModel model) { + if (ModelState.IsValid) { + if (MembershipService.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword)) { + return RedirectToAction("ChangePasswordSuccess"); + } else { + ModelState.AddModelError("", "The current password is incorrect or the new password is invalid."); + } + } + + // If we got this far, something failed, redisplay form + ViewData["PasswordLength"] = MembershipService.MinPasswordLength; + return View(model); + } + + // ************************************** + // URL: /Account/ChangePasswordSuccess + // ************************************** + + public ActionResult ChangePasswordSuccess() { + return View(); + } + + } +} |