asp.net core后台系统登录的快速构建
登錄流程圖
示例預(yù)覽
構(gòu)建步驟
當(dāng)然,你也可以直接之前前往coding倉庫查看源碼,要是發(fā)現(xiàn)bug記得提醒我啊~?LoginDemo地址
1. 首先你得有一個(gè)項(xiàng)目
2. 然后你需要一個(gè)登錄頁面
完整Login.cshtml視圖代碼戳這里-共計(jì)55行
效果預(yù)覽圖
3. 然后你需要一個(gè)登錄的控制器AccountController
控制器里面至少擁有一個(gè)呈現(xiàn)登錄頁的action,一個(gè)接收登錄請求的action,一個(gè)退出的action
·登錄· 判斷是否存在用戶,將用戶名或者用戶ID加密后記錄到cookie中,跳轉(zhuǎn)到管理頁
·退出· 將cookie移出掉,跳轉(zhuǎn)到登錄頁
加密的方法可自行切換為其他的加密方法
? ?? ?private readonly IUserService _userService; ?
? ?? ?? ?
? ??public AccountController(IUserService userService) ? ? ? ?{_userService = userService;} ? ? ?
? ??
? ?? ?public IActionResult Login() ? ? ? ?{ ? ? ?
? ?? ? ? ?return View();}
? ?? ? ? ?[HttpPost][ValidateAntiForgeryToken] ?public IActionResult Login(AccountModel model) ? ? ? ?{ ? ? ? ? ? ?//驗(yàn)證模型是否正確if (!ModelState.IsValid){
? ?? ? ? ?return View(model);} ? ? ? ? ?
? ?? ? ? ?//調(diào)用服務(wù)驗(yàn)證用戶名密碼if (!_userService.Login(model.UserName, model.UserPwd)){ModelState.AddModelError(nameof(model.UserPwd), "用戶名或密碼錯(cuò)誤"); ? ? ? ? ? ? ? ?return View();} ? ? ? ? ?
? ?? ? ? ?//加密用戶名寫入cookie中,AdminAuthorizeAttribute特性標(biāo)記取出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. 然后還需要一個(gè)身份驗(yàn)證的特性標(biāo)記AdminAuthorizeAttribute
本文只是簡單的驗(yàn)證是否登錄,關(guān)于更復(fù)雜的權(quán)限驗(yàn)證可參考文章:http://www.cnblogs.com/morang/p/7606843.html,以及示例項(xiàng)目
將此特性標(biāo)記加到需要的地方即可在訪問時(shí)驗(yàn)證用戶是否登錄,未登錄則跳轉(zhuǎn)到登錄頁。
上面特性標(biāo)記代碼中的WebContext.AdminName是如何取到的呢?還需要結(jié)合如下代碼
? ?//服務(wù)定位器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{ ? ? ? ? ? ? ? ?//獲取cookievar 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";} ? ?
? ? ????//Startuppublic void ConfigureServices(IServiceCollection services) ? ?{services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();//用于獲取請求上下文services.AddTransient<IUserService, UserService>();services.AddMvc();} ? ?public void Configure(IApplicationBuilder app, IHostingEnvironment env) ? ?{ ? ? ? ?//app.UseMvc()..//最末的時(shí)候賦值ServiceLocator.Instance = app.ApplicationServices;}
代碼說明
首先定義了一個(gè)存放服務(wù)的靜態(tài)對象:ServiceLocator
在程序啟動(dòng)后將IApplicationBuilder.ApplicationServices賦值給ServiceLocator.Instance,這樣就能夠在任何地方使用ServiceLocator.Instance獲取到注入的服務(wù)
(為了更好的獲取實(shí)例添加了一個(gè)T GetService<T>()方法)
在WebContext中取獲取Cookie值:ServiceLocator.GetService<IHttpContextAccessor>().HttpContext.Request.Cookies
解密獲取的cookie得到用戶名:ServiceLocator.GetService<IUserService>().LoginDecrypt(encryptValue, ApplicationKeys.User_Cookie_Encryption_Key);
所以在后臺就能使用WebContext.AdminName獲取到當(dāng)前登錄用戶名,或者根據(jù)用戶名獲取登錄信息
總結(jié)
-
自定義特性標(biāo)記和過濾器之間差開一個(gè)IFilterMetadata,換言之:特性標(biāo)記實(shí)現(xiàn)了IFilterMetadata就等于是個(gè)過濾器(個(gè)人理解)
-
asp.net core中模型綁定使用asp-for
-
asp.net core注入服務(wù): 在?Startup.ConfigureServices方法中注入?services.AddTransient<IUserService, UserService>()
-
asp.net core獲取HttpContext對象 參考:ASP.NET Core開發(fā)之HttpContext
ASP.NET Core中提供了一個(gè)IHttpContextAccessor接口,HttpContextAccessor?默認(rèn)實(shí)現(xiàn)了它簡化了訪問HttpContext。
它必須在程序啟動(dòng)時(shí)在IServicesCollection中注冊,這樣在程序中就能獲取到HttpContextAccessor,并用來訪問HttpContext。
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); -
asp.net core中表單直接使用form標(biāo)簽,asp-action,asp-controller等指定路由參數(shù)即可,并且能夠自動(dòng)生成防偽字段標(biāo)識,配合ValidateAntiForgeryToken特性標(biāo)記預(yù)防CSRF?代碼生成比較圖
相關(guān)文檔地址:https://docs.microsoft.com/zh-cn/aspnet/core/security/anti-request-forgery -
autofocus屬性 可使文本框自動(dòng)獲取焦點(diǎn)
Demo下載地址
-
點(diǎn)擊下載Demo
-
Coding倉庫地址克隆代碼:git clone https://git.coding.net/yimocoding/WeDemo.git -b LoginDemo LoginDemo
探索學(xué)習(xí)中,若有錯(cuò)誤或者不足指出還望園友指出。
原文地址:http://www.cnblogs.com/morang/p/7614537.html
.NET社區(qū)新聞,深度好文,微信中搜索dotNET跨平臺或掃描二維碼關(guān)注
總結(jié)
以上是生活随笔為你收集整理的asp.net core后台系统登录的快速构建的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Asp.Net Core 2.0 多角色
- 下一篇: 从头编写 asp.net core 2.