网络优化seo招聘优化搜索引擎

web/2025/9/30 6:26:14/文章来源:
网络优化seo招聘,优化搜索引擎,石家庄seo推广,宿迁做网站多少钱在《asp.net core认证与授权》中讲解了固定和自定义角色授权系统权限#xff0c;其实我们还可以通过其他方式来授权#xff0c;比如可以通过角色组#xff0c;用户名#xff0c;生日等#xff0c;但这些主要取决于ClaimTypes#xff0c;其实我们也可以自定义键值来授权其实我们还可以通过其他方式来授权比如可以通过角色组用户名生日等但这些主要取决于ClaimTypes其实我们也可以自定义键值来授权这些统一叫策略授权其中更强大的是我们可以自定义授权Handler来达到灵活授权下面一一展开。 注意下面的代码只是部分代码完整代码参照https://github.com/axzxs2001/Asp.NetCoreExperiment/tree/master/Asp.NetCoreExperiment/%E6%9D%83%E9%99%90%E7%AE%A1%E7%90%86/PolicyPrivilegeManagement 首先看基于角色组或用户名或基于ClaimType或自定义键值等授权策略这些都是通过Services.AddAuthorization添加并且是AuthorizationOptions来AddPolicy这里策略的名称统一用RequireClaim来命名不同的请求的策略名称各不相同如用户名时就用policy.RequireUserName()同时在登录时验证成功后要添加相应的Claim到ClaimsIdentity中 Startup.cs public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddAuthorization(options { //基于角色组的策略 options.AddPolicy(RequireClaim, policy policy.RequireRole(admin, system)); //基于用户名 //options.AddPolicy(RequireClaim, policy policy.RequireUserName(桂素伟)); //基于ClaimType //options.AddPolicy(RequireClaim, policy policy.RequireClaim(ClaimTypes.Country,中国)); //自定义值 // options.AddPolicy(RequireClaim, policy policy.RequireClaim(date,2017-09-02));                 }).AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options { options.LoginPath new PathString(/login); options.AccessDeniedPath new PathString(/denied); });  } HomeController.cs  using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using PolicyPrivilegeManagement.Models; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.Cookies; using System.Security.Claims; namespace PolicyPrivilegeManagement.Controllers { [Authorize(Policy RequireClaim)] public class HomeController : Controller {        public IActionResult Index() { return View(); } public IActionResult About() { ViewData[Message] Your application description page.; return View(); } public IActionResult Contact() { ViewData[Message] Your contact page.; return View(); } public IActionResult Error() { return View(new ErrorViewModel { RequestId Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } [AllowAnonymous] [HttpGet(login)] public IActionResult Login(string returnUrl null) { TempData[returnUrl] returnUrl; return View(); } [AllowAnonymous] [HttpPost(login)] public async TaskIActionResult Login(string userName, string password, string returnUrl null) { var list new Listdynamic { new { UserName gsw, Password 111111, Role admin,Name桂素伟,Country中国,Date2017-09-02,BirthDay1979-06-22}, new { UserName aaa, Password 222222, Role system,Name测试A ,Country美国,Date2017-09-03,BirthDay1999-06-22} }; var user list.SingleOrDefault(s s.UserName userName s.Password password); if (user ! null) { //用户标识 var identity new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme); identity.AddClaim(new Claim(ClaimTypes.Sid, userName)); identity.AddClaim(new Claim(ClaimTypes.Name, user.Name)); identity.AddClaim(new Claim(ClaimTypes.Role, user.Role)); identity.AddClaim(new Claim(ClaimTypes.Country, user.Country)); identity.AddClaim(new Claim(date, user.Date)); await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity)); if (returnUrl null) { returnUrl TempData[returnUrl]?.ToString(); } if (returnUrl ! null) { return Redirect(returnUrl); } else { return RedirectToAction(nameof(HomeController.Index), Home); } } else { const string badUserNameOrPasswordMessage 用户名或密码错误; return BadRequest(badUserNameOrPasswordMessage); } } [HttpGet(logout)] public async TaskIActionResult Logout() { await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); return RedirectToAction(Index, Home); } [AllowAnonymous] [HttpGet(denied)] public IActionResult Denied() { return View(); } } } 上面的授权策略都相对简单单一使用场景也很有限就和固定角色授权如出一辙其实可以用更好的来例用授权那就是自定义授权Handler我们在《asp.net core认证与授权》一文中是通过中间件来达到自定义解色的现在我们换个思路通过自定义授权Handler来实现。 首先定义一个UserPermission即用户权限实体类 /// summary /// 用户权限 /// /summary public class UserPermission { /// summary /// 用户名 /// /summary public string UserName { get; set; } /// summary /// 请求Url /// /summary public string Url { get; set; } } 接下来定义一个PermissionRequirement为请求条件实体类 /// summary /// 必要参数类 /// /summary public class PermissionRequirement : IAuthorizationRequirement { /// summary /// 用户权限集合 /// /summary public  ListUserPermission UserPermissions { get;private set; } /// summary /// 无权限action /// /summary public string DeniedAction { get; set; } /// summary /// 构造 /// /summary /// param namedeniedAction无权限action/param /// param nameuserPermissions用户权限集合/param public PermissionRequirement(string deniedAction, ListUserPermission userPermissions) { DeniedAction deniedAction; UserPermissions userPermissions; } } 再定义自定义授权Hanlder我们命名为PermissionHandler此类必需继承AuthorizationHandlerT只用实现public virtual Task HandleAsync(AuthorizationHandlerContext context)些方法是用户请求时验证是否授权的主方法所以实现与自定义角色中间件的Invoke很相似。 using Microsoft.AspNetCore.Authorization; using System.Collections.Generic; using System.Linq; using System.Security.Claims; using System.Threading.Tasks; namespace PolicyPrivilegeManagement.Models { /// summary /// 权限授权Handler /// /summary public class PermissionHandler : AuthorizationHandlerPermissionRequirement { /// summary /// 用户权限 /// /summary public ListUserPermission UserPermissions { get; set; } protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, PermissionRequirement requirement) { //赋值用户权限 UserPermissions requirement.UserPermissions; //从AuthorizationHandlerContext转成HttpContext以便取出表求信息 var httpContext (context.Resource as Microsoft.AspNetCore.Mvc.Filters.AuthorizationFilterContext).HttpContext; //请求Url var questUrl httpContext.Request.Path.Value.ToLower(); //是否经过验证 var isAuthenticated httpContext.User.Identity.IsAuthenticated; if (isAuthenticated) { if (UserPermissions.GroupBy(g g.Url).Where(w w.Key.ToLower() questUrl).Count() 0) { //用户名 var userName httpContext.User.Claims.SingleOrDefault(s s.Type ClaimTypes.Sid).Value; if (UserPermissions.Where(w w.UserName userName w.Url.ToLower() questUrl).Count() 0) { context.Succeed(requirement); } else { //无权限跳转到拒绝页面 httpContext.Response.Redirect(/denied); } } else { context.Succeed(requirement); } } return Task.CompletedTask; } } } 此次的Startup.cs的ConfigureServices发生了变化如下 public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddAuthorization(options {   //自定义RequirementuserPermission可从数据库中获得 var userPermission new ListUserPermission { new UserPermission {  Url/, UserNamegsw}, new UserPermission {  Url/home/permissionadd, UserNamegsw}, new UserPermission {  Url/, UserNameaaa}, new UserPermission {  Url/home/contact, UserNameaaa} }; options.AddPolicy(Permission, policy policy.Requirements.Add(new PermissionRequirement(/denied, userPermission))); }).AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options { options.LoginPath new PathString(/login); options.AccessDeniedPath new PathString(/denied); }); //注入授权Handler services.AddSingletonIAuthorizationHandler, PermissionHandler(); } HomeController中代码如下 using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using PolicyPrivilegeManagement.Models; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.Cookies; using System.Security.Claims; namespace PolicyPrivilegeManagement.Controllers { [Authorize(Policy Permission)]    public class HomeController : Controller { PermissionHandler _permissionHandler; public HomeController(IAuthorizationHandler permissionHandler) { _permissionHandler permissionHandler as PermissionHandler; } public IActionResult Index() { return View(); } public IActionResult PermissionAdd() {            return View(); } [HttpPost(addpermission)] public IActionResult AddPermission(string url,string userName) {        //添加权限 _permissionHandler.UserPermissions.Add(new UserPermission { Url url, UserName userName }); return Content(添加成功); } public IActionResult Contact() { ViewData[Message] Your contact page.; return View(); } public IActionResult Error() { return View(new ErrorViewModel { RequestId Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } [AllowAnonymous] [HttpGet(login)] public IActionResult Login(string returnUrl null) { TempData[returnUrl] returnUrl; return View(); } [AllowAnonymous] [HttpPost(login)] public async TaskIActionResult Login(string userName, string password, string returnUrl null) { var list new Listdynamic { new { UserName gsw, Password 111111, Role admin,Name桂素伟,Country中国,Date2017-09-02,BirthDay1979-06-22}, new { UserName aaa, Password 222222, Role system,Name测试A ,Country美国,Date2017-09-03,BirthDay1999-06-22} }; var user list.SingleOrDefault(s s.UserName userName s.Password password); if (user ! null) { //用户标识 var identity new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme); identity.AddClaim(new Claim(ClaimTypes.Sid, userName)); identity.AddClaim(new Claim(ClaimTypes.Name, user.Name)); identity.AddClaim(new Claim(ClaimTypes.Role, user.Role)); identity.AddClaim(new Claim(ClaimTypes.Country, user.Country)); identity.AddClaim(new Claim(date, user.Date)); await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity)); if (returnUrl null) { returnUrl TempData[returnUrl]?.ToString(); } if (returnUrl ! null) { return Redirect(returnUrl); } else { return RedirectToAction(nameof(HomeController.Index), Home); } } else { const string badUserNameOrPasswordMessage 用户名或密码错误; return BadRequest(badUserNameOrPasswordMessage); } } [HttpGet(logout)] public async TaskIActionResult Logout() { await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); return RedirectToAction(Index, Home); } [AllowAnonymous] [HttpGet(denied)] public IActionResult Denied() { return View(); } } } 本例设计是当用户gsw密码111111登录时是不能访问/home/contact的刚登录时访该action是不成功的这里我们在/home/addpermission中添加一个Action名称:/home/contact用户名:gsw的信息此时再访问/home/contact会发现是可以访问的这是因为我们热更新了PermissionHandler中的用户权限集合用户的权限得到了扩展和变化。 其实用中间件能达到灵活权限的设置用自定义授权Handler也可以接下来比较一下两种做法的优劣 中间件自定义授权Handler用户权限集合静态对象实体化对象热更新时用中间件名称.用户权限集合更新因为在Startup.cs中PermissionHandler是依赖注放的可以在热更新的构造中获取并操作性能方面每个action请求都会触发Invock方法标记[AllowAnonymous]特性的Action也会触发只有标记[Authorize]特性的Action会触发该方法标记[AllowAnonymous]特性的Action不会触发性能更优化 相关文章  .NET Core 2.0 正式发布信息汇总.NET Standard 2.0 特性介绍和使用指南.NET Core 2.0 的dll实时更新、https、依赖包变更问题及解决.NET Core 2.0 特性介绍和使用指南Entity Framework Core 2.0 新特性体验 PHP under .NET Core.NET Core 2.0使用NLog升级项目到.NET Core 2.0在Linux上安装Docker并成功部署解决Visual Studio For Mac Restore失败的问题ASP.NET Core 2.0 特性介绍和使用指南.Net Core下通过Proxy 模式 使用 WCF.NET Core 2.0 开源Office组件 NPOIASP.NET Core Razor页面 vs MVCRazor Page–Asp.Net Core 2.0新功能  Razor Page介绍MySql 使用 EF Core 2.0 CodeFirst、DbFirst、数据库迁移Migration介绍及示例.NET Core 2.0迁移技巧之web.config配置文件asp.net core MVC 过滤器之ExceptionFilter过滤器(一)ASP.NET Core 使用Cookie验证身份ASP.NET Core MVC – Tag Helpers 介绍ASP.NET Core MVC – Caching Tag HelpersASP.NET Core MVC – Form Tag HelpersASP.NET Core MVC – 自定义 Tag HelpersASP.NET Core MVC – Tag Helper 组件ASP.Net Core Razor 页面路由粗略使用.NetCore2.0自带授权登陆Authorize 原文地址http://www.cnblogs.com/axzxs2001/p/7482777.html .NET社区新闻深度好文微信中搜索dotNET跨平台或扫描二维码关注

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/84303.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

贵金属企业网站源码宣传网站开发

一、函数指针 1、定义 顾名思义,函数指针就是函数的指针。它是一个指针,属于一个数据类型,其指向一个函数。如定义一个函数,其入口地址就是这个函数的指针,是个常量,可以用该常量给函数指针类型的变量赋值&…

网站开发语言识别北京优秀的网站建设公司

python缓冲区When people who speak different languages get together and talk, they try to use a language that everyone in the group understands. 当说不同语言的人聚在一起聊天时,他们会尝试使用小组中每个人都能理解的语言。 To achieve this, everyone …

合肥网站设计公司怎样让百度搜不到自己的网站

一、NLP是什么 自然语言处理( Natural Language Processing, NLP)是计算机科学领域与人工智能领域中的一个重要方向,也就是人们常说的「自然语言处理」,就是研究如何让计算机读懂人类语言,即将人的自然语言转换为计算机可以阅读的指令。它研…

盐城集团网站建设南通网站排名优化

大模型背后的范式 整个预训练语言模型的使用范式: 对于预训练模型,最核心的要素是从无标注的数据中去学习,通过自监督的一些任务去做预训练,得到丰富的知识。在具体的应用中,会引入一些任务相关的数据,去调…

米各庄网站建设遂宁模板建站公司

maven:编译出现Process terminated解决方法(超全) 一. 情况一:配置文件 settings. xml 出错(解决方法1)1.1 项目编译报错如下:1.2 点击【项目名】提示找到出错文件1.3 点击查看出错文件1.4 原因及解决办法 …

制作网页的网站有哪些建设银行网站连不上

今天又学会了一个知识,加油! 目录 一、基带信号与宽带信号 1、基带信号 2、宽带信号 3、选择 4、关系 二、数字数据编码为数字信号 1、非归零编码【NRZ】 2、曼彻斯特编码 3、差分曼彻斯特编码 4、归零编码【RZ】 5、反向不归零编码【NRZI】 …

大学网页制作与网站建设开通网站需要什么手续

文章首发地址 为了解决在云原生环境中,Java应用启动慢的问题,出现了很多派系,如拯救派,让应用在原有基础上启动更快(一般都是用资源换时间),还有就是革命派,Java向Golang学习&#x…

三亚网站建设公司移动网站建设是什么意思

安卓工具箱专业版是款集所有功能于一身的工具箱!包括硬件和软件和工具,您的手机使用的信息。非常容易使用,具有非常用户友好的用户界面。 主要功能: 1。硬件信息:CPU核心,CPU类型,内存信息&…

胡芦娃app软件下载网站前端开发工具哪个好

Linux——权限的理解 文章目录 Linux——权限的理解一、shell命令以及运行原理二、Linux权限的概念切换用户对指令提权 三、Linux权限管理1. 文件访问者的分类(人)2. 文件类型和访问权限(事物属性)文件类型基本权限文件权限值的表…

手机网站开发步骤网站建设的基本技术

泛型 基本概念为什么我们需要泛型泛型类型泛型类简单泛型类多元泛型类 泛型接口泛型方法为什么要使用泛型方法呢?使用方法 泛型的上下限上限下限加点难度的例子例子一例子二例子三 泛型数组深入理解泛型什么是泛型擦除后保留的原始类型泛型类型擦除原则如何进行擦除的?怎么证…

网站建设教程培训中国十大经典广告

1. 请自我介绍一下(需简单清楚的表述自已的基本情况,在这过程中要展现出自信,对工作有激情,上进,好学) 面试官您好,我叫###,今年26岁,来自江西九江,就读专业是电子商务,毕…

素材网站下载专做智能化施工的网站

背景 随着酒店业务的高速发展,我们为用户、商家提供的服务越来越精细,系统服务化程度、复杂度也逐渐上升。微服务化虽然能够很好地解决问题,但也有副作用,比如,问题定位。 每次问题定位都需要从源头开始找同事帮我人肉…

各大网站网址奉贤做网站的

apache shiro 反序列化漏洞解决方案 反序列化漏洞解决方案产生原因解决方案1:1.升级shiro至最新版本1.7.1解决方案2:修改rememberMe默认密钥,生成随机密钥。 反序列化漏洞解决方案 反序列化漏洞介绍 序列化:把对象转换为字符串或…

网站照片要求南阳企业网站推广方法

一:Settings一级菜单 1、AndroidManifest.xml 每个APP对应都有一个AndroidManifest.xml,从该文件入手分析最为合适。 packages/apps/Settings/AndroidManifest.xml 根据<category android:name="android.intent.category.LAUNCHER" />可找到当前当前APP a…

聊城网站建设公司电话商城购物网站开发意义

前言 在上一章节中我们用W5500-EVB-PICO通过dhcp获取ip地址&#xff08;网关&#xff0c;子网掩码&#xff0c;dns服务器&#xff09;等信息&#xff0c;给我们的开发板配置网络信息&#xff0c;成功的接入网络中&#xff0c;那么本章将教大家如何让我们的开发板进行DNS域名解析…

密云做网站的中国建行官网首页

Kafka_04_Topic和日志 Topic/PartitionTopicPartition 日志存储存储格式日志清理删除压缩 Topic/Partition Topic/Partition: Kafka中消息管理的基础单位 Topic和Partition并不实际存在(仅逻辑上的概念) 如: Topic和Partition关系 // 每个日志文件可对应多个日志分段, 其还可…

建立个人网站主题新中式装修风格效果图

最近遇到一个python的小数的问题&#xff0c;本来应该很简单的小于判断&#xff0c;无论如何都不正确&#xff0c;而且浮点小数都没问题&#xff0c;但decimal小数有问题&#xff0c;给我整蒙了&#xff0c;后来才发现是对decimal不了解所致&#xff0c;如果你还用float转decim…

网站管理建设落实报告wordpress register

问题描述&#xff1a;vue3项目的页面A跳转到页面B时&#xff0c;页面B页面是空白的&#xff0c;需要手动刷新一下才能恢复正常&#xff0c;在页面A中用iframe引入了别的网站&#xff08;后续事实证明&#xff0c;跟iframe没一毛钱的关系&#xff09;。着急的童鞋可以直接拉到底…

工信部网站备案查询步骤详解五合一免费建站

1 赛题思路 (赛题出来以后第一时间在群内分享&#xff0c;点击下方群名片即可加群) 2 比赛日期和时间 报名截止时间&#xff1a;2024年4月11日&#xff08;周四&#xff09;12:00 比赛开始时间&#xff1a;2024年4月12日&#xff08;周五&#xff09;8:00 比赛结束时间&…

山西长治做网站公司又拍网站怎么做的

1&#xff09;控制标签体内容是否输出 2&#xff09;控制标签余下内容是否输出 3&#xff09;控制重复输出标签体内容 4&#xff09;改变标签体内容 5&#xff09;带属性的标签 package com.loaderman.demo.a_tag;import java.io.IOException; import java.io.StringWriter;imp…