• 在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. jquery预览本地图片

    本人qq群也有许多的技术文档,希望可以为你提供一些帮助(非技术的勿加). QQ群:   281442983 (点击链接加入群:http://jq.qq.com/?_wv=1027&k=29Lo ...

  2. python中导入from appium import webdriver时报错:ModuleNotFoundError: No module named 'appium'

    1.检查一下有没有安装Appium-Python-Client,执行语句:pip install Appium-Python-Client进行安装 2.安装后,出现ModuleNotFoundErro ...

  3. 编译安装cmake

    安装cmake 1.为什么用cmake?    mysql部分版本安装前编译需要用软件cmake,而不是我们之前通常使用的make! 百度百科:CMake 可以编译源代码.制作程式库.产生适配器(wr ...

  4. MySQL优化建议与使用规范

    适用场景:并发量大.数据量大的互联网业务;可以先阅读必须掌握的MySQL优化指南 一.基础规范 (1)必须使用InnoDB存储引擎 解读:支持事务.行级锁.并发性能更好.CPU及内存缓存页优化使得资源 ...

  5. Python之常用模块三(面向对象相关的三个模块)

    hashlib.configparser.logging模块 一.常用模块二 hashlib模块 hashlib提供了常见的摘要算法,如md5和sha1等等. 那么什么是摘要算法呢?摘要算法又称为哈希 ...

  6. python类库32[序列化和反序列化之pickle]

      一 pickle pickle模块用来实现python对象的序列化和反序列化.通常地pickle将python对象序列化为二进制流或文件.   python对象与文件之间的序列化和反序列化: pi ...

  7. Hive 中的 LEFT SEMI JOIN 与 JOIN ON

    hive 的 join 类型有好几种,其实都是把 MR 中的几种方式都封装实现了,其中 join on.left semi join 算是里边具有代表性,且使用频率较高的 join 方式. 1.联系 ...

  8. C++ 遍历循环表达示 auto, auto&, auto&&

    for(auto x : range) 创建拷贝,无法修改range中的元素 for(auto& x : range) 可以修改range中的元素,但一般用以下这种 for(auto& ...

  9. 【leetcode】Reorganize String

    题目如下: Given a string S, check if the letters can be rearranged so that two characters that are adjac ...

  10. cmd命令行的FTP使用

    进入ftp:ftp 打开连接:open 192.168.1.106 2121 用户名空:none 密码空:不用输入,直接回车 查询远程服务器当前路径:pwd 显示远程服务器当前路径下的文件:dir 远 ...