推荐文章

ASP.NET Core中使用百度在线编辑器UEditor

ASP.NET Core中使用百度在线编辑器UEditor

ASP.NET Core中使用百度在线编辑器UEditor
.net core添加Area区域

.net core添加Area区域

Asp. Ce使用Area区域,可以有效的对业务进行隔离,各种业务及分工可以更灵活。在Asp. Ce中启用区域也是极简单的,比如:一个网站有前端、用户后台、管理,我们使用Area就能清晰划分每个功能板块前端:使用用默认的路由用户:使用“Users”路由管理:使用“Admin”路由1、在“Startup.cs”,添加Area路由public void Configure(IApplicationBu
ASP.NET CORE对url参数的编码

ASP.NET CORE对url参数的编码

用URL传参数的时候,用&符号连接,如果某一个参数中含"#$ ^ & + ="这些符号的时候,在另一个页面就会取不到传过来的参数。解决办法:ASP. CE:编码var url="http:www.baidu.com&text=" + Uri.EscapeDataString("(&^%$#@!!")输出:http:www.baidu.com&text=%28%2A%26%5E%25%24%23
ASP.NET Core 2.0使用NLog日志配置

ASP.NET Core 2.0使用NLog日志配置

一、新建ASP. Ce 2.0 MVC项目,使用NuGet在浏览中搜索:NLog.Web.AspCe,如下图所示:二、在项目下新建一个xml类型的nlog.config文件《?xml version="1.0" encoding="utf8"?》《nlog xmlns="http:www.nlogproject.gschemasNLog.xsd" xmlns:xsi="http:www.
asp.net core使用IHttpContextAccessor获取来源页

asp.net core使用IHttpContextAccessor获取来源页

1、在Startup文件注入IHttpContextAccesspublic void ConfigureServices(IServiceCollection services) { ... 注入HttpContextAccess services.AddSingleton《IHttpContex

ASP.NET core多角色登录权限

日期:2018-07-15 点击:2211 来源:PB2.CN

微软Asp.Net Core中的身份认证与之前相比使用更加便捷,使用Authentication的登录认证变得简单了许多。
长话短说,开始新建工程,进行实践操作:


一、新建项目:asp.net core web 应用

新建项目.png

选择asp.net core版本,这里我们选择最新的asp.net core 2.1

新建项目2.png


二、数据模型

   1、在Models文件夹下添加Users.cs

public class Users
{
        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName { set; get; }
        /// <summary>
        /// 密码
        /// </summary>
        public string Password { set; get; }
        /// <summary>
        /// 昵称
        /// </summary>
        public string Nickname { set; get; }
        /// <summary>
        /// 角色  Admin普通用户,SuperAdmin超级用户
        /// </summary>
        public string Role { set; get; }
        /// <summary>
        /// 角色权限
        /// </summary>
        public string Policy { set; get; }
}

   2、新建Data文件夹,添加DB.cs,模拟用户数据库

public static List<Users> Get()
{
  var list = new List<Users>
  {
   //普通用户
   new Users{UserName="001",Password="12345",Nickname="张三",Role="Admin",Policy="ADD,UPDATE,DEL,SHOW"},
   new Users{UserName="002",Password="12345",Nickname="李四",Role="Admin",Policy="ADD,SHOW"},
   new Users{UserName="003",Password="12345",Nickname="王五",Role="Admin",Policy="UPDATE,SHOW"},
   new Users{UserName="004",Password="12345",Nickname="小六",Role="Admin",Policy="DEL,SHOW"},
   new Users{UserName="005",Password="12345",Nickname="七仔",Role="Admin",Policy="SHOW"},
   //超级用户
   new Users{UserName="101",Password="12345",Nickname="老板",Role="SuperAdmin",Policy="ADD,UPDATE,DEL,SHOW"},
   new Users{UserName="102",Password="12345",Nickname="项目经理",Role="SuperAdmin",Policy="ADD,SHOW"},
   new Users{UserName="103",Password="12345",Nickname="总监",Role="SuperAdmin",Policy="UPDATE,SHOW"},
   new Users{UserName="104",Password="12345",Nickname="小鬼",Role="SuperAdmin",Policy="DEL,SHOW"},
   new Users{UserName="104",Password="12345",Nickname="苦力",Role="SuperAdmin",Policy="SHOW"},
   };
   return list;
}



三、添加Areas区域文件夹

    1、在Areas目录里添加普通用户区域Admin

添加区域.png


    2、在Admin目录里添加AdminAuthorize权限认证

普通用户Authorize.png

    

    3、在Admin目录里添加DefaultController控制器

在DefaultController前面加上路由和[AdminAuthorize]认证

普通用户控制器.png



同理添加超级用户控制器(DefaultController)和权限(SuperAdminAuthorize)

Areas.png


四、打开跟目录的startup.cs,在startup.cs里添加注入与中间件
 ConfigureServices 添加注入

public void ConfigureServices(IServiceCollection services){
....
#region 用户权限注入
   //用户权限 
   services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
    .AddCookie(SuperAdminAuthorize.SuperAdminAuthorizeScheme, option =>
    {
     //超级用户
         option.LoginPath = new PathString("/Home/Index");//未登录状态
         option.AccessDeniedPath = new PathString("/SuperAdmin/Default/Index");//无权限状态
     }).AddCookie(AdminAuthorize.AdminAuthorizeScheme, option =>
     {
          //普通用户
         option.LoginPath = new PathString("/Home/Index");//未登录状态
         option.AccessDeniedPath = new PathString("/Admin/Default/Index");//无权限状态
     });
     services.AddAuthorization(options =>
     {
         //权限策略
         options.AddPolicy("ADD", policy => policy.Requirements.Add(new PolicyRequirement("ADD")));
         options.AddPolicy("UPDATE", policy => policy.Requirements.Add(new PolicyRequirement("UPDATE")));
         options.AddPolicy("DEL", policy => policy.Requirements.Add(new PolicyRequirement("DEL")));
         options.AddPolicy("SHOW", policy => policy.Requirements.Add(new PolicyRequirement("SHOW")));
      });
      //权限策略注入
      services.AddSingleton<IAuthorizationHandler, PolicyHandler>();
     //用户权限 
 #endregion 
....
}

  Configure 添加中间件

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
  ...
    //验证中间件
    app.UseAuthentication();
    app.UseCookiePolicy();
    app.UseMvc(routes =>
   {
      //区域路由
      routes.MapRoute(
         name: "areas",
          template: "{area:exists}/{controller=Default}/{action=Index}/{id?}"
       );
      //区域路由
      routes.MapRoute(
         name: "default",
         template: "{controller=Home}/{action=Index}/{id?}");
      });
}

注入.png

五、登录界面

登录界面.png


六、验证登录


这篇文档对您是否有帮助?

微信扫码下载源程序

ASP.NET Core中使用百度在线编辑器UEditor

ASP.NET Core中使用百度在线编辑器UEditor

ASP.NET Core中使用百度在线编辑器UEditor
.net core添加Area区域

.net core添加Area区域

Asp. Ce使用Area区域,可以有效的对业务进行隔离,各种业务及分工可以更灵活。在Asp. Ce中启用区域也是极简单的,比如:一个网站有前端、用户后台、管理,我们使用Area就能清晰划分每个功能板块前端:使用用默认的路由用户:使用“Users”路由管理:使用“Admin”路由1、在“Startup.cs”,添加Area路由public void Configure(IApplicationBu
ASP.NET CORE对url参数的编码

ASP.NET CORE对url参数的编码

用URL传参数的时候,用&符号连接,如果某一个参数中含"#$ ^ & + ="这些符号的时候,在另一个页面就会取不到传过来的参数。解决办法:ASP. CE:编码var url="http:www.baidu.com&text=" + Uri.EscapeDataString("(&^%$#@!!")输出:http:www.baidu.com&text=%28%2A%26%5E%25%24%23
ASP.NET Core 2.0使用NLog日志配置

ASP.NET Core 2.0使用NLog日志配置

一、新建ASP. Ce 2.0 MVC项目,使用NuGet在浏览中搜索:NLog.Web.AspCe,如下图所示:二、在项目下新建一个xml类型的nlog.config文件《?xml version="1.0" encoding="utf8"?》《nlog xmlns="http:www.nlogproject.gschemasNLog.xsd" xmlns:xsi="http:www.
asp.net core使用IHttpContextAccessor获取来源页

asp.net core使用IHttpContextAccessor获取来源页

1、在Startup文件注入IHttpContextAccesspublic void ConfigureServices(IServiceCollection services) { ... 注入HttpContextAccess services.AddSingleton《IHttpContex