登录流程图

示例预览

构建步骤

当然,你也可以直接之前前往coding仓库查看源码,要是发现bug记得提醒我啊~ LoginDemo地址

1. 首先你得有一个项目

2. 然后你需要一个登录页面

完整Login.cshtml视图代码戳这里-共计55行

效果预览图

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>登录界面</title>
<meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no">
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">
<style type="text/css">
body { color: #fff; font-family: "微软雅黑"; font-size: 14px; background: url('https://dn-coding-net-production-pp.qbox.me/96ec8cc7-0e5f-4217-b853-4a88c15579f3.png') no-repeat; }
.wrap1 { position: absolute; top: 0; right: 0; bottom: 0; left: 0; margin: auto; height: 450px; }
/*把整个屏幕真正撑开--而且能自己实现居中*/
.main_content { background: url(https://dn-coding-net-production-pp.qbox.me/2ed70a05-04ad-4ccf-81d4-bc1fad2b6e41.png) repeat; margin-left: auto; margin-right: auto; text-align: left; float: none; border-radius: 8px; }
.form-group { position: relative; }
.login_btn { display: block; background: #3872f6; color: #fff; font-size: 15px; width: 100%; line-height: 50px; border-radius: 3px; border: none; }
.login_input { width: 100%; border: 1px solid #3872f6; border-radius: 3px; line-height: 40px; padding: 2px 5px 2px 30px; background: none; }
.icon_font { position: absolute; top: 12px; left: 10px; font-size: 18px; color: #3872f6; }
.font16 { font-size: 16px; }
.mg-t20 { margin-top: 20px; }
@media (min-width:200px) {.pd-xs-20 { padding: 20px; }}
@media (min-width:768px) {.pd-sm-50 { padding: 50px; }}
#grad { background: -webkit-linear-gradient(#4990c1, #52a3d2, #6186a3); /* Safari 5.1 - 6.0 */ background: -o-linear-gradient(#4990c1, #52a3d2, #6186a3); /* Opera 11.1 - 12.0 */ background: -moz-linear-gradient(#4990c1, #52a3d2, #6186a3); /* Firefox 3.6 - 15 */ background: linear-gradient(#4990c1, #52a3d2, #6186a3); /* 标准的语法 */ }
/*==jquery.validate css==*/
.field-validation-error { color: #e14430 !important; padding-top: 5px; }
.input-validation-error { border-color: #d38e99; }
</style>
</head>
<body>
<div class="container wrap1">
<h2 class="mg-b20 text-center">后台管理系统</h2>
<div class="col-sm-8 col-md-5 center-auto pd-sm-50 pd-xs-20 main_content">
<p class="text-center font16">用户登录</p>
<form asp-action="Login" method="post" >
<div class="form-group mg-t20">
<i class="icon_font glyphicon glyphicon-user"></i>
<input type="text" class="login_input" asp-for="UserName" placeholder="请输入用户名" autofocus />
<span asp-validation-for="UserName"></span>
</div>
<div class="form-group mg-t20">
<i class="icon_font glyphicon glyphicon-lock"></i>
<input type="password" class="login_input" asp-for="UserPwd" placeholder="请输入密码" />
<span asp-validation-for="UserPwd"></span>
</div>
<div class="checkbox mg-b25 hide">
<label>
<input type="checkbox">记住我的登录信息
</label>
</div>
<button type="submit" class="login_btn">登 录</button>
</form>
</div>
</div>
</body>
</html>

3. 然后你需要一个登录的控制器AccountController

控制器里面至少拥有一个呈现登录页的action,一个接收登录请求的action,一个退出的action

·登录· 判断是否存在用户,将用户名或者用户ID加密后记录到cookie中,跳转到管理页

·退出· 将cookie移出掉,跳转到登录页

加密的方法可自行切换为其他的加密方法

    public class AccountController : Controller
{
private readonly IUserService _userService;
public AccountController(IUserService userService)
{
_userService = userService;
} public IActionResult Login()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Login(AccountModel model)
{
//验证模型是否正确
if (!ModelState.IsValid)
{
return View(model);
}
//调用服务验证用户名密码
if (!_userService.Login(model.UserName, model.UserPwd))
{
ModelState.AddModelError(nameof(model.UserPwd), "用户名或密码错误");
return View();
}
//加密用户名写入cookie中,AdminAuthorizeAttribute特性标记取出cookie并解码除用户名
var encryptValue = _userService.LoginEncrypt(model.UserName, ApplicationKeys.User_Cookie_Encryption_Key);
HttpContext.Response.Cookies.Append(ApplicationKeys.User_Cookie_Key, encryptValue);
return Redirect("/");
}
public IActionResult Logout()
{
HttpContext.Response.Cookies.Delete(ApplicationKeys.User_Cookie_Key);
return Redirect(WebContext.LoginUrl);
}
}

4. 然后还需要一个身份验证的特性标记AdminAuthorizeAttribute

本文只是简单的验证是否登录,关于更复杂的权限验证可参考文章:http://www.cnblogs.com/morang/p/7606843.html,以及示例项目

将此特性标记加到需要的地方即可在访问时验证用户是否登录,未登录则跳转到登录页。

    public class AdminAuthorizeAttribute : Attribute, IAuthorizationFilter
{
public void OnAuthorization(AuthorizationFilterContext filterContext)
{
if (string.IsNullOrEmpty(WebContext.AdminName))
{
if (filterContext.HttpContext.Request.Headers["X-Requested-With"] == "XMLHttpRequest")
{
filterContext.Result = new JsonResult("未登录");
}
else
{
filterContext.Result = new RedirectResult("Account/Login");
}
return;
}
}
}

上面特性标记代码中的WebContext.AdminName是如何取到的呢?还需要结合如下代码

    //服务定位器
public static class ServiceLocator
{
public static IServiceProvider Instance { get; set; }
public static T GetService<T>() where T : class
{
return Instance.GetService<T>();
} }
//一些通用的信息
public static class WebContext
{
public static string AdminName
{
get
{
//获取cookie
var hasCookie = ServiceLocator.GetService<IHttpContextAccessor>()
.HttpContext
.Request.Cookies
.TryGetValue(ApplicationKeys.User_Cookie_Key, out string encryptValue);
if (!hasCookie || string.IsNullOrEmpty(encryptValue))
return null;
var adminName = ServiceLocator.GetService<IUserService>().LoginDecrypt(encryptValue, ApplicationKeys.User_Cookie_Encryption_Key);
return adminName;
}
}
public const string LoginUrl = "/account/login";
}
//全局的一些Key值
public class ApplicationKeys
{
public const string User_Cookie_Encryption_Key = "User_Cookie_Encryption_Key"; public const string User_Cookie_Key = "User_Cookie_Key";
} //Startup
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();//用于获取请求上下文
services.AddTransient<IUserService, UserService>();
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
//app.UseMvc()..
//最末的时候赋值
ServiceLocator.Instance = app.ApplicationServices;
}

代码说明

  1. 首先定义了一个存放服务的静态对象:ServiceLocator
  2. 在程序启动后将IApplicationBuilder.ApplicationServices赋值给ServiceLocator.Instance,这样就能够在任何地方使用ServiceLocator.Instance获取到注入的服务

    (为了更好的获取实例添加了一个T GetService<T>()方法)
  3. 在WebContext中取获取Cookie值:ServiceLocator.GetService<IHttpContextAccessor>().HttpContext.Request.Cookies
  4. 解密获取的cookie得到用户名:ServiceLocator.GetService<IUserService>().LoginDecrypt(encryptValue, ApplicationKeys.User_Cookie_Encryption_Key);
  5. 所以在后台就能使用WebContext.AdminName获取到当前登录用户名,或者根据用户名获取登录信息

总结

  • 自定义特性标记和过滤器之间差开一个IFilterMetadata,换言之:特性标记实现了IFilterMetadata就等于是个过滤器(个人理解)
  • asp.net core中模型绑定使用asp-for
  • asp.net core注入服务: 在 Startup.ConfigureServices方法中注入 services.AddTransient<IUserService, UserService>()
  • asp.net core获取HttpContext对象 参考:ASP.NET Core开发之HttpContext

ASP.NET Core中提供了一个IHttpContextAccessor接口,HttpContextAccessor 默认实现了它简化了访问HttpContext

它必须在程序启动时在IServicesCollection中注册,这样在程序中就能获取到HttpContextAccessor,并用来访问HttpContext

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

Demo下载地址

探索学习中,若有错误或者不足指出还望园友指出。

asp.net core后台系统登录的快速构建的更多相关文章

  1. ASP.NET Core模块化前后端分离快速开发框架介绍之4、模块化实现思路

    源码 GitHub:https://github.com/iamoldli/NetModular 演示地址 地址:https://nm.iamoldli.com 账户:admin 密码:admin 前 ...

  2. ASP.NET Core模块化前后端分离快速开发框架介绍之2、快速创建一个业务模块

    源码地址 GitHub:https://github.com/iamoldli/NetModular 演示地址 地址:https://nm.iamoldli.com 账户:admin 密码:admin ...

  3. 在 ASP.NET Core Web API中使用 Polly 构建弹性容错的微服务

    在 ASP.NET Core Web API中使用 Polly 构建弹性容错的微服务 https://procodeguide.com/programming/polly-in-aspnet-core ...

  4. ASP.NET Core模块化前后端分离快速开发框架介绍之1、开篇

    源码地址 GitHub:https://github.com/iamoldli/NetModular 演示地址 地址:https://nm.iamoldli.com 账户:admin 密码:admin ...

  5. ASP.NET Core MVC TagHelper实践HighchartsNET快速图表控件-开源

    ASP.NET Core MVC TagHelper最佳实践HighchartsNET快速图表控件支持ASP.NET Core. 曾经在WebForms上写过 HighchartsNET快速图表控件- ...

  6. ASP.NET Core AD 域登录

    在选择AD登录时,其实可以直接选择 Windows 授权,不过因为有些网站需要的是LDAP获取信息进行授权,而非直接依赖Web Server自带的Windows 授权功能. 当然如果使用的是Azure ...

  7. 【.NET Core】ASP.NET Core之IdentityServer4(1):快速入门

    [.NET Core]ASP.NET Core之IdentityServer4 本文中的IdentityServer4基于上节的jenkins 进行docker自动化部署. 使用了MariaDB,EF ...

  8. .NET 黑魔法 - asp.net core 日志系统

    asp.net core 里如何记录日志呢? 这要从asp.net core的依赖注入说起,在asp.net core里的依赖注入真是无所不在,各种面向切面的接口与事件. 好吧,来点干货. 首先,我们 ...

  9. ASP.NET Core AD 域登录 (转载)

    在选择AD登录时,其实可以直接选择 Windows 授权,不过因为有些网站需要的是LDAP获取信息进行授权,而非直接依赖Web Server自带的Windows 授权功能. 当然如果使用的是Azure ...

随机推荐

  1. 理解javascript 对象,原型对象、闭包

    javascript作为一个面向对象的语言,理解 对象.原型.闭包.模块模式等技术点对于成为一名合格的javascript程序员相当重要,多年没写过blog,今天就先拋个玉,在下基本也不做前端,但颇感 ...

  2. 用PyCharm执行测试成功但无法生成HTMLTestRunner报告

    问题:代码写的没问题,执行也成功了,但就是无法生成HTMLTestRunner的报告 其实这是编辑器搞得鬼,编辑器为了方便用户执行测试,都有一项功能,可以用编辑器来调用unittest或者nose来执 ...

  3. mvc中html导出成word下载-简单粗暴方式

    由于工作需求,需要把html简历页导出成word下载.网上搜索了很多解决方案,基本都是用一些插件,然后写法也很麻烦,需要创建模板什么的. 固定替换值  代码一大堆.但是对于我的需求来说  并没有什么用 ...

  4. 将位图导入为ArcGIS面要素

    本文根据笔者经验,介绍一种从位图图像导入ArcGIS称为要素的方法.这种方法适用于从现有出版物图片中获取地理信息的情况. 首先要说明的是,从位图导入要素是非常非常不精确的方式,如果有其它数据来源,那么 ...

  5. Codis分布式锁

    近期一项需求需要使用分布式锁,考虑的方案主要有如下两种: zookeeper codis 因为对于zookeeper不是特别熟悉,因此选用了codis,Codis是一个分布式的Redis解决方案,从应 ...

  6. 【grunt】两小时入门

    目录: 1. 用途和场景 2.Grunt插件 3.相关资源 4.环境安装 5.开始学习 5.1 一个新项目 5.2 生成package.json 5.3 在项目中安装grunt和相关插件 5.4 Gr ...

  7. 第4阶段——制作根文件系统之分析init_post()如何启动第1个程序(1)

    本章学习如何启动第一个应用程序 1.在前面的分析中我们了解到,在init进程中内核挂接到根文件系统之后,会开始启动第一个应用程序: kernel_init函数代码如下: static int __in ...

  8. Java代理详解

    一.概述 代理模式是Java常用的设计模式之一,实现代理模式要求代理类和委托类(被代理的类)具有相同的方法(提供相同的服务),代理类对象自身并不实现真正的核心逻辑,而是通过调用委托类对象的相关方法来处 ...

  9. Day-12: 进程和线程

    进程和线程 在操作系统看来,一个任务就是一个进程,而一个进程内部如果要做多个任务就是有多个线程.一个进程至少有一个线程. 真正的并行执行任务是由多个CUP分别执行任务,实际中是由,操作系统轮流让各个任 ...

  10. vmware 遇到 “无法打开内核设备 \\.\Global\vmx86” 解决

    问题描述:vmware没有正常关闭,再次打开使用时蓝屏,在安全模式下再次打开不会蓝屏,但提示"无法打开内核设备 \.\Global\vmx86: 系统找不到指定的文件,你想要安装VMware ...