Last active
March 27, 2016 05:23
-
-
Save NightOwl888/693879b0e19fa4210b43 to your computer and use it in GitHub Desktop.
MVC 5 Default Template Changes Required for Dependency Inejction
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.Globalization; | |
using System.Linq; | |
using System.Security.Claims; | |
using System.Threading.Tasks; | |
using System.Web; | |
using System.Web.Mvc; | |
using Microsoft.AspNet.Identity; | |
using Microsoft.AspNet.Identity.Owin; | |
using Microsoft.Owin.Security; | |
using NinjectMVC5Owin.Models; | |
namespace NinjectMVC5Owin.Controllers | |
{ | |
[Authorize] | |
public class AccountController : Controller | |
{ | |
private readonly ApplicationSignInManager signInManager; | |
private readonly ApplicationUserManager userManager; | |
private readonly IAuthenticationManager authenticationManager; | |
public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager, IAuthenticationManager authenticationManager) | |
{ | |
if (userManager == null) | |
throw new ArgumentNullException("userManager"); | |
if (signInManager == null) | |
throw new ArgumentNullException("signInManager"); | |
if (authenticationManager == null) | |
throw new ArgumentNullException("authenticationManager"); | |
this.userManager = userManager; | |
this.signInManager = signInManager; | |
this.authenticationManager = authenticationManager; | |
} | |
// | |
// GET: /Account/Login | |
[AllowAnonymous] | |
public ActionResult Login(string returnUrl) | |
{ | |
ViewBag.ReturnUrl = returnUrl; | |
return View(); | |
} | |
// | |
// POST: /Account/Login | |
[HttpPost] | |
[AllowAnonymous] | |
[ValidateAntiForgeryToken] | |
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) | |
{ | |
if (!ModelState.IsValid) | |
{ | |
return View(model); | |
} | |
// This doesn't count login failures towards account lockout | |
// To enable password failures to trigger account lockout, change to shouldLockout: true | |
var result = await this.signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false); | |
switch (result) | |
{ | |
case SignInStatus.Success: | |
return RedirectToLocal(returnUrl); | |
case SignInStatus.LockedOut: | |
return View("Lockout"); | |
case SignInStatus.RequiresVerification: | |
return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe }); | |
case SignInStatus.Failure: | |
default: | |
ModelState.AddModelError("", "Invalid login attempt."); | |
return View(model); | |
} | |
} | |
// | |
// GET: /Account/VerifyCode | |
[AllowAnonymous] | |
public async Task<ActionResult> VerifyCode(string provider, string returnUrl, bool rememberMe) | |
{ | |
// Require that the user has already logged in via username/password or external login | |
if (!await this.signInManager.HasBeenVerifiedAsync()) | |
{ | |
return View("Error"); | |
} | |
return View(new VerifyCodeViewModel { Provider = provider, ReturnUrl = returnUrl, RememberMe = rememberMe }); | |
} | |
// | |
// POST: /Account/VerifyCode | |
[HttpPost] | |
[AllowAnonymous] | |
[ValidateAntiForgeryToken] | |
public async Task<ActionResult> VerifyCode(VerifyCodeViewModel model) | |
{ | |
if (!ModelState.IsValid) | |
{ | |
return View(model); | |
} | |
// The following code protects for brute force attacks against the two factor codes. | |
// If a user enters incorrect codes for a specified amount of time then the user account | |
// will be locked out for a specified amount of time. | |
// You can configure the account lockout settings in IdentityConfig | |
var result = await this.signInManager.TwoFactorSignInAsync(model.Provider, model.Code, isPersistent: model.RememberMe, rememberBrowser: model.RememberBrowser); | |
switch (result) | |
{ | |
case SignInStatus.Success: | |
return RedirectToLocal(model.ReturnUrl); | |
case SignInStatus.LockedOut: | |
return View("Lockout"); | |
case SignInStatus.Failure: | |
default: | |
ModelState.AddModelError("", "Invalid code."); | |
return View(model); | |
} | |
} | |
// | |
// GET: /Account/Register | |
[AllowAnonymous] | |
public ActionResult Register() | |
{ | |
return View(); | |
} | |
// | |
// POST: /Account/Register | |
[HttpPost] | |
[AllowAnonymous] | |
[ValidateAntiForgeryToken] | |
public async Task<ActionResult> Register(RegisterViewModel model) | |
{ | |
if (ModelState.IsValid) | |
{ | |
var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; | |
var result = await this.userManager.CreateAsync(user, model.Password); | |
if (result.Succeeded) | |
{ | |
await this.signInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false); | |
// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 | |
// Send an email with this link | |
// string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); | |
// var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); | |
// await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); | |
return RedirectToAction("Index", "Home"); | |
} | |
AddErrors(result); | |
} | |
// If we got this far, something failed, redisplay form | |
return View(model); | |
} | |
// | |
// GET: /Account/ConfirmEmail | |
[AllowAnonymous] | |
public async Task<ActionResult> ConfirmEmail(string userId, string code) | |
{ | |
if (userId == null || code == null) | |
{ | |
return View("Error"); | |
} | |
var result = await this.userManager.ConfirmEmailAsync(userId, code); | |
return View(result.Succeeded ? "ConfirmEmail" : "Error"); | |
} | |
// | |
// GET: /Account/ForgotPassword | |
[AllowAnonymous] | |
public ActionResult ForgotPassword() | |
{ | |
return View(); | |
} | |
// | |
// POST: /Account/ForgotPassword | |
[HttpPost] | |
[AllowAnonymous] | |
[ValidateAntiForgeryToken] | |
public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model) | |
{ | |
if (ModelState.IsValid) | |
{ | |
var user = await this.userManager.FindByNameAsync(model.Email); | |
if (user == null || !(await this.userManager.IsEmailConfirmedAsync(user.Id))) | |
{ | |
// Don't reveal that the user does not exist or is not confirmed | |
return View("ForgotPasswordConfirmation"); | |
} | |
// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 | |
// Send an email with this link | |
// string code = await this.userManager.GeneratePasswordResetTokenAsync(user.Id); | |
// var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); | |
// await this.userManager.SendEmailAsync(user.Id, "Reset Password", "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>"); | |
// return RedirectToAction("ForgotPasswordConfirmation", "Account"); | |
} | |
// If we got this far, something failed, redisplay form | |
return View(model); | |
} | |
// | |
// GET: /Account/ForgotPasswordConfirmation | |
[AllowAnonymous] | |
public ActionResult ForgotPasswordConfirmation() | |
{ | |
return View(); | |
} | |
// | |
// GET: /Account/ResetPassword | |
[AllowAnonymous] | |
public ActionResult ResetPassword(string code) | |
{ | |
return code == null ? View("Error") : View(); | |
} | |
// | |
// POST: /Account/ResetPassword | |
[HttpPost] | |
[AllowAnonymous] | |
[ValidateAntiForgeryToken] | |
public async Task<ActionResult> ResetPassword(ResetPasswordViewModel model) | |
{ | |
if (!ModelState.IsValid) | |
{ | |
return View(model); | |
} | |
var user = await this.userManager.FindByNameAsync(model.Email); | |
if (user == null) | |
{ | |
// Don't reveal that the user does not exist | |
return RedirectToAction("ResetPasswordConfirmation", "Account"); | |
} | |
var result = await this.userManager.ResetPasswordAsync(user.Id, model.Code, model.Password); | |
if (result.Succeeded) | |
{ | |
return RedirectToAction("ResetPasswordConfirmation", "Account"); | |
} | |
AddErrors(result); | |
return View(); | |
} | |
// | |
// GET: /Account/ResetPasswordConfirmation | |
[AllowAnonymous] | |
public ActionResult ResetPasswordConfirmation() | |
{ | |
return View(); | |
} | |
// | |
// POST: /Account/ExternalLogin | |
[HttpPost] | |
[AllowAnonymous] | |
[ValidateAntiForgeryToken] | |
public ActionResult ExternalLogin(string provider, string returnUrl) | |
{ | |
// Request a redirect to the external login provider | |
return new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl })); | |
} | |
// | |
// GET: /Account/SendCode | |
[AllowAnonymous] | |
public async Task<ActionResult> SendCode(string returnUrl, bool rememberMe) | |
{ | |
var userId = await this.signInManager.GetVerifiedUserIdAsync(); | |
if (userId == null) | |
{ | |
return View("Error"); | |
} | |
var userFactors = await this.userManager.GetValidTwoFactorProvidersAsync(userId); | |
var factorOptions = userFactors.Select(purpose => new SelectListItem { Text = purpose, Value = purpose }).ToList(); | |
return View(new SendCodeViewModel { Providers = factorOptions, ReturnUrl = returnUrl, RememberMe = rememberMe }); | |
} | |
// | |
// POST: /Account/SendCode | |
[HttpPost] | |
[AllowAnonymous] | |
[ValidateAntiForgeryToken] | |
public async Task<ActionResult> SendCode(SendCodeViewModel model) | |
{ | |
if (!ModelState.IsValid) | |
{ | |
return View(); | |
} | |
// Generate the token and send it | |
if (!await this.signInManager.SendTwoFactorCodeAsync(model.SelectedProvider)) | |
{ | |
return View("Error"); | |
} | |
return RedirectToAction("VerifyCode", new { Provider = model.SelectedProvider, ReturnUrl = model.ReturnUrl, RememberMe = model.RememberMe }); | |
} | |
// | |
// GET: /Account/ExternalLoginCallback | |
[AllowAnonymous] | |
public async Task<ActionResult> ExternalLoginCallback(string returnUrl) | |
{ | |
var loginInfo = await this.authenticationManager.GetExternalLoginInfoAsync(); | |
if (loginInfo == null) | |
{ | |
return RedirectToAction("Login"); | |
} | |
// Sign in the user with this external login provider if the user already has a login | |
var result = await this.signInManager.ExternalSignInAsync(loginInfo, isPersistent: false); | |
switch (result) | |
{ | |
case SignInStatus.Success: | |
return RedirectToLocal(returnUrl); | |
case SignInStatus.LockedOut: | |
return View("Lockout"); | |
case SignInStatus.RequiresVerification: | |
return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = false }); | |
case SignInStatus.Failure: | |
default: | |
// If the user does not have an account, then prompt the user to create an account | |
ViewBag.ReturnUrl = returnUrl; | |
ViewBag.LoginProvider = loginInfo.Login.LoginProvider; | |
return View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel { Email = loginInfo.Email }); | |
} | |
} | |
// | |
// POST: /Account/ExternalLoginConfirmation | |
[HttpPost] | |
[AllowAnonymous] | |
[ValidateAntiForgeryToken] | |
public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl) | |
{ | |
if (User.Identity.IsAuthenticated) | |
{ | |
return RedirectToAction("Index", "Manage"); | |
} | |
if (ModelState.IsValid) | |
{ | |
// Get the information about the user from the external login provider | |
var info = await this.authenticationManager.GetExternalLoginInfoAsync(); | |
if (info == null) | |
{ | |
return View("ExternalLoginFailure"); | |
} | |
var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; | |
var result = await this.userManager.CreateAsync(user); | |
if (result.Succeeded) | |
{ | |
result = await this.userManager.AddLoginAsync(user.Id, info.Login); | |
if (result.Succeeded) | |
{ | |
await this.signInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); | |
return RedirectToLocal(returnUrl); | |
} | |
} | |
AddErrors(result); | |
} | |
ViewBag.ReturnUrl = returnUrl; | |
return View(model); | |
} | |
// | |
// POST: /Account/LogOff | |
[HttpPost] | |
[ValidateAntiForgeryToken] | |
public ActionResult LogOff() | |
{ | |
this.authenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie); | |
return RedirectToAction("Index", "Home"); | |
} | |
// | |
// GET: /Account/ExternalLoginFailure | |
[AllowAnonymous] | |
public ActionResult ExternalLoginFailure() | |
{ | |
return View(); | |
} | |
#region Helpers | |
// Used for XSRF protection when adding external logins | |
private const string XsrfKey = "XsrfId"; | |
private void AddErrors(IdentityResult result) | |
{ | |
foreach (var error in result.Errors) | |
{ | |
ModelState.AddModelError("", error); | |
} | |
} | |
private ActionResult RedirectToLocal(string returnUrl) | |
{ | |
if (Url.IsLocalUrl(returnUrl)) | |
{ | |
return Redirect(returnUrl); | |
} | |
return RedirectToAction("Index", "Home"); | |
} | |
internal class ChallengeResult : HttpUnauthorizedResult | |
{ | |
public ChallengeResult(string provider, string redirectUri) | |
: this(provider, redirectUri, null) | |
{ | |
} | |
public ChallengeResult(string provider, string redirectUri, string userId) | |
{ | |
LoginProvider = provider; | |
RedirectUri = redirectUri; | |
UserId = userId; | |
} | |
public string LoginProvider { get; set; } | |
public string RedirectUri { get; set; } | |
public string UserId { get; set; } | |
public override void ExecuteResult(ControllerContext context) | |
{ | |
var properties = new AuthenticationProperties { RedirectUri = RedirectUri }; | |
if (UserId != null) | |
{ | |
properties.Dictionary[XsrfKey] = UserId; | |
} | |
context.HttpContext.GetOwinContext().Authentication.Challenge(properties, LoginProvider); | |
} | |
} | |
#endregion | |
} | |
} |
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.Data.Entity; | |
using System.Linq; | |
using System.Security.Claims; | |
using System.Threading.Tasks; | |
using System.Web; | |
using Microsoft.AspNet.Identity; | |
using Microsoft.AspNet.Identity.EntityFramework; | |
using Microsoft.AspNet.Identity.Owin; | |
using Microsoft.Owin; | |
using Microsoft.Owin.Security; | |
using Microsoft.Owin.Security.DataProtection; | |
using NinjectMVC5Owin.Models; | |
namespace NinjectMVC5Owin | |
{ | |
public interface IMessageService : IIdentityMessageService | |
{ | |
bool AppliesTo(IEnumerable<string> providers); | |
} | |
public interface IMessageStrategy | |
{ | |
IEnumerable<IMessageService> GetMessageServices(string provider); | |
IEnumerable<IMessageService> GetMessageServices(IEnumerable<string> providers); | |
void Send(IdentityMessage message, string provider); | |
void Send(IdentityMessage message, IEnumerable<string> providers); | |
} | |
public class EmailService : IMessageService | |
{ | |
public Task SendAsync(IdentityMessage message) | |
{ | |
// Plug in your email service here to send an email. | |
return Task.FromResult(0); | |
} | |
public bool AppliesTo(IEnumerable<string> providers) | |
{ | |
return providers.Contains("email"); | |
} | |
} | |
public class SmsService : IMessageService | |
{ | |
public Task SendAsync(IdentityMessage message) | |
{ | |
// Plug in your SMS service here to send a text message. | |
return Task.FromResult(0); | |
} | |
public bool AppliesTo(IEnumerable<string> providers) | |
{ | |
return providers.Contains("sms"); | |
} | |
} | |
public class MessageStrategy : IMessageStrategy | |
{ | |
private readonly IMessageService[] messageServices; | |
public MessageStrategy(IMessageService[] messageServices) | |
{ | |
if (messageServices == null) | |
throw new ArgumentNullException("messageServices"); | |
this.messageServices = messageServices; | |
} | |
public IEnumerable<IMessageService> GetMessageServices(string provider) | |
{ | |
return this.GetMessageServices(this.SplitProviders(provider)); | |
} | |
public IEnumerable<IMessageService> GetMessageServices(IEnumerable<string> providers) | |
{ | |
return this.messageServices.Where(p => p.AppliesTo(providers)); | |
} | |
public void Send(IdentityMessage message, string provider) | |
{ | |
this.Send(message, this.SplitProviders(provider)); | |
} | |
public void Send(IdentityMessage message, IEnumerable<string> providers) | |
{ | |
var messageServices = this.GetMessageServices(providers); | |
foreach (IMessageService messageService in messageServices) | |
{ | |
messageService.Send(message); | |
} | |
} | |
private IEnumerable<string> SplitProviders(string provider) | |
{ | |
return provider.Split(',').Select(p => p.ToLower().Trim()).ToArray(); | |
} | |
} | |
// Configure the application user manager used in this application. UserManager is defined in ASP.NET Identity and is used by the application. | |
public class ApplicationUserManager : UserManager<ApplicationUser> | |
{ | |
public ApplicationUserManager(IUserStore<ApplicationUser> store, IDataProtectionProvider dataProtectionProvider, IMessageStrategy messageStrategy) | |
: base(store) | |
{ | |
if (dataProtectionProvider == null) | |
throw new ArgumentNullException("dataProtectionProvider"); | |
UserValidator = new UserValidator<ApplicationUser>(this) | |
{ | |
AllowOnlyAlphanumericUserNames = false, | |
RequireUniqueEmail = true | |
}; | |
// Configure validation logic for passwords | |
PasswordValidator = new PasswordValidator | |
{ | |
RequiredLength = 6, | |
RequireNonLetterOrDigit = true, | |
RequireDigit = true, | |
RequireLowercase = true, | |
RequireUppercase = true, | |
}; | |
// Configure user lockout defaults | |
UserLockoutEnabledByDefault = true; | |
DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5); | |
MaxFailedAccessAttemptsBeforeLockout = 5; | |
// Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user | |
// You can write your own provider and plug it in here. | |
RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<ApplicationUser> | |
{ | |
MessageFormat = "Your security code is {0}" | |
}); | |
RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<ApplicationUser> | |
{ | |
Subject = "Security Code", | |
BodyFormat = "Your security code is {0}" | |
}); | |
EmailService = messageStrategy.GetMessageServices("email").First(); | |
SmsService = messageStrategy.GetMessageServices("sms").First(); | |
UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity")); | |
} | |
} | |
// Configure the application sign-in manager which is used in this application. | |
public class ApplicationSignInManager : SignInManager<ApplicationUser, string> | |
{ | |
public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager) | |
: base(userManager, authenticationManager) | |
{ | |
} | |
public override Task<ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user) | |
{ | |
return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager); | |
} | |
} | |
// From: https://www.talksharp.com/configuring-autofac-to-work-with-the-aspnet-identity-framework-in-mvc-5 | |
public class ApplicationUserStore : UserStore<ApplicationUser> | |
{ | |
public ApplicationUserStore(ApplicationDbContext context) | |
: base(context) | |
{ | |
} | |
} | |
} |
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.Data.Entity; | |
using System.Security.Claims; | |
using System.Threading.Tasks; | |
using Microsoft.AspNet.Identity; | |
using Microsoft.AspNet.Identity.EntityFramework; | |
namespace NinjectMVC5Owin.Models | |
{ | |
// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more. | |
public class ApplicationUser : IdentityUser | |
{ | |
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) | |
{ | |
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType | |
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); | |
// Add custom user claims here | |
return userIdentity; | |
} | |
} | |
public class ApplicationDbContext : IdentityDbContext<ApplicationUser> | |
{ | |
public ApplicationDbContext() | |
: base("DefaultConnection", throwIfV1Schema: false) | |
{ | |
} | |
} | |
} |
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.Linq; | |
using System.Threading.Tasks; | |
using System.Web; | |
using System.Web.Mvc; | |
using Microsoft.AspNet.Identity; | |
using Microsoft.AspNet.Identity.Owin; | |
using Microsoft.Owin.Security; | |
using NinjectMVC5Owin.Models; | |
namespace NinjectMVC5Owin.Controllers | |
{ | |
[Authorize] | |
public class ManageController : Controller | |
{ | |
private readonly ApplicationSignInManager signInManager; | |
private readonly ApplicationUserManager userManager; | |
private readonly IAuthenticationManager authenticationManager; | |
public ManageController(ApplicationUserManager userManager, ApplicationSignInManager signInManager, IAuthenticationManager authenticationManager) | |
{ | |
if (userManager == null) | |
throw new ArgumentNullException("userManager"); | |
if (signInManager == null) | |
throw new ArgumentNullException("signInManager"); | |
if (authenticationManager == null) | |
throw new ArgumentNullException("authenticationManager"); | |
this.userManager = userManager; | |
this.signInManager = signInManager; | |
this.authenticationManager = authenticationManager; | |
} | |
// | |
// GET: /Manage/Index | |
public async Task<ActionResult> Index(ManageMessageId? message) | |
{ | |
ViewBag.StatusMessage = | |
message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed." | |
: message == ManageMessageId.SetPasswordSuccess ? "Your password has been set." | |
: message == ManageMessageId.SetTwoFactorSuccess ? "Your two-factor authentication provider has been set." | |
: message == ManageMessageId.Error ? "An error has occurred." | |
: message == ManageMessageId.AddPhoneSuccess ? "Your phone number was added." | |
: message == ManageMessageId.RemovePhoneSuccess ? "Your phone number was removed." | |
: ""; | |
var userId = User.Identity.GetUserId(); | |
var model = new IndexViewModel | |
{ | |
HasPassword = HasPassword(), | |
PhoneNumber = await this.userManager.GetPhoneNumberAsync(userId), | |
TwoFactor = await this.userManager.GetTwoFactorEnabledAsync(userId), | |
Logins = await this.userManager.GetLoginsAsync(userId), | |
BrowserRemembered = await this.authenticationManager.TwoFactorBrowserRememberedAsync(userId) | |
}; | |
return View(model); | |
} | |
// | |
// POST: /Manage/RemoveLogin | |
[HttpPost] | |
[ValidateAntiForgeryToken] | |
public async Task<ActionResult> RemoveLogin(string loginProvider, string providerKey) | |
{ | |
ManageMessageId? message; | |
var result = await this.userManager.RemoveLoginAsync(User.Identity.GetUserId(), new UserLoginInfo(loginProvider, providerKey)); | |
if (result.Succeeded) | |
{ | |
var user = await this.userManager.FindByIdAsync(User.Identity.GetUserId()); | |
if (user != null) | |
{ | |
await this.signInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); | |
} | |
message = ManageMessageId.RemoveLoginSuccess; | |
} | |
else | |
{ | |
message = ManageMessageId.Error; | |
} | |
return RedirectToAction("ManageLogins", new { Message = message }); | |
} | |
// | |
// GET: /Manage/AddPhoneNumber | |
public ActionResult AddPhoneNumber() | |
{ | |
return View(); | |
} | |
// | |
// POST: /Manage/AddPhoneNumber | |
[HttpPost] | |
[ValidateAntiForgeryToken] | |
public async Task<ActionResult> AddPhoneNumber(AddPhoneNumberViewModel model) | |
{ | |
if (!ModelState.IsValid) | |
{ | |
return View(model); | |
} | |
// Generate the token and send it | |
var code = await this.userManager.GenerateChangePhoneNumberTokenAsync(User.Identity.GetUserId(), model.Number); | |
if (this.userManager.SmsService != null) | |
{ | |
var message = new IdentityMessage | |
{ | |
Destination = model.Number, | |
Body = "Your security code is: " + code | |
}; | |
await this.userManager.SmsService.SendAsync(message); | |
} | |
return RedirectToAction("VerifyPhoneNumber", new { PhoneNumber = model.Number }); | |
} | |
// | |
// POST: /Manage/EnableTwoFactorAuthentication | |
[HttpPost] | |
[ValidateAntiForgeryToken] | |
public async Task<ActionResult> EnableTwoFactorAuthentication() | |
{ | |
await this.userManager.SetTwoFactorEnabledAsync(User.Identity.GetUserId(), true); | |
var user = await this.userManager.FindByIdAsync(User.Identity.GetUserId()); | |
if (user != null) | |
{ | |
await this.signInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); | |
} | |
return RedirectToAction("Index", "Manage"); | |
} | |
// | |
// POST: /Manage/DisableTwoFactorAuthentication | |
[HttpPost] | |
[ValidateAntiForgeryToken] | |
public async Task<ActionResult> DisableTwoFactorAuthentication() | |
{ | |
await this.userManager.SetTwoFactorEnabledAsync(User.Identity.GetUserId(), false); | |
var user = await this.userManager.FindByIdAsync(User.Identity.GetUserId()); | |
if (user != null) | |
{ | |
await this.signInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); | |
} | |
return RedirectToAction("Index", "Manage"); | |
} | |
// | |
// GET: /Manage/VerifyPhoneNumber | |
public async Task<ActionResult> VerifyPhoneNumber(string phoneNumber) | |
{ | |
var code = await this.userManager.GenerateChangePhoneNumberTokenAsync(User.Identity.GetUserId(), phoneNumber); | |
// Send an SMS through the SMS provider to verify the phone number | |
return phoneNumber == null ? View("Error") : View(new VerifyPhoneNumberViewModel { PhoneNumber = phoneNumber }); | |
} | |
// | |
// POST: /Manage/VerifyPhoneNumber | |
[HttpPost] | |
[ValidateAntiForgeryToken] | |
public async Task<ActionResult> VerifyPhoneNumber(VerifyPhoneNumberViewModel model) | |
{ | |
if (!ModelState.IsValid) | |
{ | |
return View(model); | |
} | |
var result = await this.userManager.ChangePhoneNumberAsync(User.Identity.GetUserId(), model.PhoneNumber, model.Code); | |
if (result.Succeeded) | |
{ | |
var user = await this.userManager.FindByIdAsync(User.Identity.GetUserId()); | |
if (user != null) | |
{ | |
await this.signInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); | |
} | |
return RedirectToAction("Index", new { Message = ManageMessageId.AddPhoneSuccess }); | |
} | |
// If we got this far, something failed, redisplay form | |
ModelState.AddModelError("", "Failed to verify phone"); | |
return View(model); | |
} | |
// | |
// GET: /Manage/RemovePhoneNumber | |
public async Task<ActionResult> RemovePhoneNumber() | |
{ | |
var result = await this.userManager.SetPhoneNumberAsync(User.Identity.GetUserId(), null); | |
if (!result.Succeeded) | |
{ | |
return RedirectToAction("Index", new { Message = ManageMessageId.Error }); | |
} | |
var user = await this.userManager.FindByIdAsync(User.Identity.GetUserId()); | |
if (user != null) | |
{ | |
await this.signInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); | |
} | |
return RedirectToAction("Index", new { Message = ManageMessageId.RemovePhoneSuccess }); | |
} | |
// | |
// GET: /Manage/ChangePassword | |
public ActionResult ChangePassword() | |
{ | |
return View(); | |
} | |
// | |
// POST: /Manage/ChangePassword | |
[HttpPost] | |
[ValidateAntiForgeryToken] | |
public async Task<ActionResult> ChangePassword(ChangePasswordViewModel model) | |
{ | |
if (!ModelState.IsValid) | |
{ | |
return View(model); | |
} | |
var result = await this.userManager.ChangePasswordAsync(User.Identity.GetUserId(), model.OldPassword, model.NewPassword); | |
if (result.Succeeded) | |
{ | |
var user = await this.userManager.FindByIdAsync(User.Identity.GetUserId()); | |
if (user != null) | |
{ | |
await this.signInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); | |
} | |
return RedirectToAction("Index", new { Message = ManageMessageId.ChangePasswordSuccess }); | |
} | |
AddErrors(result); | |
return View(model); | |
} | |
// | |
// GET: /Manage/SetPassword | |
public ActionResult SetPassword() | |
{ | |
return View(); | |
} | |
// | |
// POST: /Manage/SetPassword | |
[HttpPost] | |
[ValidateAntiForgeryToken] | |
public async Task<ActionResult> SetPassword(SetPasswordViewModel model) | |
{ | |
if (ModelState.IsValid) | |
{ | |
var result = await this.userManager.AddPasswordAsync(User.Identity.GetUserId(), model.NewPassword); | |
if (result.Succeeded) | |
{ | |
var user = await this.userManager.FindByIdAsync(User.Identity.GetUserId()); | |
if (user != null) | |
{ | |
await this.signInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); | |
} | |
return RedirectToAction("Index", new { Message = ManageMessageId.SetPasswordSuccess }); | |
} | |
AddErrors(result); | |
} | |
// If we got this far, something failed, redisplay form | |
return View(model); | |
} | |
// | |
// GET: /Manage/ManageLogins | |
public async Task<ActionResult> ManageLogins(ManageMessageId? message) | |
{ | |
ViewBag.StatusMessage = | |
message == ManageMessageId.RemoveLoginSuccess ? "The external login was removed." | |
: message == ManageMessageId.Error ? "An error has occurred." | |
: ""; | |
var user = await this.userManager.FindByIdAsync(User.Identity.GetUserId()); | |
if (user == null) | |
{ | |
return View("Error"); | |
} | |
var userLogins = await this.userManager.GetLoginsAsync(User.Identity.GetUserId()); | |
var otherLogins = this.authenticationManager.GetExternalAuthenticationTypes().Where(auth => userLogins.All(ul => auth.AuthenticationType != ul.LoginProvider)).ToList(); | |
ViewBag.ShowRemoveButton = user.PasswordHash != null || userLogins.Count > 1; | |
return View(new ManageLoginsViewModel | |
{ | |
CurrentLogins = userLogins, | |
OtherLogins = otherLogins | |
}); | |
} | |
// | |
// POST: /Manage/LinkLogin | |
[HttpPost] | |
[ValidateAntiForgeryToken] | |
public ActionResult LinkLogin(string provider) | |
{ | |
// Request a redirect to the external login provider to link a login for the current user | |
return new AccountController.ChallengeResult(provider, Url.Action("LinkLoginCallback", "Manage"), User.Identity.GetUserId()); | |
} | |
// | |
// GET: /Manage/LinkLoginCallback | |
public async Task<ActionResult> LinkLoginCallback() | |
{ | |
var loginInfo = await this.authenticationManager.GetExternalLoginInfoAsync(XsrfKey, User.Identity.GetUserId()); | |
if (loginInfo == null) | |
{ | |
return RedirectToAction("ManageLogins", new { Message = ManageMessageId.Error }); | |
} | |
var result = await this.userManager.AddLoginAsync(User.Identity.GetUserId(), loginInfo.Login); | |
return result.Succeeded ? RedirectToAction("ManageLogins") : RedirectToAction("ManageLogins", new { Message = ManageMessageId.Error }); | |
} | |
#region Helpers | |
// Used for XSRF protection when adding external logins | |
private const string XsrfKey = "XsrfId"; | |
private void AddErrors(IdentityResult result) | |
{ | |
foreach (var error in result.Errors) | |
{ | |
ModelState.AddModelError("", error); | |
} | |
} | |
private bool HasPassword() | |
{ | |
var user = this.userManager.FindById(User.Identity.GetUserId()); | |
if (user != null) | |
{ | |
return user.PasswordHash != null; | |
} | |
return false; | |
} | |
private bool HasPhoneNumber() | |
{ | |
var user = this.userManager.FindById(User.Identity.GetUserId()); | |
if (user != null) | |
{ | |
return user.PhoneNumber != null; | |
} | |
return false; | |
} | |
public enum ManageMessageId | |
{ | |
AddPhoneSuccess, | |
ChangePasswordSuccess, | |
SetTwoFactorSuccess, | |
SetPasswordSuccess, | |
RemoveLoginSuccess, | |
RemovePhoneSuccess, | |
Error | |
} | |
#endregion | |
} | |
} |
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 Microsoft.Owin; | |
using Owin; | |
using System.Web.Mvc; | |
using System.Web.Optimization; | |
using System.Web.Routing; | |
[assembly: OwinStartupAttribute(typeof(NinjectMVC5Owin.Startup))] | |
namespace NinjectMVC5Owin | |
{ | |
public partial class Startup | |
{ | |
public void Configuration(IAppBuilder app) | |
{ | |
AreaRegistration.RegisterAllAreas(); | |
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); | |
RouteConfig.RegisterRoutes(RouteTable.Routes); | |
BundleConfig.RegisterBundles(BundleTable.Bundles); | |
ConfigureAuth(app); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment