增加功能:默认首页母版页更改为_Frame

This commit is contained in:
Argo Zhang 2019-04-18 15:37:26 +08:00
parent 369023b0bc
commit e37836f1b4
3 changed files with 102 additions and 116 deletions

View File

@ -1,96 +1,96 @@
using Bootstrap.Admin.Models; using Bootstrap.Admin.Models;
using Bootstrap.DataAccess; using Bootstrap.DataAccess;
using Longbow.Web; using Longbow.Web;
using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System; using System;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Security.Claims; using System.Security.Claims;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Bootstrap.Admin.Controllers namespace Bootstrap.Admin.Controllers
{ {
/// <summary> /// <summary>
/// Account controller. /// Account controller.
/// </summary> /// </summary>
[AllowAnonymous] [AllowAnonymous]
[AutoValidateAntiforgeryToken] [AutoValidateAntiforgeryToken]
public class AccountController : Controller public class AccountController : Controller
{ {
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
[HttpGet] [HttpGet]
public ActionResult Login() public ActionResult Login()
{ {
if (DictHelper.RetrieveSystemModel()) if (DictHelper.RetrieveSystemModel())
{ {
ViewBag.UserName = "Admin"; ViewBag.UserName = "Admin";
ViewBag.Password = "123789"; ViewBag.Password = "123789";
}
return User.Identity.IsAuthenticated ? (ActionResult)Redirect("~/Home/Index") : View("Login", new LoginModel());
}
/// <summary>
/// Login the specified userName, password and remember.
/// </summary>
/// <returns>The login.</returns>
/// <param name="onlineUserSvr"></param>
/// <param name="ipLocator"></param>
/// <param name="userName">User name.</param>
/// <param name="password">Password.</param>
/// <param name="remember">Remember.</param>
[HttpPost]
public async Task<IActionResult> Login([FromServices]IOnlineUsers onlineUserSvr, [FromServices]IIPLocatorProvider ipLocator, string userName, string password, string remember)
{
if (UserHelper.Authenticate(userName, password, loginUser => CreateLoginUser(onlineUserSvr, ipLocator, HttpContext, loginUser)))
{
var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
identity.AddClaim(new Claim(ClaimTypes.Name, userName));
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity), new AuthenticationProperties { ExpiresUtc = DateTimeOffset.Now.AddDays(DictHelper.RetrieveCookieExpiresPeriod()), IsPersistent = remember == "true" });
// redirect origin url
var originUrl = Request.Query[CookieAuthenticationDefaults.ReturnUrlParameter].FirstOrDefault() ?? "~/Home/Index";
return Redirect(originUrl);
} }
return View("Login", new LoginModel()); return User.Identity.IsAuthenticated ? (ActionResult)Redirect("~/Home/Index") : View("Login", new LoginModel());
} }
/// <summary> /// <summary>
/// /// Login the specified userName, password and remember.
/// </summary> /// </summary>
/// <param name="onlineUserSvr"></param> /// <returns>The login.</returns>
/// <param name="ipLocator"></param> /// <param name="onlineUserSvr"></param>
/// <param name="context"></param> /// <param name="ipLocator"></param>
/// <param name="loginUser"></param> /// <param name="userName">User name.</param>
internal static void CreateLoginUser(IOnlineUsers onlineUserSvr, IIPLocatorProvider ipLocator, HttpContext context, LoginUser loginUser) /// <param name="password">Password.</param>
{ /// <param name="remember">Remember.</param>
var agent = new UserAgent(context.Request.Headers["User-Agent"]); [HttpPost]
loginUser.Ip = (context.Connection.RemoteIpAddress ?? IPAddress.IPv6Loopback).ToString(); public async Task<IActionResult> Login([FromServices]IOnlineUsers onlineUserSvr, [FromServices]IIPLocatorProvider ipLocator, string userName, string password, string remember)
loginUser.City = ipLocator.Locate(loginUser.Ip); {
loginUser.Browser = $"{agent.Browser.Name} {agent.Browser.Version}"; if (UserHelper.Authenticate(userName, password, loginUser => CreateLoginUser(onlineUserSvr, ipLocator, HttpContext, loginUser)))
loginUser.OS = $"{agent.OS.Name} {agent.OS.Version}"; {
} var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
identity.AddClaim(new Claim(ClaimTypes.Name, userName));
/// <summary> await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity), new AuthenticationProperties { ExpiresUtc = DateTimeOffset.Now.AddDays(DictHelper.RetrieveCookieExpiresPeriod()), IsPersistent = remember == "true" });
/// Logout this instance. // redirect origin url
/// </summary> var originUrl = Request.Query[CookieAuthenticationDefaults.ReturnUrlParameter].FirstOrDefault() ?? "~/Home/Index";
/// <returns>The logout.</returns> return Redirect(originUrl);
public async Task<IActionResult> Logout() }
{ return View("Login", new LoginModel());
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); }
return Redirect("~" + CookieAuthenticationDefaults.LoginPath);
} /// <summary>
///
/// <summary> /// </summary>
/// Accesses the denied. /// <param name="onlineUserSvr"></param>
/// </summary> /// <param name="ipLocator"></param>
/// <returns>The denied.</returns> /// <param name="context"></param>
[ResponseCache(Duration = 600)] /// <param name="loginUser"></param>
public ActionResult AccessDenied() => View("Error", ErrorModel.CreateById(403)); internal static void CreateLoginUser(IOnlineUsers onlineUserSvr, IIPLocatorProvider ipLocator, HttpContext context, LoginUser loginUser)
} {
var agent = new UserAgent(context.Request.Headers["User-Agent"]);
loginUser.Ip = (context.Connection.RemoteIpAddress ?? IPAddress.IPv6Loopback).ToString();
loginUser.City = ipLocator.Locate(loginUser.Ip);
loginUser.Browser = $"{agent.Browser.Name} {agent.Browser.Version}";
loginUser.OS = $"{agent.OS.Name} {agent.OS.Version}";
}
/// <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)]
public ActionResult AccessDenied() => View("Error", ErrorModel.CreateById(403));
}
} }

View File

@ -17,7 +17,7 @@ namespace Bootstrap.Admin.Controllers
/// <returns></returns> /// <returns></returns>
public IActionResult Index() public IActionResult Index()
{ {
var model = new HeaderBarModel(User.Identity); var model = new NavigatorBarModel(this);
var url = DictHelper.RetrieveHomeUrl(model.AppCode); var url = DictHelper.RetrieveHomeUrl(model.AppCode);
return url.Equals("~/Home/Index", System.StringComparison.OrdinalIgnoreCase) ? (IActionResult)View(model) : Redirect(url); return url.Equals("~/Home/Index", System.StringComparison.OrdinalIgnoreCase) ? (IActionResult)View(model) : Redirect(url);
} }
@ -30,12 +30,12 @@ namespace Bootstrap.Admin.Controllers
[AllowAnonymous] [AllowAnonymous]
public IActionResult Error(int id) public IActionResult Error(int id)
{ {
var model = ErrorModel.CreateById(id); var model = ErrorModel.CreateById(id);
if (id != 403) if (id != 403)
{ {
var returnUrl = Request.Query[CookieAuthenticationDefaults.ReturnUrlParameter].ToString(); var returnUrl = Request.Query[CookieAuthenticationDefaults.ReturnUrlParameter].ToString();
if (!string.IsNullOrEmpty(returnUrl)) model.ReturnUrl = returnUrl; if (!string.IsNullOrEmpty(returnUrl)) model.ReturnUrl = returnUrl;
} }
return View(model); return View(model);
} }
} }

View File

@ -1,18 +1,4 @@
@{ @{
ViewBag.Title = "首页"; ViewBag.Title = "首页";
Layout = "_Bootstrap"; Layout = "_Frame";
} }
@section css {
<style type="text/css">
.content-body {
position: fixed;
left: 0;
bottom: 40px;
right: 0;
top: 96px;
overflow: hidden;
}
</style>
}
<div class="content-body welcome-bg">
</div>