• 在startup中添加授权相关的管道

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection; namespace mvcforcookie
    {
    using Microsoft.AspNetCore.Authorization;
    using Microsoft.AspNetCore.Authentication.Cookies;
    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)
    {
    services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(option => option.LoginPath = "/Acounnt/Index");
    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)
    {
    if (env.IsDevelopment())
    {
    app.UseDeveloperExceptionPage();
    }
    else
    {
    app.UseExceptionHandler("/Home/Error");
    }
    app.UseStaticFiles();
    app.UseAuthentication();
    app.UseMvc(routes =>
    {
    routes.MapRoute(
    name: "default",
    template: "{controller=Home}/{action=Index}/{id?}");
    });
    }
    }
    }
  • 将需要权限访问的页面贴上特性标签 
    [Authorize(Roles="Admin")] 表名只有Admin身份的人才能进入Admin控制器
  • 用户成功输入用户名和密码之后生成用户票据
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using mvcforcookie.Models; namespace mvcforcookie.Controllers
    {
    using Microsoft.AspNetCore.Authorization;
    using Microsoft.AspNetCore.Authentication.Cookies;
    using Microsoft.AspNetCore.Authentication;
    using System.Security.Claims;
    public class AcounntController : Controller
    {
    public IActionResult Index()
    {
    //数据库查询用户输入的用户名和密码等一系列匹配操作
    //模拟用户登录后的操作
    //创建一个用户身份
    var claims=new List<Claim>{
    new Claim(ClaimTypes.Name,"cyao"),
    new Claim(ClaimTypes.Role,"Admin")
    };
    var claimidentity=new ClaimsIdentity(claims,CookieAuthenticationDefaults.AuthenticationScheme);
    //向上下文容器中添加当前用户
    HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,new ClaimsPrincipal(claimidentity));
    return Ok();
    }
    public IActionResult LoginOut()
    {
    HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
    return Ok();
    }
    }
    }
  • 如果要获取当前用户的身份和用户名的话

          ViewBag.User= User.Claims.Where(c =>c.Type==ClaimTypes.Name).First().Value;
    ViewBag.Type= User.Claims.Where(c =>c.Type==ClaimTypes.Role).First().Value;

.net core 学习小结之 Cookie-based认证的更多相关文章

  1. .net core 学习小结之 JWT 认证授权

    新增配置文件 { "Logging": { "IncludeScopes": false, "Debug": { "LogLeve ...

  2. .net core 学习小结之环境配置篇

    安装IIs对 netcore 的支持 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/aspnet-core-mod ...

  3. .net core 学习小结之 自定义JWT授权

    自定义token的验证类 using System; using System.Collections.Generic; using System.IO; using System.Linq; usi ...

  4. .net core 学习小结之 PostMan报415

    首先415的官方解释是:对于当前请求的方法和所请求的资源,请求中提交的实体并不是服务器中所支持的格式,因此请求被拒绝. 也就是说我所准备的数据格式并不是后台代码使用的数据格式 后台代码如下 using ...

  5. .net core 学习小结之 配置介绍(config)以及热更新

    命令行的配置 var settings = new Dictionary<string, string>{ { "name","cyao"}, {& ...

  6. ASP.NET CORE中使用Cookie身份认证

    大家在使用ASP.NET的时候一定都用过FormsAuthentication做登录用户的身份认证,FormsAuthentication的核心就是Cookie,ASP.NET会将用户名存储在Cook ...

  7. NET Core 2.0使用Cookie认证实现SSO单点登录

    NET Core 2.0使用Cookie认证实现SSO单点登录 之前写了一个使用ASP.NET MVC实现SSO登录的Demo,https://github.com/bidianqing/SSO.Sa ...

  8. Asp.net core 学习笔记 ( identity server 4 JWT Part )

    更新 : id4 使用这个 DbContext 哦 dotnet ef migrations add identity-server-init --context PersistedGrantDbCo ...

  9. .NET Core 学习资料精选:入门

    开源跨平台的.NET Core,还没上车的赶紧的,来不及解释了-- 本系列文章,主要分享一些.NET Core比较优秀的社区资料和微软官方资料.我进行了知识点归类,让大家可以更清晰的学习.NET Co ...

随机推荐

  1. 如何使用windows performance recorder

    先下载WPA TOOLS:从该地址下载,选最新的版本,然后可以只选择下载WPA工具 后面编写XML文件等等,可以参考这篇文章. 需要注意: 用管理员启动cmd后,如果想运行特定路径的文件,需要带上绝对 ...

  2. Python之网络编程之concurrent.futures模块

    需要注意一下不能无限的开进程,不能无限的开线程最常用的就是开进程池,开线程池.其中回调函数非常重要回调函数其实可以作为一种编程思想,谁好了谁就去掉 只要你用并发,就会有锁的问题,但是你不能一直去自己加 ...

  3. MySQL 创建唯一索引忽略对已经重复数据的检查

    MySQL 创建唯一索引忽略对已经重复数据的检查 在创建唯一索引的基础上加上关键字"IGNORE "即可.(注意,经测试,在5.7版本已经不再支持该参数) # 重复数据 mysql ...

  4. 树莓派Zero W无键盘无屏幕初始化

    买了个树莓派Zero W,结果发现没mini hdmi线 淘宝了一根mini hdmi,等了5天,拿到手后发现zero还没地接键盘,这下脑袋大了 查了下资料,发现树莓的系统,可以这boot分区中加入一 ...

  5. PIXI如何绘制离屏canvas到舞台上

    有个方法是toDataURL(),原生的,先转换成图片再绘制. 但是pixi提供了一个BaseTexture,其构造函数的参数可以是一个canvas 因此可以直接使用如下代码绘制canvas //微信 ...

  6. 对前端Jenkins自动化部署的研究

    1. 安装 安装 Nginx 1.1去官网下直接下载,解压缩 start nginx就可以使了,常用命令: start nginx # 启动 nginx -s reload # 修改配置后重新加载生效 ...

  7. ReentrantReadWriteLock源码分析笔记

    ReentrantReadWriteLock包含两把锁,一是读锁ReadLock, 此乃共享锁, 一是写锁WriteLock, 此乃排它锁. 这两把锁都是基于AQS来实现的. 下面通过源码来看看Ree ...

  8. 这里面ID为002和005的记录是重复的,在这里要把其中一条去掉,达到下面的效果:

    --去掉重复的记录 select ID,Code,ColorNum from (     SELECT      ROW_NUMBER() OVER(         PARTITION BY Cod ...

  9. udp拼接传递数据包

    1.拼接项少 pl = ["<0112>","<32>","<1024x768>","< ...

  10. SpringBoot 入门实战篇

    SpringBoot入门 使用SpringBoot + mysql + Jpa 快速搭建一个spring项目 Spring Boot 2 + Spring Data JPA + MySQL 8 簡單範 ...