本篇承接上篇內容,如果你不小心點擊進來,建議從第一篇開始完整閱讀,文章內容繼承性連貫性。 構建NetCore應用框架之實戰篇系列 一、簡介 1、登錄功能完成後,框架的雛形已經形成,有必要進行複習。 2、本篇簡單對框架代碼進行一些解釋。同時可以簡單理解框架的規範。 二、目錄結構規範 1、直接上圖,目錄 ...
本篇承接上篇內容,如果你不小心點擊進來,建議從第一篇開始完整閱讀,文章內容繼承性連貫性。
一、簡介
1、登錄功能完成後,框架的雛形已經形成,有必要進行複習。
2、本篇簡單對框架代碼進行一些解釋。同時可以簡單理解框架的規範。
二、目錄結構規範
1、直接上圖,目錄結構已經包含規範,哪類文件該放哪裡。
三、Startup中的代碼功能解釋
1、想了很多辦法,最終還是覺得用註釋的方式,請細讀文中註釋。
2、見代碼
namespace BitAdminCoreLearn { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { //註冊Session服務 services.AddSession(); //註冊HttpContext單例,這個HttpContextCore.Current要用到,不註冊取出來是null。 services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>(); //使用登錄認證 services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) .AddCookie(options => options.TicketDataFormat = new TicketDataFormat<AuthenticationTicket>()); services.AddMvc(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider svc) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } //使用配置信息,就是一個靜態變數付值。 HttpContextCore.Configuration = this.Configuration; HttpContextCore.ServiceProvider = svc; HttpContextCore.HostingEnvironment = env; //啟用靜態文件 app.UseStaticFiles(); //啟用Session緩存 app.UseSession(); //啟用登錄認證服務 app.UseAuthentication(); //使用自定義路由模版 app.UseMvc(routes => routes.MapRoute(name: "default", template: "{controller=Account}/{action=Index}/{id?}")); } } /// <summary> /// 這個是自定義Ticket加密解密類。 /// 功能:實現負責均衡支持,雖然Cookie模式,預設實現會與伺服器綁定,伺服器A產生的Cookies無法在伺服器B解釋。 /// 應用:登錄組件在加解密Cookies時會調用本方法,具體見services.AddAuthentication /// </summary> /// <typeparam name="T"></typeparam> public class TicketDataFormat<T> : ISecureDataFormat<T> where T : AuthenticationTicket { public string Protect(T data, string purpose) { TicketSerializer _serializer = new TicketSerializer(); byte[] userData = _serializer.Serialize(data); return Convert.ToBase64String(userData); } public T Unprotect(string protectedText, string purpose) { TicketSerializer _serializer = new TicketSerializer(); byte[] bytes = Convert.FromBase64String(protectedText); return _serializer.Deserialize(bytes) as T; } string ISecureDataFormat<T>.Protect(T data) { TicketSerializer _serializer = new TicketSerializer(); byte[] userData = _serializer.Serialize(data); return Convert.ToBase64String(userData); } T ISecureDataFormat<T>.Unprotect(string protectedText) { TicketSerializer _serializer = new TicketSerializer(); byte[] bytes = Convert.FromBase64String(protectedText); return _serializer.Deserialize(bytes) as T; } } /// <summary> /// 這裡是自定義許可權過濾器。 /// 功能:如果未登錄,返回405 /// 應用:如果api定義了過濾,會運行以下代碼,需要登錄才能訪問 /// 前端:前端ajax有全局設置,跳轉到登錄頁。 /// </summary> [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)] public class BitAuthorizeAttribute : AuthorizeAttribute, IAuthorizationFilter { public void OnAuthorization(AuthorizationFilterContext context) { if (!SSOClient.IsLogin) { context.Result = new StatusCodeResult((int)HttpStatusCode.MethodNotAllowed); } } } }
四、AccountController中的代碼功能解釋
1、上代碼、見註釋。
namespace BitAdminCore.Controllers { public class AccountController : Controller { DataContext dbContext = new DataContext(); /// <summary> /// 首頁跳轉,當用戶輸入功能變數名稱時,可以路轉到登錄頁,而不會出現錯誤。 /// </summary> /// <returns></returns> public ActionResult Index() { return Redirect("/pages/account/login.html"); } /// <summary> /// 判斷是否登錄,前端每個頁面載入時,都會進行判斷。 /// 登錄頁:如果已登錄,跳轉到首頁。 /// 其它頁:如果未登錄,跳轉到登錄頁。 /// </summary> /// <returns></returns> public JsonResult IsLogin() { return Json(Convert.ToString(SSOClient.IsLogin).ToLower()); } /// <summary> /// 驗證碼,直接返回image類型,把路徑寫在src上即可 /// </summary> /// <returns></returns> public ActionResult VerifyCode() { try { string code = VerificationCode.CreateCode(4); Bitmap image = VerificationCode.CreateImage(code); MemoryStream ms = new MemoryStream(); image.Save(ms, ImageFormat.Png); byte[] bytes = ms.GetBuffer(); ms.Close(); HttpContextCore.Current.Session.Set("VerificationCode", code); return File(bytes, "image/jpeg"); } catch (Exception ex) { LogHelper.SaveLog(ex); return Json(new { Code = 1, Msg = "伺服器異常,請聯繫管理員!" }); } } /// <summary> /// 登錄驗證方法,你懂的 /// </summary> /// <param name="account"></param> /// <param name="password"></param> /// <param name="verifyCode"></param> /// <returns></returns> public JsonResult Login(string account, string password,string verifyCode) { try { string vcode = HttpContextCore.Current.Session.Get<string>("VerificationCode"); if (Convert.ToString(verifyCode).ToLower() != Convert.ToString(vcode).ToLower()) return Json(new { Code = 1, Msg = "驗證碼不正確,請重新輸入!" }); if (!SSOClient.Validate(account, password, out Guid userId)) return Json(new { Code = 1, Msg = "帳號或密碼不正確,請重新輸入!" }); HttpContextCore.Current.Session.Set("VerificationCode", string.Empty); SSOClient.SignIn(userId); return Json(new { Code = 0 }); } catch (Exception ex) { LogHelper.SaveLog(ex); return Json(new { Code = 1, Msg = "伺服器異常,請聯繫管理員!" }); } } /// <summary> /// 登錄後獲取當前用戶信息,首頁會用到。 /// 這裡需要登錄後才能調用,所以加了過濾。 /// </summary> /// <returns></returns> [BitAuthorize] public JsonResult GetUser() { try { SysUser user = SSOClient.User; //SysDepartment department = SSOClient.Department; return Json(new { userCode = Convert.ToString(user.UserCode), userName = Convert.ToString(user.UserName), idCard = Convert.ToString(user.IdCard), mobile = Convert.ToString(user.Mobile), email = Convert.ToString(user.Email), //departmentName = Convert.ToString(department.DepartmentName) }); } catch (Exception ex) { LogHelper.SaveLog(ex); return Json(new { Code = 1, Msg = "伺服器異常,請聯繫管理員!" }); } } /// <summary> /// 登出 /// </summary> /// <returns></returns> public ActionResult SignOut() { SSOClient.SignOut(); return Json(new { Code = 0 }); } } }
五、SSOClient中的代碼功能解釋
1、上代碼、見註釋。
namespace BitAdminCore.Helpers { public partial class SSOClient { public static bool IsLogin { get { if (HttpContextCore.Current.User == null || HttpContextCore.Current.User.Identity == null) return false; return HttpContextCore.Current.User.Identity.IsAuthenticated; } } /// <summary> /// 通過用戶名密碼驗證用戶信息,如果成功,返回用戶ID,供系統預設登錄使用。 /// </summary> /// <param name="sign"></param> /// <param name="password"></param> /// <param name="userid"></param> /// <returns></returns> public static bool Validate(string sign, string password,out Guid userid) { userid = Guid.Empty; DataContext dbContext = new DataContext(); password = EncryptHelper.MD5(password); var userModel = dbContext.SysUser.FirstOrDefault(t => (t.Mobile == sign || t.Email == sign || t.UserCode == sign) && t.UserPassword == password); if (userModel == null) return false; userid = userModel.UserId; return true; } /// <summary> /// 通過用戶標識驗證用戶。不需要密碼,通常是第三方登錄之後,返回標識,再通過標識驗證。 /// </summary> /// <param name="sign"></param> /// <param name="user"></param> /// <returns></returns> public static bool Validate(string sign, out SysUser user) { DataContext dbContext = new DataContext(); user = dbContext.SysUser.FirstOrDefault(t => (t.Mobile == sign || t.Email == sign || t.UserCode == sign) ); if (user == null) return false; return true; } /// <summary> /// 登錄函數,寫入登錄狀態和登錄信息。 /// 這裡需要在Startup中的那些配置項(具體機制後續寫一篇文章介紹)。 /// </summary> /// <param name="userid"></param> public static void SignIn(Guid userid) { DataContext dbContext = new DataContext(); SysUser user = dbContext.SysUser.FirstOrDefault(x => x.UserId == userid); //var roles = dbContext.SysRoleUser.Where(x => x.UserId == user.UserId).ToList(); ClaimsIdentity identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme); identity.AddClaim(new Claim(ClaimTypes.Sid, user.UserId.ToString())); identity.AddClaim(new Claim(ClaimTypes.Name, user.UserCode)); //foreach (var role in roles) //{ // identity.AddClaim(new Claim(ClaimTypes.Role, role.RoleId.ToString())); //} SignOut(); HttpContextCore.Current.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity)); } /// <summary> /// 登出,也就是清除登錄Cookies和Session /// </summary> public static void SignOut() { HttpContextCore.Current.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); HttpContextCore.Current.Session.Clear(); } } }
本篇代碼解讀到此,接下來怎麼接著寫,需要思考一段時間了,連續寫太多,有些思路不清晰。