-
-
Save anonymous/3528ab17e13c6d5b2460 to your computer and use it in GitHub Desktop.
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
[HttpGet] | |
public ActionResult LogIn() | |
{ | |
return View(); | |
} | |
[HttpPost] | |
public ActionResult LogIn(LoginModel user, string returnUrl) | |
{ | |
if (ModelState.IsValid) | |
{ | |
if (user.IsValid(user.Email, user.Password)) | |
{ | |
FormsAuthentication.SetAuthCookie(user.Email, false); | |
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\")) | |
{ | |
return Redirect(returnUrl); | |
} | |
else | |
{ | |
return RedirectToAction("Index", "MyTemplate"); | |
} | |
} | |
else | |
{ | |
ModelState.AddModelError("", "Login Data is Incorrect!"); | |
} | |
} | |
return View(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
@model Project_v3.Models.UserModel | |
@{ | |
ViewBag.Title = "Log In | Open Airlines"; | |
Layout = "~/Views/Shared/_MyLayout.cshtml"; | |
} | |
<h2>Log In</h2> | |
@using (Html.BeginForm()) | |
{ | |
@Html.AntiForgeryToken() | |
@Html.ValidationSummary(true, "LogIn failed, Check your login details!") | |
<fieldset> | |
<legend>UserModel</legend> | |
<div class="editor-label"> | |
@Html.LabelFor(model => model.Email) | |
</div> | |
<div class="editor-field"> | |
@Html.EditorFor(model => model.Email) | |
@Html.ValidationMessageFor(model => model.Email) | |
</div> | |
<div class="editor-label"> | |
@Html.LabelFor(model => model.Password) | |
</div> | |
<div class="editor-field"> | |
@Html.EditorFor(model => model.Password) | |
@Html.ValidationMessageFor(model => model.Password) | |
</div> | |
<p> | |
<input type="submit" value="Log In" /> | |
</p> | |
</fieldset> | |
} | |
@section Scripts { | |
@Scripts.Render("~/bundles/jqueryval") | |
} |
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; | |
using System.Collections.Generic; | |
using System.ComponentModel.DataAnnotations; | |
using System.Linq; | |
using System.Web; | |
namespace Project_v3.Models | |
{ | |
public class LoginModel | |
{ | |
[Required] | |
[EmailAddress] | |
[StringLength(50)] | |
[Display(Name = "Email Address")] | |
public string Email { set; get; } | |
[Required] | |
[DataType(DataType.Password)] | |
[StringLength(20, MinimumLength = 6)] | |
[Display(Name = "Password")] | |
public string Password { set; get; } | |
public bool IsValid(string email, string password) | |
{ | |
var crypto = new SimpleCrypto.PBKDF2(); | |
bool IsValid = false; | |
using (var db = new MyMainDBEntities()) | |
{ | |
// retrieve the user according to the email entered | |
var user = db.SystemUsers.FirstOrDefault(u => u.Email == email); | |
if (user != null) | |
{ | |
// and the password is the same as what enterd | |
if (user.Password == crypto.Compute(password, user.PasswordSalt)) | |
{ | |
IsValid = true; | |
} | |
} | |
} | |
return IsValid; | |
} | |
} | |
} |
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
[HttpGet] | |
public ActionResult Registration() | |
{ | |
return View(); | |
} | |
[HttpPost] | |
public ActionResult Registration(UserModel user) | |
{ | |
if (ModelState.IsValid) | |
{ | |
using (var db = new MyMainDBEntities()) | |
{ | |
var crypto = new SimpleCrypto.PBKDF2(); | |
var encrypPass = crypto.Compute(user.Password); | |
var sysUser = db.SystemUsers.Create(); | |
sysUser.FirstName = user.FirstName; | |
sysUser.Surname = user.Surname; | |
sysUser.Age = user.Age; | |
sysUser.MobileNumber = user.MobileNumber; | |
sysUser.HomeNumber = user.HomeNumber; | |
sysUser.Email = user.Email; | |
sysUser.Password = encrypPass; | |
sysUser.PasswordSalt = crypto.Salt; | |
db.SystemUsers.Add(sysUser); | |
db.SaveChanges(); | |
return RedirectToAction("Index", "MyTemplate"); | |
} | |
} | |
else | |
{ | |
ModelState.AddModelError("", "Registration Data is Incorrect!"); | |
} | |
return View(); | |
} |
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
@model Project_v3.Models.UserModel | |
@{ | |
ViewBag.Title = "Register"; | |
Layout = "~/Views/Shared/_MyLayout.cshtml"; | |
} | |
<h2>Register</h2> | |
@using (Html.BeginForm()) | |
{ | |
@Html.AntiForgeryToken() | |
@Html.ValidationSummary(true, "Create User failed!, please check your details") | |
<fieldset> | |
<legend>Register Account</legend> | |
<div class="editor-label"> | |
@Html.LabelFor(model => model.FirstName) | |
</div> | |
<div class="editor-field"> | |
@Html.EditorFor(model => model.FirstName) | |
@Html.ValidationMessageFor(model => model.FirstName) | |
</div> | |
<div class="editor-label"> | |
@Html.LabelFor(model => model.Surname) | |
</div> | |
<div class="editor-field"> | |
@Html.EditorFor(model => model.Surname) | |
@Html.ValidationMessageFor(model => model.Surname) | |
</div> | |
<div class="editor-label"> | |
@Html.LabelFor(model => model.Age) | |
</div> | |
<div class="editor-field"> | |
@Html.EditorFor(model => model.Age) | |
@Html.ValidationMessageFor(model => model.Age) | |
</div> | |
<div class="editor-label"> | |
@Html.LabelFor(model => model.MobileNumber) | |
</div> | |
<div class="editor-field"> | |
@Html.EditorFor(model => model.MobileNumber) | |
@Html.ValidationMessageFor(model => model.MobileNumber) | |
</div> | |
<div class="editor-label"> | |
@Html.LabelFor(model => model.HomeNumber) | |
</div> | |
<div class="editor-field"> | |
@Html.EditorFor(model => model.HomeNumber) | |
@Html.ValidationMessageFor(model => model.HomeNumber) | |
</div> | |
<div class="editor-label"> | |
@Html.LabelFor(model => model.Email) | |
</div> | |
<div class="editor-field"> | |
@Html.EditorFor(model => model.Email) | |
@Html.ValidationMessageFor(model => model.Email) | |
</div> | |
<div class="editor-label"> | |
@Html.LabelFor(model => model.Password) | |
</div> | |
<div class="editor-field"> | |
@Html.EditorFor(model => model.Password) | |
@Html.ValidationMessageFor(model => model.Password) | |
</div> | |
<p> | |
<input type="submit" value="Create User" /> | |
</p> | |
</fieldset> | |
} | |
@section Scripts { | |
@Scripts.Render("~/bundles/jqueryval") | |
} | |
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; | |
using System.Collections.Generic; | |
using System.ComponentModel.DataAnnotations; | |
using System.Linq; | |
using System.Web; | |
namespace Project_v3.Models | |
{ | |
public class UserModel | |
{ | |
[Required] | |
[StringLength(50)] | |
[Display(Name = "First Name")] | |
public string FirstName { set; get; } | |
[Required] | |
[StringLength(50)] | |
[Display(Name = "Surname")] | |
public string Surname { set; get; } | |
[Required] | |
[Display(Name = "Age")] | |
public int Age { set; get; } | |
[Required(ErrorMessage = "Mobile Number is required")] | |
[DataType(DataType.PhoneNumber)] | |
[RegularExpression(@"^[0-9]{5} [0-9]{6}$", ErrorMessage = "Must be 5 digits, space and then 6 digits.")] | |
[Display(Name = "Mobile Number")] | |
public string MobileNumber { set; get; } | |
[Required(ErrorMessage = "Home Number is required")] | |
[RegularExpression(@"^[0-9]{5} [0-9]{6}$", ErrorMessage = "Must be 5 digits, space and then 6 digits.")] | |
[DataType(DataType.PhoneNumber)] | |
[Display(Name = "Home Number")] | |
public string HomeNumber { set; get; } | |
[Required] | |
[EmailAddress] | |
[StringLength(50)] | |
[Display(Name = "Email Address")] | |
public string Email { set; get; } | |
[Required] | |
[DataType(DataType.Password)] | |
[StringLength(20, MinimumLength = 6)] | |
[Display(Name = "Password")] | |
public string Password { set; get; } | |
public bool IsValid(string email, string password) | |
{ | |
var crypto = new SimpleCrypto.PBKDF2(); | |
bool IsValid = false; | |
using (var db = new MyMainDBEntities()) | |
{ | |
// retrieve the user according to the email entered | |
var user = db.SystemUsers.FirstOrDefault(u => u.Email == email); | |
if (user != null) | |
{ | |
// and the password is the same as what enterd | |
if (user.Password == crypto.Compute(password, user.PasswordSalt)) | |
{ | |
IsValid = true; | |
} | |
} | |
} | |
return IsValid; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment