Permalink
Browse files

Recreated as RC1.

  • Loading branch information...
ashimoon committed Nov 21, 2015
1 parent 43e60e6 commit a253ea63570417e32f4db0a145b60c8e41383559
Showing 498 changed files with 8,848 additions and 107,013 deletions.
@@ -8,6 +8,7 @@
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.Data.Entity;
using Microsoft.Extensions.Logging;
using UpgradeToRcDemo.Models;
using UpgradeToRcDemo.Services;
using UpgradeToRcDemo.ViewModels.Account;
@@ -21,21 +22,20 @@ public class AccountController : Controller
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly IEmailSender _emailSender;
private readonly ISmsSender _smsSender;
private readonly ApplicationDbContext _applicationDbContext;

This comment has been minimized.

Show comment
Hide comment
@ashimoon

ashimoon Nov 21, 2015

Owner

ApplicationDbContext used to be required in the AccountController because this was the first component to use the database in the default ASP.NET 5 template.

This was a workaround so that the controller could ensure the database had been created before performing any operations on it. This workaround has been fixed and the database is migrated in Startup.cs instead.

@ashimoon

ashimoon Nov 21, 2015

Owner

ApplicationDbContext used to be required in the AccountController because this was the first component to use the database in the default ASP.NET 5 template.

This was a workaround so that the controller could ensure the database had been created before performing any operations on it. This workaround has been fixed and the database is migrated in Startup.cs instead.

private static bool _databaseChecked;
private readonly ILogger _logger;
public AccountController(
UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager,
IEmailSender emailSender,
ISmsSender smsSender,
ApplicationDbContext applicationDbContext)
ILoggerFactory loggerFactory)
{
_userManager = userManager;
_signInManager = signInManager;
_emailSender = emailSender;
_smsSender = smsSender;
_applicationDbContext = applicationDbContext;
_logger = loggerFactory.CreateLogger<AccountController>();

This comment has been minimized.

Show comment
Hide comment
@ashimoon

ashimoon Nov 21, 2015

Owner

Logging has been added to the AccountController. Note that we could have just injected an ILogger<AccountController> instead of manually creating the logger instance from the ILoggerFactory.

@ashimoon

ashimoon Nov 21, 2015

Owner

Logging has been added to the AccountController. Note that we could have just injected an ILogger<AccountController> instead of manually creating the logger instance from the ILoggerFactory.

}
//
@@ -55,7 +55,6 @@ public IActionResult Login(string returnUrl = null)
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
{
EnsureDatabaseCreated(_applicationDbContext);
ViewData["ReturnUrl"] = returnUrl;
if (ModelState.IsValid)
{
@@ -64,6 +63,7 @@ public async Task<IActionResult> Login(LoginViewModel model, string returnUrl =
var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false);
if (result.Succeeded)
{
_logger.LogInformation(1, "User logged in.");
return RedirectToLocal(returnUrl);
}
if (result.RequiresTwoFactor)
@@ -72,6 +72,7 @@ public async Task<IActionResult> Login(LoginViewModel model, string returnUrl =
}
if (result.IsLockedOut)
{
_logger.LogWarning(2, "User account locked out.");
return View("Lockout");
}
else
@@ -101,7 +102,6 @@ public IActionResult Register()
[ValidateAntiForgeryToken]
public async Task<IActionResult> Register(RegisterViewModel model)
{
EnsureDatabaseCreated(_applicationDbContext);
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
@@ -115,6 +115,7 @@ public async Task<IActionResult> Register(RegisterViewModel model)
//await _emailSender.SendEmailAsync(model.Email, "Confirm your account",
// "Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>");
await _signInManager.SignInAsync(user, isPersistent: false);
_logger.LogInformation(3, "User created a new account with password.");
return RedirectToAction(nameof(HomeController.Index), "Home");
}
AddErrors(result);
@@ -131,6 +132,7 @@ public async Task<IActionResult> Register(RegisterViewModel model)
public async Task<IActionResult> LogOff()
{
await _signInManager.SignOutAsync();
_logger.LogInformation(4, "User logged out.");
return RedirectToAction(nameof(HomeController.Index), "Home");
}
@@ -141,7 +143,6 @@ public async Task<IActionResult> LogOff()
[ValidateAntiForgeryToken]
public IActionResult ExternalLogin(string provider, string returnUrl = null)
{
EnsureDatabaseCreated(_applicationDbContext);
// Request a redirect to the external login provider.
var redirectUrl = Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl });
var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl);
@@ -164,6 +165,7 @@ public async Task<IActionResult> ExternalLoginCallback(string returnUrl = null)
var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent: false);
if (result.Succeeded)
{
_logger.LogInformation(5, "User logged in with {Name} provider.", info.LoginProvider);
return RedirectToLocal(returnUrl);
}
if (result.RequiresTwoFactor)
@@ -193,7 +195,7 @@ public async Task<IActionResult> ExternalLoginConfirmation(ExternalLoginConfirma
{
if (User.IsSignedIn())
{
return RedirectToAction(nameof(ManageController.Index),"Manage");
return RedirectToAction(nameof(ManageController.Index), "Manage");
}
if (ModelState.IsValid)
@@ -212,6 +214,7 @@ public async Task<IActionResult> ExternalLoginConfirmation(ExternalLoginConfirma
if (result.Succeeded)
{
await _signInManager.SignInAsync(user, isPersistent: false);
_logger.LogInformation(6, "User created an account using {Name} provider.", info.LoginProvider);
return RedirectToLocal(returnUrl);
}
}
@@ -422,6 +425,7 @@ public async Task<IActionResult> VerifyCode(VerifyCodeViewModel model)
}
if (result.IsLockedOut)
{
_logger.LogWarning(7, "User account locked out.");
return View("Lockout");
}
else
@@ -433,20 +437,6 @@ public async Task<IActionResult> VerifyCode(VerifyCodeViewModel model)
#region Helpers
// The following code creates the database and schema if they don't exist.
// This is a temporary workaround since deploying database through EF migrations is
// not yet supported in this release.
// Please see this http://go.microsoft.com/fwlink/?LinkID=615859 for more information on how to do deploy the database
// when publishing your application.
private static void EnsureDatabaseCreated(ApplicationDbContext context)
{
if (!_databaseChecked)
{
_databaseChecked = true;
context.Database.Migrate();
}
}

This comment has been minimized.

Show comment
Hide comment
@ashimoon

ashimoon Nov 21, 2015

Owner

Database creation happens in Startup.cs now.

@ashimoon

ashimoon Nov 21, 2015

Owner

Database creation happens in Startup.cs now.

private void AddErrors(IdentityResult result)
{
foreach (var error in result.Errors)
@@ -29,7 +29,7 @@ public IActionResult Contact()
public IActionResult Error()
{
return View("~/Views/Shared/Error.cshtml");
return View();
}
}
}
@@ -6,6 +6,7 @@
using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Mvc;
using Microsoft.Extensions.Logging;
using UpgradeToRcDemo.Models;
using UpgradeToRcDemo.Services;
using UpgradeToRcDemo.ViewModels.Manage;
@@ -19,21 +20,24 @@ public class ManageController : Controller
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly IEmailSender _emailSender;
private readonly ISmsSender _smsSender;
private readonly ILogger _logger;
public ManageController(
UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager,
IEmailSender emailSender,
ISmsSender smsSender)
UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager,
IEmailSender emailSender,
ISmsSender smsSender,
ILoggerFactory loggerFactory)
{
_userManager = userManager;
_signInManager = signInManager;
_emailSender = emailSender;
_smsSender = smsSender;
_logger = loggerFactory.CreateLogger<ManageController>();
}
//
// GET: /Account/Index
// GET: /Manage/Index
[HttpGet]
public async Task<IActionResult> Index(ManageMessageId? message = null)
{
@@ -58,28 +62,17 @@ public async Task<IActionResult> Index(ManageMessageId? message = null)
return View(model);
}
//
// GET: /Account/RemoveLogin
[HttpGet]
public async Task<IActionResult> RemoveLogin()
{
var user = await GetCurrentUserAsync();
var linkedAccounts = await _userManager.GetLoginsAsync(user);
ViewData["ShowRemoveButton"] = await _userManager.HasPasswordAsync(user) || linkedAccounts.Count > 1;
return View(linkedAccounts);
}
//
// POST: /Manage/RemoveLogin
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> RemoveLogin(string loginProvider, string providerKey)
public async Task<IActionResult> RemoveLogin(RemoveLoginViewModel account)
{
ManageMessageId? message = ManageMessageId.Error;
var user = await GetCurrentUserAsync();
if (user != null)
{
var result = await _userManager.RemoveLoginAsync(user, loginProvider, providerKey);
var result = await _userManager.RemoveLoginAsync(user, account.LoginProvider, account.ProviderKey);
if (result.Succeeded)
{
await _signInManager.SignInAsync(user, isPersistent: false);
@@ -90,14 +83,14 @@ public async Task<IActionResult> RemoveLogin(string loginProvider, string provid
}
//
// GET: /Account/AddPhoneNumber
// GET: /Manage/AddPhoneNumber
public IActionResult AddPhoneNumber()
{
return View();
}
//
// POST: /Account/AddPhoneNumber
// POST: /Manage/AddPhoneNumber
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> AddPhoneNumber(AddPhoneNumberViewModel model)
@@ -124,6 +117,7 @@ public async Task<IActionResult> EnableTwoFactorAuthentication()
{
await _userManager.SetTwoFactorEnabledAsync(user, true);
await _signInManager.SignInAsync(user, isPersistent: false);
_logger.LogInformation(1, "User enabled two-factor authentication.");
}
return RedirectToAction(nameof(Index), "Manage");
}
@@ -139,12 +133,13 @@ public async Task<IActionResult> DisableTwoFactorAuthentication()
{
await _userManager.SetTwoFactorEnabledAsync(user, false);
await _signInManager.SignInAsync(user, isPersistent: false);
_logger.LogInformation(2, "User disabled two-factor authentication.");
}
return RedirectToAction(nameof(Index), "Manage");
}
//
// GET: /Account/VerifyPhoneNumber
// GET: /Manage/VerifyPhoneNumber
[HttpGet]
public async Task<IActionResult> VerifyPhoneNumber(string phoneNumber)
{
@@ -154,7 +149,7 @@ public async Task<IActionResult> VerifyPhoneNumber(string phoneNumber)
}
//
// POST: /Account/VerifyPhoneNumber
// POST: /Manage/VerifyPhoneNumber
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> VerifyPhoneNumber(VerifyPhoneNumberViewModel model)
@@ -179,7 +174,7 @@ public async Task<IActionResult> VerifyPhoneNumber(VerifyPhoneNumberViewModel mo
}
//
// GET: /Account/RemovePhoneNumber
// GET: /Manage/RemovePhoneNumber
[HttpGet]
public async Task<IActionResult> RemovePhoneNumber()
{
@@ -205,7 +200,7 @@ public IActionResult ChangePassword()
}
//
// POST: /Account/Manage
// POST: /Manage/ChangePassword
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> ChangePassword(ChangePasswordViewModel model)
@@ -221,6 +216,7 @@ public async Task<IActionResult> ChangePassword(ChangePasswordViewModel model)
if (result.Succeeded)
{
await _signInManager.SignInAsync(user, isPersistent: false);
_logger.LogInformation(3, "User changed their password successfully.");
return RedirectToAction(nameof(Index), new { Message = ManageMessageId.ChangePasswordSuccess });
}
AddErrors(result);
@@ -263,7 +259,7 @@ public async Task<IActionResult> SetPassword(SetPasswordViewModel model)
return RedirectToAction(nameof(Index), new { Message = ManageMessageId.Error });
}
//GET: /Account/Manage
//GET: /Manage/ManageLogins
[HttpGet]
public async Task<IActionResult> ManageLogins(ManageMessageId? message = null)
{
@@ -329,16 +325,6 @@ private void AddErrors(IdentityResult result)
}
}
private async Task<bool> HasPhoneNumber()
{
var user = await _userManager.FindByIdAsync(User.GetUserId());
if (user != null)
{
return user.PhoneNumber != null;
}
return false;
}
public enum ManageMessageId
{
AddPhoneSuccess,
@@ -356,18 +342,6 @@ private async Task<ApplicationUser> GetCurrentUserAsync()
return await _userManager.FindByIdAsync(HttpContext.User.GetUserId());
}
private IActionResult RedirectToLocal(string returnUrl)
{
if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction(nameof(HomeController.Index), nameof(HomeController));
}
}
#endregion
}
}
Oops, something went wrong.

0 comments on commit a253ea6

Please sign in to comment.