207 lines
6.7 KiB
C#
207 lines
6.7 KiB
C#
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace IdentityDemo.Controllers
|
|
{
|
|
|
|
[ApiExplorerSettings(IgnoreApi = true)]
|
|
[Route("[controller]/[action]")]
|
|
public class AccountController : Controller
|
|
{
|
|
/*
|
|
private readonly UserManager<User> _userManager;
|
|
private readonly SignInManager<User> _signInManager;
|
|
private readonly ILogger _logger;
|
|
|
|
public AccountController(
|
|
UserManager<User> userManager,
|
|
SignInManager<User> signInManager,
|
|
ILogger<AccountController> logger)
|
|
{
|
|
_userManager = userManager;
|
|
_signInManager = signInManager;
|
|
_logger = logger;
|
|
}
|
|
/*
|
|
[TempData]
|
|
public string ErrorMessage { get; set; }
|
|
|
|
[HttpGet]
|
|
[AllowAnonymous]
|
|
public async Task<IActionResult> Login(string returnUrl = null)
|
|
{
|
|
// Clear the existing external cookie to ensure a clean login process
|
|
await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);
|
|
|
|
ViewData["ReturnUrl"] = returnUrl;
|
|
return View();
|
|
}
|
|
*/
|
|
|
|
[HttpPost]
|
|
public ActionResult _login(string username, string password, bool rememberMe)
|
|
{
|
|
var result = username != null && password != null && username.Equals("acc1") && password.Equals("123"); //await _signInManager.PasswordSignInAsync(userName, password, rememberMe, lockoutOnFailure: false);
|
|
JsonResponse response = new JsonResponse();
|
|
response.Success = false;
|
|
response.Message = "Username or password is invalid.";
|
|
|
|
if (result)
|
|
{
|
|
var loggedUser = HttpContext.Session.GetString("username");
|
|
if (loggedUser==null || !loggedUser.Equals(username))
|
|
{
|
|
HttpContext.Session.SetString("username", username);
|
|
response.Success = true;
|
|
response.Message = "";
|
|
}
|
|
else
|
|
{
|
|
response.Message = "User already logged";
|
|
}
|
|
}
|
|
return Json(response);
|
|
}
|
|
|
|
public ActionResult _logout()
|
|
{
|
|
HttpContext.Session.Clear();
|
|
return Json(new JsonResponse());
|
|
}
|
|
|
|
internal class JsonResponse
|
|
{
|
|
public bool Success { get; internal set; }
|
|
public string Message { get; internal set; }
|
|
}
|
|
/*
|
|
[HttpPost]
|
|
[AllowAnonymous]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
|
|
{
|
|
ViewData["ReturnUrl"] = returnUrl;
|
|
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("User logged in.");
|
|
return RedirectToLocal(returnUrl);
|
|
}
|
|
if (result.IsLockedOut)
|
|
{
|
|
_logger.LogWarning("User account locked out.");
|
|
return RedirectToAction(nameof(Lockout));
|
|
}
|
|
else
|
|
{
|
|
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
|
|
return View(model);
|
|
}
|
|
}
|
|
|
|
// If we got this far, something failed, redisplay form
|
|
return View(model);
|
|
}
|
|
|
|
[HttpGet]
|
|
[AllowAnonymous]
|
|
public IActionResult Lockout()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
[HttpGet]
|
|
[AllowAnonymous]
|
|
public IActionResult Register(string returnUrl = null)
|
|
{
|
|
ViewData["ReturnUrl"] = returnUrl;
|
|
return View();
|
|
}
|
|
|
|
[HttpPost]
|
|
[AllowAnonymous]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> Register(RegisterViewModel model, string returnUrl = null)
|
|
{
|
|
ViewData["ReturnUrl"] = returnUrl;
|
|
if (ModelState.IsValid)
|
|
{
|
|
var user = new User { UserName = model.Email, Email = model.Email };
|
|
var result = await _userManager.CreateAsync(user, model.Password);
|
|
if (result.Succeeded)
|
|
{
|
|
await _signInManager.SignInAsync(user, isPersistent: false);
|
|
_logger.LogInformation("User created a new account with password.");
|
|
return RedirectToLocal(returnUrl);
|
|
}
|
|
AddErrors(result);
|
|
}
|
|
|
|
// If we got this far, something failed, redisplay form
|
|
return View(model);
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> Logout()
|
|
{
|
|
await _signInManager.SignOutAsync();
|
|
_logger.LogInformation("User logged out.");
|
|
return RedirectToAction(nameof(HomeController.Index), "Home");
|
|
}
|
|
|
|
[HttpGet]
|
|
[AllowAnonymous]
|
|
public async Task<IActionResult> ConfirmEmail(string userId, string code)
|
|
{
|
|
if (userId == null || code == null)
|
|
{
|
|
return RedirectToAction(nameof(HomeController.Index), "Home");
|
|
}
|
|
var user = await _userManager.FindByIdAsync(userId);
|
|
if (user == null)
|
|
{
|
|
throw new ApplicationException($"Unable to load user with ID '{userId}'.");
|
|
}
|
|
var result = await _userManager.ConfirmEmailAsync(user, code);
|
|
return View(result.Succeeded ? "ConfirmEmail" : "Error");
|
|
}
|
|
|
|
[HttpGet]
|
|
public IActionResult AccessDenied()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
#region Helpers
|
|
|
|
private void AddErrors(IdentityResult result)
|
|
{
|
|
foreach (var error in result.Errors)
|
|
{
|
|
ModelState.AddModelError(string.Empty, error.Description);
|
|
}
|
|
}
|
|
|
|
private IActionResult RedirectToLocal(string returnUrl)
|
|
{
|
|
if (Url.IsLocalUrl(returnUrl))
|
|
{
|
|
return Redirect(returnUrl);
|
|
}
|
|
else
|
|
{
|
|
return RedirectToAction(nameof(HomeController.Index), "Home");
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
*/
|
|
}
|
|
} |