2018-06-07 00:45:47 +08:00
|
|
|
|
using Bootstrap.Admin.Models;
|
2018-10-19 23:09:52 +08:00
|
|
|
|
using Bootstrap.DataAccess;
|
2019-03-04 01:27:04 +08:00
|
|
|
|
using Longbow.Web;
|
2018-06-07 00:45:47 +08:00
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
|
|
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2019-03-04 01:27:04 +08:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2018-06-07 00:45:47 +08:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2018-09-26 17:57:07 +08:00
|
|
|
|
using System;
|
2018-09-16 13:23:26 +08:00
|
|
|
|
using System.Linq;
|
2019-03-09 19:18:03 +08:00
|
|
|
|
using System.Net;
|
2018-06-07 00:45:47 +08:00
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Bootstrap.Admin.Controllers
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Account controller.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[AllowAnonymous]
|
2018-09-13 11:53:55 +08:00
|
|
|
|
[AutoValidateAntiforgeryToken]
|
2018-06-07 00:45:47 +08:00
|
|
|
|
public class AccountController : Controller
|
|
|
|
|
{
|
2018-09-13 11:53:55 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public ActionResult Login()
|
|
|
|
|
{
|
2019-03-26 17:12:18 +08:00
|
|
|
|
if (DictHelper.RetrieveSystemModel())
|
|
|
|
|
{
|
|
|
|
|
ViewBag.UserName = "Admin";
|
|
|
|
|
ViewBag.Password = "123789";
|
|
|
|
|
}
|
2018-09-26 17:57:07 +08:00
|
|
|
|
return User.Identity.IsAuthenticated ? (ActionResult)Redirect("~/Home/Index") : View("Login", new ModelBase());
|
2018-09-13 11:53:55 +08:00
|
|
|
|
}
|
2018-11-05 11:36:30 +08:00
|
|
|
|
|
2018-06-07 00:45:47 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Login the specified userName, password and remember.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>The login.</returns>
|
2019-03-04 01:27:04 +08:00
|
|
|
|
/// <param name="onlineUserSvr"></param>
|
2018-06-07 00:45:47 +08:00
|
|
|
|
/// <param name="userName">User name.</param>
|
|
|
|
|
/// <param name="password">Password.</param>
|
|
|
|
|
/// <param name="remember">Remember.</param>
|
2018-09-13 11:53:55 +08:00
|
|
|
|
[HttpPost]
|
2019-03-04 01:27:04 +08:00
|
|
|
|
public async Task<IActionResult> Login([FromServices]IOnlineUsers onlineUserSvr, string userName, string password, string remember)
|
2018-06-07 00:45:47 +08:00
|
|
|
|
{
|
2019-03-04 01:27:04 +08:00
|
|
|
|
if (UserHelper.Authenticate(userName, password, loginUser => CreateLoginUser(onlineUserSvr, HttpContext, loginUser)))
|
2018-06-07 00:45:47 +08:00
|
|
|
|
{
|
|
|
|
|
var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
|
|
|
|
|
identity.AddClaim(new Claim(ClaimTypes.Name, userName));
|
2019-03-08 15:33:00 +08:00
|
|
|
|
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity), new AuthenticationProperties { ExpiresUtc = DateTimeOffset.Now.AddDays(DictHelper.RetrieveCookieExpiresPeriod()), IsPersistent = remember == "true" });
|
2019-03-13 18:25:32 +08:00
|
|
|
|
// redirect origin url
|
|
|
|
|
var originUrl = Request.Query[CookieAuthenticationDefaults.ReturnUrlParameter].FirstOrDefault() ?? "~/Home/Index";
|
|
|
|
|
return Redirect(originUrl);
|
2018-06-07 00:45:47 +08:00
|
|
|
|
}
|
2019-03-13 18:25:32 +08:00
|
|
|
|
return View("Login", new ModelBase());
|
2018-06-07 00:45:47 +08:00
|
|
|
|
}
|
2018-11-05 11:36:30 +08:00
|
|
|
|
|
2019-03-04 01:27:04 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="onlineUserSvr"></param>
|
|
|
|
|
/// <param name="context"></param>
|
|
|
|
|
/// <param name="loginUser"></param>
|
|
|
|
|
internal static void CreateLoginUser(IOnlineUsers onlineUserSvr, HttpContext context, LoginUser loginUser)
|
|
|
|
|
{
|
|
|
|
|
var agent = new UserAgent(context.Request.Headers["User-Agent"]);
|
2019-03-09 19:18:03 +08:00
|
|
|
|
loginUser.Ip = (context.Connection.RemoteIpAddress ?? IPAddress.IPv6Loopback).ToString();
|
2019-03-04 01:27:04 +08:00
|
|
|
|
loginUser.City = onlineUserSvr.RetrieveLocaleByIp(loginUser.Ip);
|
|
|
|
|
loginUser.Browser = $"{agent.Browser.Name} {agent.Browser.Version}";
|
|
|
|
|
loginUser.OS = $"{agent.OS.Name} {agent.OS.Version}";
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2018-11-05 11:36:30 +08:00
|
|
|
|
|
2018-06-07 00:45:47 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Accesses the denied.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>The denied.</returns>
|
2018-09-12 22:31:42 +08:00
|
|
|
|
[ResponseCache(Duration = 600)]
|
2019-03-07 01:12:36 +08:00
|
|
|
|
public ActionResult AccessDenied() => View("Error", ErrorModel.CreateById(403));
|
2018-06-07 00:45:47 +08:00
|
|
|
|
}
|
|
|
|
|
}
|