Last active
September 9, 2015 18:22
-
-
Save xDaevax/76aec2867494c0b1d0d8 to your computer and use it in GitHub Desktop.
Stack Overflow Membership MVC Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Web.Mvc; | |
using System.Web.Routing; | |
namespace AuthDemo { | |
public class RouteConfig { | |
public static void RegisterRoutes(RouteCollection routes) { | |
routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); | |
routes.MapRoute(name: "logon", url: "logon", defaults: new { controller = "Login", action = "Logon" }); | |
routes.MapRoute( | |
name: "Default", | |
url: "{controller}/{action}/{id}", | |
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } | |
); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Web.Mvc; | |
using AuthDemo.Models; | |
using AuthDemo.ViewModels; | |
namespace AuthDemo.Controllers { | |
public class LoginController : Controller { | |
[HttpGet] | |
public ActionResult Index() { | |
LoginViewModel viewModel = new LoginViewModel(); | |
return View("Index", viewModel); | |
} | |
[HttpPost] | |
public ActionResult Logon(LoginRequest loginRequest) { | |
// The default model binder has already performed basic validation against the request, so we check against that | |
ActionResult result = null; | |
if (ModelState.IsValid) { | |
// Continue with login | |
// Perform some back-end user validation | |
bool isValidLogin = false; | |
// var isValidUser = this.MembershipRepository.ValidateUser(loginRequest); | |
// TODO: perform operations based on the boolean. For now, we pretend it's true | |
isValidLogin = true; | |
if (isValidLogin) { | |
Session["user"] = new UserModel() { | |
FirstName = "Clara", | |
LastName = "Oswald", | |
Email = "oswin@thetardis.com", | |
Id = 5 | |
}; | |
FormsAuthentication.SetAuthCookie(Session["user"].Email, false); | |
result = RedirectToRoute("MemberHome"); // Landing page for authenticated users. | |
} else { | |
// The user wasn't found in the repository | |
LoginViewModel viewModel = new LoginViewModel(); | |
viewModel.LogOnRequest = loginRequest; | |
viewModel.LogOnResponse.Successful = false; | |
viewModel.Messages.Add("Could not find the user specified."); | |
viewModel.LogOnRequest.Attempts += 1; | |
result = View("Index", viewModel); | |
} | |
} else { | |
// Login failed | |
LoginViewModel viewModel = new LoginViewModel(); // Build a new instance of the view model so we can show validation errors | |
viewModel.LogOnRequest = loginRequest; | |
viewModel.LogOnResponse.Successful = false; | |
viewModel.LogOnRequest.Attempts += 1; | |
viewModel.Messages.Add("Invalid login"); | |
result = View("Index", viewModel); | |
} | |
return result; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Web.Mvc; | |
using AuthDemo.Models; | |
namespace AuthDemo.Controllers { | |
public class MemberController : Controller { | |
// | |
// GET: /Member/ | |
[Authorize] | |
public ActionResult Index() { | |
UserModel user = (UserModel)this.Session["user"]; | |
return View("Index", user); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.ComponentModel.DataAnnotations; | |
namespace AuthDemo.Models { | |
public class LoginRequest { | |
private string _email; | |
private string _password; | |
private int _attempts; | |
public LoginRequest() { | |
this._email = string.Empty; | |
this._password = string.Empty; | |
this._attempts = 0; | |
} | |
public int Attempts { | |
get { | |
return this._attempts; | |
} set { | |
this._attempts = value; | |
} | |
} | |
[Required(AllowEmptyStrings = false, ErrorMessage = "Email address is required.")] | |
public string Email { | |
get { | |
return this._email; | |
} set { | |
this._email = value; | |
} | |
} | |
[Required(AllowEmptyStrings = false, ErrorMessage = "Password is required.")] | |
public string Password { | |
get { | |
return this._password; | |
} set { | |
this._password = value; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace AuthDemo.Models { | |
public class LoginResponse { | |
private bool _successful; | |
private int _userId; | |
public LoginResponse() { | |
this._successful = false; | |
this._userId = 0; | |
} | |
public bool Successful { | |
get { | |
return this._successful; | |
} set { | |
this._successful = value; | |
} | |
} | |
public int UserId { | |
get { | |
return this._userId; | |
} | |
set { | |
this._userId = value; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace AuthDemo.Models { | |
public class UserModel { | |
public string FirstName { get; set; } | |
public string LastName { get; set; } | |
public int Id { get; set; } | |
public string Email { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
namespace AuthDemo.ViewModels { | |
public abstract class BaseViewModel { | |
private bool _isAuthenticated; | |
private List<string> _messages; | |
protected BaseViewModel() { | |
this._isAuthenticated = false; | |
this._messages = new List<string>(); | |
} | |
public bool IsAuthenticated { | |
get { | |
return this._isAuthenticated; | |
} set { | |
this._isAuthenticated = value; | |
} | |
} | |
public List<string> Messages { | |
get { | |
return this._messages; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using AuthDemo.Models; | |
namespace AuthDemo.ViewModels { | |
public class LoginViewModel : BaseViewModel { | |
private LoginRequest _loginRequest; | |
private LoginResponse _loginResponse; | |
public LoginViewModel() : base() { | |
this._loginRequest = new LoginRequest(); | |
this._loginResponse = new LoginResponse(); | |
} | |
public LoginRequest LogOnRequest { | |
get { | |
return this._loginRequest; | |
} set { | |
if (!object.Equals(null, value)) { | |
this._loginRequest = value; | |
} | |
} | |
} | |
public LoginResponse LogOnResponse { | |
get { | |
return this._loginResponse; | |
} set { | |
if (!object.Equals(null, value)) { | |
this._loginResponse = value; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment