• 在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. Linux下vim显示行数

    在Linux环境下的编辑器有vi.vim.gedit等等.进入这些编辑器之后,为了方便我们需要编辑器显示出当前的行号,可偏偏编辑器默认是不会显示行号的.我们有二种办法可以解决: 第一种是,手动显示:在 ...

  2. ubuntu 16.04 安装Opencv-3.2.0_GPU 与 opencv_contrib-3.2.0

    1.准备依赖库 sudo apt-get install build-essential sudo apt-get install cmake git libgtk2.0-dev pkg-config ...

  3. springcloud 高可用的服务注册中心

    https://blog.csdn.net/forezp/article/details/81041101 上面是方老师的博客,看liuyan也有好多同学不是很清楚,这里自己也记录一下具体的做法. 1 ...

  4. 在python里使用WriteProcessMemory修改内存地址上的值

    import os import sys from ctypes import * windll.kernel32.WriteProcessMemory.argtypes = [c_void_p, c ...

  5. 【转】原生js实现移动端h5长按事件

    $("#target").on({ touchstart: function(e) { // 长按事件触发 timeOutEvent = setTimeout(function() ...

  6. Puppet利用Nginx多端口实现负载均衡

    随着公司应用需求的增加,需要不断的扩展,服务器数量也随之增加,当服务器数量不断增加,我们会发现一台puppetmaster压力大,解析缓慢,而且时不时出现"time out"之类的 ...

  7. Java内存区域笔记(一)

    程序计数器: 程序计数器是一块较小的内存空间,它可以看作当前线程所执行的字节码的行号指示器. 字节码解释器工作时就是通过改变这个计数器的值来选取下一条需要执行的字节码指令. 由于Java虚拟机的多线程 ...

  8. 【初中部 NOIP提高组 二试】模拟赛B总结

    今天的比赛大翻车了,惨败. 第一题是一道模拟找规律题,由于一些计算错误,没有认真对拍,本来送的100分有40分飞走了~~ 第二题是在一个结论基础上的dp加单调队列,比赛上没有搞出那个结论,打了个n^3 ...

  9. OpenQA.Selenium.Chrome Action滑动操作

    //[定位] //滑块元素 var wrapper = driver.FindElement(By.XPath("/html/body/div/div[1]/div[1]/div[2]&qu ...

  10. Selenium 三种等待方式详解

    我们在做WEB自动化时,一般要等待页面元素加载完成后,才能执行操作,否则会报找不到元素的错误,这样就要求我们在有些场景下加等待时间. 我们平常用到的有三种等待方式: 强制等待 隐式等待 显示等待 一. ...