网站建设公司西安重庆找工作哪个网站好
news/
2025/9/23 11:21:37/
文章来源:
网站建设公司西安,重庆找工作哪个网站好,请问我做吉利网站吉利啊,门户网站建设公开情况自查登录流程图 示例预览 构建步骤 当然#xff0c;你也可以直接之前前往coding仓库查看源码#xff0c;要是发现bug记得提醒我啊~ LoginDemo地址 1. 首先你得有一个项目 2. 然后你需要一个登录页面 完整Login.cshtml视图代码戳这里-共计55行 效果预览图 !DOCTYPE html你也可以直接之前前往coding仓库查看源码要是发现bug记得提醒我啊~ LoginDemo地址 1. 首先你得有一个项目 2. 然后你需要一个登录页面 完整Login.cshtml视图代码戳这里-共计55行 效果预览图 !DOCTYPE htmlhtmlheadmeta http-equivContent-Type contenttext/html; charsetutf-8 /title登录界面/titlemeta nameviewport contentwidthdevice-width,initial-scale1.0,user-scalablenolink relstylesheet hrefhttps://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.cssstyle typetext/cssbody { 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/headbodydiv classcontainer wrap1h2 classmg-b20 text-center后台管理系统/h2div classcol-sm-8 col-md-5 center-auto pd-sm-50 pd-xs-20 main_contentp classtext-center font16用户登录/pform asp-actionLogin methodpost div classform-group mg-t20i classicon_font glyphicon glyphicon-user/iinput typetext classlogin_input asp-forUserName placeholder请输入用户名 autofocus /span asp-validation-forUserName/span/divdiv classform-group mg-t20i classicon_font glyphicon glyphicon-lock/iinput typepassword classlogin_input asp-forUserPwd placeholder请输入密码 /span asp-validation-forUserPwd/span/divdiv classcheckbox mg-b25 hidelabelinput typecheckbox记住我的登录信息 /label/divbutton typesubmit classlogin_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 GetServiceT() where T : class{ return Instance.GetServiceT();}} //一些通用的信息public static class WebContext{ public static string AdminName{ get{ //获取cookievar hasCookie ServiceLocator.GetServiceIHttpContextAccessor().HttpContext.Request.Cookies.TryGetValue(ApplicationKeys.User_Cookie_Key, out string encryptValue); if (!hasCookie || string.IsNullOrEmpty(encryptValue)) return null; var adminName ServiceLocator.GetServiceIUserService().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.AddSingletonIHttpContextAccessor, HttpContextAccessor();//用于获取请求上下文services.AddTransientIUserService, UserService();services.AddMvc();} public void Configure(IApplicationBuilder app, IHostingEnvironment env) { //app.UseMvc()..//最末的时候赋值ServiceLocator.Instance app.ApplicationServices;} 代码说明 首先定义了一个存放服务的静态对象ServiceLocator 在程序启动后将IApplicationBuilder.ApplicationServices赋值给ServiceLocator.Instance,这样就能够在任何地方使用ServiceLocator.Instance获取到注入的服务 为了更好的获取实例添加了一个T GetServiceT()方法 在WebContext中取获取Cookie值ServiceLocator.GetServiceIHttpContextAccessor().HttpContext.Request.Cookies 解密获取的cookie得到用户名ServiceLocator.GetServiceIUserService().LoginDecrypt(encryptValue, ApplicationKeys.User_Cookie_Encryption_Key); 所以在后台就能使用WebContext.AdminName获取到当前登录用户名或者根据用户名获取登录信息 总结 自定义特性标记和过滤器之间差开一个IFilterMetadata换言之特性标记实现了IFilterMetadata就等于是个过滤器(个人理解) asp.net core中模型绑定使用asp-for asp.net core注入服务: 在 Startup.ConfigureServices方法中注入 services.AddTransientIUserService, UserService() asp.net core获取HttpContext对象 参考ASP.NET Core开发之HttpContext ASP.NET Core中提供了一个IHttpContextAccessor接口HttpContextAccessor 默认实现了它简化了访问HttpContext。 它必须在程序启动时在IServicesCollection中注册这样在程序中就能获取到HttpContextAccessor并用来访问HttpContext。 services.AddSingletonIHttpContextAccessor, HttpContextAccessor(); asp.net core中表单直接使用form标签asp-action,asp-controller等指定路由参数即可并且能够自动生成防伪字段标识配合ValidateAntiForgeryToken特性标记预防CSRF 代码生成比较图 相关文档地址https://docs.microsoft.com/zh-cn/aspnet/core/security/anti-request-forgery autofocus属性 可使文本框自动获取焦点 Demo下载地址 点击下载Demo Coding仓库地址克隆代码git clone https://git.coding.net/yimocoding/WeDemo.git -b LoginDemo LoginDemo 探索学习中若有错误或者不足指出还望园友指出。 原文地址http://www.cnblogs.com/morang/p/7614537.html.NET社区新闻深度好文微信中搜索dotNET跨平台或扫描二维码关注
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/912354.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!