• 在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;

最新文章

  1. Spring的3种切入点PointCut实现
  2. windows服务
  3. paper 106:图像增强方面的介绍
  4. js格式化数字和金额
  5. atitit.标准时间格式 互相转换 秒数 最佳实践
  6. linux tricks 之 typeof用法.
  7. FlyCapture2 VS2010 Configuration
  8. DOM 中 Property 和 Attribute 的区别
  9. 2013 Multi-University Training Contest 1 Cards
  10. 遍历map的四方方法
  11. java基础之导入(药师点评)
  12. 图片拉伸(有保护区域) resizableImageWithCapInsets
  13. UML它 时序图
  14. Servlet支持上传多张图片
  15. 如何使用.bas文件
  16. BootKit病毒——“异鬼Ⅱ”的前世今生
  17. 201521123002 《Java程序设计》第4周学习总结
  18. Java进阶篇设计模式之十二 ---- 备忘录模式和状态模式
  19. redis介绍、安装、redis持久化、redis数据类型
  20. 1. cs231n k近邻和线性分类器 Image Classification

热门文章

  1. 由n个已按1,2,3,.....,n编号的人围成一圈,从编号为1的人开始按1,2,3顺序循环报数,凡报到3者出圈,最后留一个人,问其编号是多少
  2. pyinstaller 打包工具的使用方法
  3. swiper保存
  4. 查看是否安装jdk及路径
  5. 19.顺时针打印矩阵(python)
  6. py从入门到实践 第四章
  7. #419 Div2 Problem C Karen and Game (贪心 &amp;&amp; 暴力)
  8. Devexpress MVC Gridview
  9. luogu P1083 借教室 x
  10. 苹果CMSv10对接微信公众号教程