- MVCFormAuthentication_src.zip
- MVCFormAuthentication
- Demo.Base
- Demo.Web.Security
- Demo.Web.Tests
- Demo.Web
- App_Data
- bin
- Demo.Web.Security.dll
- Content
- Site.css
- themes
- base
- images
- ui-bg_flat_0_aaaaaa_40x100.png
- ui-bg_flat_75_ffffff_40x100.png
- ui-bg_glass_55_fbf9ee_1x400.png
- ui-bg_glass_65_ffffff_1x400.png
- ui-bg_glass_75_dadada_1x400.png
- ui-bg_glass_75_e6e6e6_1x400.png
- ui-bg_glass_95_fef1ec_1x400.png
- ui-bg_highlight-soft_75_cccccc_1x100.png
- ui-icons_222222_256x240.png
- ui-icons_2e83ff_256x240.png
- ui-icons_454545_256x240.png
- ui-icons_888888_256x240.png
- ui-icons_cd0a0a_256x240.png
- jquery-ui.css
- Controllers
- Demo.Web.cd
- Demo.Web.csproj
- Demo.Web.csproj.user
- Global.asax
- Global.asax.cs
- obj
- Debug
- DesignTimeResolveAssemblyReferencesInput.cache
- TempPE
- Properties
- Scripts
- ServiceInterfaces
- UnityDependencyLocator.cs
- Utilities
- ViewModels
- Views
- Web.config
- Web.Debug.config
- Web.Release.config
- WebRegistry.cs
- Library
- Microsoft.Practices.ServiceLocation.dll
- Microsoft.Practices.Unity.Configuration.dll
- Microsoft.Practices.Unity.dll
- Microsoft.Practices.Unity.Interception.Configuration.dll
- Microsoft.Practices.Unity.Interception.dll
- MVCFormAuthentication.sln
- MVCFormAuthentication.suo
- TestResults
- Henry_LAPTOP 2011-08-27 09_55_30.trx
- Henry_LAPTOP 2011-08-27 09_55_30
- Out
- Demo.Base.dll
- Demo.Base.pdb
- Demo.Web.dll
- Demo.Web.pdb
- demo.web.tests.dll
- demo.web.tests.dll.config
- Demo.Web.Tests.pdb
- Microsoft.Practices.ServiceLocation.dll
- Microsoft.Practices.Unity.Configuration.dll
- Microsoft.Practices.Unity.dll
|
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 Demo.Web.ServiceInterfaces;
using Demo.Web.ViewModels;
using Demo.Web.Utilities;
namespace Demo.Web.Controllers
{
public class AccountController : Controller
{
public IFormsAuthenticationService FormsService { get; set; }
public IMembershipService MembershipService { get; set; }
protected override void Initialize(RequestContext requestContext)
{
if (FormsService == null) { FormsService = WebRegistry.FormsService; }
if (MembershipService == null) { MembershipService = WebRegistry.MembershipService; }
base.Initialize(requestContext);
}
// **************************************
// URL: /Account/LogOn
// **************************************
public ActionResult LogOn()
{
return View();
}
[HttpPost]
public ActionResult LogOn(LogOnViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (MembershipService.ValidateUser(model.UserName, model.Password))
{
FormsService.SignIn(model.UserName, model.RememberMe);
if (Url.IsLocalUrl(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()
{
ViewBag.PasswordLength = MembershipService.MinPasswordLength;
return View();
}
[HttpPost]
public ActionResult Register(RegisterViewModel 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
ViewBag.PasswordLength = MembershipService.MinPasswordLength;
return View(model);
}
// **************************************
// URL: /Account/ChangePassword
// **************************************
[Authorize]
public ActionResult ChangePassword()
{
ViewBag.PasswordLength = MembershipService.MinPasswordLength;
return View();
}
[Authorize]
[HttpPost]
public ActionResult ChangePassword(ChangePasswordViewModel 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
ViewBag.PasswordLength = MembershipService.MinPasswordLength;
return View(model);
}
// **************************************
// URL: /Account/ChangePasswordSuccess
// **************************************
public ActionResult ChangePasswordSuccess()
{
return View();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.