cleaning up controllers (#19)

This commit is contained in:
Steve Smith
2017-07-26 17:43:32 -04:00
committed by GitHub
parent b645cb35c1
commit 11ace4ea35
3 changed files with 21 additions and 66 deletions

View File

@@ -1,6 +1,4 @@
using Microsoft.eShopWeb.Services;
using Microsoft.eShopWeb.ViewModels;
using Microsoft.AspNetCore.Hosting;
using Microsoft.eShopWeb.ViewModels;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
@@ -16,7 +14,6 @@ namespace Microsoft.eShopWeb.Controllers
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly string _externalCookieScheme;
public AccountController(
UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager,
@@ -27,57 +24,37 @@ namespace Microsoft.eShopWeb.Controllers
_userManager = userManager;
_signInManager = signInManager;
_externalCookieScheme = identityCookieOptions.Value.ExternalCookieAuthenticationScheme;
}
//
// GET: /Account/SignIn
[HttpGet]
[AllowAnonymous]
public async Task<IActionResult> SignIn(string returnUrl = null)
{
// Clear the existing external cookie to ensure a clean login process
await HttpContext.Authentication.SignOutAsync(_externalCookieScheme);
ViewData["ReturnUrl"] = returnUrl;
return View();
}
//
// POST: /Account/SignIn
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> SignIn(LoginViewModel model, string returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;
if (ModelState.IsValid)
if (!ModelState.IsValid)
{
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, set lockoutOnFailure: true
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)
//{
// return RedirectToAction(nameof(SendCode), new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
//}
if (result.IsLockedOut)
{
//_logger.LogWarning(2, "User account locked out.");
return View("Lockout");
}
else
{
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
return View(model);
}
return View(model);
}
ViewData["ReturnUrl"] = returnUrl;
// If we got this far, something failed, redisplay form
var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false);
if (result.Succeeded)
{
return RedirectToLocal(returnUrl);
}
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
return View(model);
}
@@ -92,7 +69,5 @@ namespace Microsoft.eShopWeb.Controllers
return RedirectToAction(nameof(CatalogController.Index), "Catalog");
}
}
}
}