BootstrapAdmin/Bootstrap.Admin/Controllers/AccountController.cs

66 lines
2.4 KiB
C#
Raw Normal View History

2018-06-07 00:45:47 +08:00
using Bootstrap.Admin.Models;
using Bootstrap.Security;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
using System.Threading.Tasks;
namespace Bootstrap.Admin.Controllers
{
/// <summary>
/// Account controller.
/// </summary>
[AllowAnonymous]
public class AccountController : Controller
{
/// <summary>
/// Login the specified userName, password and remember.
/// </summary>
/// <returns>The login.</returns>
/// <param name="userName">User name.</param>
/// <param name="password">Password.</param>
/// <param name="remember">Remember.</param>
public async Task<IActionResult> Login(string userName, string password, string remember)
{
if (!string.IsNullOrEmpty(userName) && BootstrapUser.Authenticate(userName, password))
{
var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
identity.AddClaim(new Claim(ClaimTypes.Name, userName));
2018-06-08 21:11:40 +08:00
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity), new AuthenticationProperties() { IsPersistent = remember == "true" });
// redirect origin url
var originUrl = Request.Query[CookieAuthenticationDefaults.ReturnUrlParameter];
return Redirect(originUrl.Count == 1 ? originUrl[0] : "~/");
2018-06-07 00:45:47 +08:00
}
return View("Login", new ModelBase());
2018-06-07 00:45:47 +08:00
}
/// <summary>
/// Logout this instance.
/// </summary>
/// <returns>The logout.</returns>
public async Task<IActionResult> Logout()
{
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
return Redirect("~" + CookieAuthenticationDefaults.LoginPath);
}
/// <summary>
/// Accesses the denied.
/// </summary>
/// <returns>The denied.</returns>
[ResponseCache(Duration = 600)]
2018-06-07 00:45:47 +08:00
public ActionResult AccessDenied()
{
return View();
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public ActionResult Mobile()
{
return View();
}
}
}