This repository has been archived by the owner. It is now read-only.
Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Merge pull request #67 from brockallen/2fa
add two factor authentication support
- Loading branch information
Showing
33 changed files
with
1,194 additions
and
90 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Web.Mvc; | ||
using BrockAllen.MembershipReboot.Mvc.Areas.UserAccount.Models; | ||
|
||
namespace BrockAllen.MembershipReboot.Mvc.Areas.UserAccount.Controllers | ||
{ | ||
[Authorize] | ||
public class ChangeMobileController : Controller | ||
{ | ||
UserAccountService userAccountService; | ||
ClaimsBasedAuthenticationService authSvc; | ||
|
||
public ChangeMobileController(UserAccountService userAccountService, ClaimsBasedAuthenticationService authSvc) | ||
{ | ||
this.userAccountService = userAccountService; | ||
this.authSvc = authSvc; | ||
} | ||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
if (disposing) | ||
{ | ||
this.userAccountService.TryDispose(); | ||
this.userAccountService = null; | ||
} | ||
base.Dispose(disposing); | ||
} | ||
|
||
public ActionResult Index() | ||
{ | ||
return View("Index"); | ||
} | ||
|
||
[HttpPost] | ||
[ValidateAntiForgeryToken] | ||
public ActionResult Index(string button, ChangeMobileRequestInputModel model) | ||
{ | ||
if (button == "change") | ||
{ | ||
if (ModelState.IsValid) | ||
{ | ||
try | ||
{ | ||
if (this.userAccountService.ChangeMobilePhoneRequest(User.Identity.Name, model.NewMobilePhone)) | ||
{ | ||
return View("ChangeRequestSuccess", (object)model.NewMobilePhone); | ||
} | ||
|
||
ModelState.AddModelError("", "Error requesting mobile phone number change."); | ||
} | ||
catch (ValidationException ex) | ||
{ | ||
ModelState.AddModelError("", ex.Message); | ||
} | ||
} | ||
} | ||
|
||
if (button == "remove") | ||
{ | ||
if (this.userAccountService.RemoveMobilePhone(User.GetUserID())) | ||
{ | ||
return View("Success"); | ||
} | ||
else | ||
{ | ||
ModelState.AddModelError("", "Error removing the mobile phone"); | ||
} | ||
} | ||
|
||
return View("Index", model); | ||
} | ||
|
||
[HttpPost] | ||
[ValidateAntiForgeryToken] | ||
public ActionResult Confirm(ChangeMobileFromCodeInputModel model) | ||
{ | ||
if (ModelState.IsValid) | ||
{ | ||
try | ||
{ | ||
if (this.userAccountService.ChangeMobileFromCode(this.User.Identity.Name, model.Code)) | ||
{ | ||
// since the mobile had changed, reissue the | ||
// cookie with the updated claims | ||
authSvc.SignIn(this.User.Identity.Name); | ||
|
||
return View("Success"); | ||
} | ||
|
||
ModelState.AddModelError("", "Error confirming code."); | ||
} | ||
catch (ValidationException ex) | ||
{ | ||
ModelState.AddModelError("", ex.Message); | ||
} | ||
} | ||
|
||
return View("Confirm", model); | ||
} | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Web; | ||
using System.Web.Mvc; | ||
|
||
namespace BrockAllen.MembershipReboot.Mvc.Areas.UserAccount.Controllers | ||
{ | ||
public class TwoFactorAuthController : Controller | ||
{ | ||
UserAccountService userAccountService; | ||
|
||
public TwoFactorAuthController(UserAccountService userAccountService) | ||
{ | ||
this.userAccountService = userAccountService; | ||
} | ||
|
||
public ActionResult Index() | ||
{ | ||
var acct = userAccountService.GetByUsername(this.User.Identity.Name); | ||
return View(acct); | ||
} | ||
|
||
[HttpPost] | ||
[ValidateAntiForgeryToken] | ||
public ActionResult Enable() | ||
{ | ||
if (this.userAccountService.EnableTwoFactorAuthentication(this.User.GetUserID())) | ||
{ | ||
return View("Success"); | ||
} | ||
|
||
return View("Fail"); | ||
} | ||
|
||
[HttpPost] | ||
[ValidateAntiForgeryToken] | ||
public ActionResult Disable() | ||
{ | ||
this.userAccountService.DisableTwoFactorAuthentication(this.User.GetUserID()); | ||
return View("Success"); | ||
} | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Web.Mvc; | ||
|
||
namespace BrockAllen.MembershipReboot.Mvc.Areas.UserAccount.Models | ||
{ | ||
public class ChangeEmailFromKeyInputModel | ||
{ | ||
[Required] | ||
[DataType(DataType.Password)] | ||
public string Password { get; set; } | ||
|
||
[Required] | ||
[EmailAddress] | ||
public string NewEmail { get; set; } | ||
|
||
[HiddenInput] | ||
public string Key { 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace BrockAllen.MembershipReboot.Mvc.Areas.UserAccount.Models | ||
{ | ||
public class ChangeMobileRequestInputModel | ||
{ | ||
//[Required] | ||
public string NewMobilePhone { 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
Oops, something went wrong.