[ASP.NET Core] 最简单 Cookie 认证与授权的范例

上一篇提到?[ASP.NET MVC][Owin] 用最简单 Cookie 认证方式,

这次改换 ASP.NET Core 的?Microsoft.AspNetCore.Authentication.Cookies 练习最简单的 Cookie 认证与授权,

  1. 在 project.json 的?dependencies 加入

    "Microsoft.AspNetCore.Authentication.Cookies": "1.0.0"

    并记得每次加完要进行套件还原(Visual Studio 会自动套件还原),自己写命令则到项目目录下,用 Command Line 输入 dotnet restore 手动还原

  2. ?在?Startup 类的 Configuration?加上使用?CookieAuthentication?的方式
               app.UseCookieAuthentication(new CookieAuthenticationOptions()
                {
                    AuthenticationScheme = "MyCookieMiddlewareInstance",
                    LoginPath = new PathString("/Test/Index/"),
                    AutomaticAuthenticate = true,
                    AutomaticChallenge = true
                });

    CookieAuthenticationOption 的属性可参考官网说明,在最下方会提供参考连结

  3. 最简单的登入范例,很重要的是?ClaimsIdentity 构造函数第二个参数是?authenticationType 一定要填,不然验证没有作用?Http Response Hreaders 也不会有 Set-Cookie
            public async Task Login()
            {
                var claims = new List() {
                    new Claim(ClaimTypes.Name, "Herry"),
                    new Claim(ClaimTypes.Role, "Users")
                };
                var claimsIdentity = new ClaimsIdentity(claims, "myTest");
                var principal = new ClaimsPrincipal(claimsIdentity);
                await HttpContext.Authentication.SignInAsync("MyCookieMiddlewareInstance", principal);
    
                return Ok();
            }
  4. 想授权哪个 Controller 或 Action 的方式一样没变,例如
            [Authorize]
            public IActionResult Index()
            {
                return View();
            }

参考文章

Using Cookie Middleware without ASP.NET Core Identity

原文:大专栏  [ASP.NET Core] 最简单 Cookie 认证与授权的范例

原文地址:https://www.cnblogs.com/chinatrump/p/11458443.html

时间: 2024-07-30 01:58:44

[ASP.NET Core] 最简单 Cookie 认证与授权的范例的相关文章

asp.net core 2.0的认证和授权

在asp.net core中,微软提供了基于认证(Authentication)和授权(Authorization)的方式,来实现权限管理的,本篇博文,介绍基于固定角色的权限管理和自定义角色权限管理,本文内容,更适合传统行业的BS应用,而非互联网应用. 在asp.net core中,我们认证(Authentication)通常是在Login的Post Action中进行用户名或密码来验证用户是否正确,如果通过验证,即该用户就会获得一个或几个特定的角色,通过ClaimTypes.Role来存储角色

在asp.net core中使用cookie认证

以admin控制器为要认证的控制器举例 1.对控制器设置权限特性 //a 认证命名空间 using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace CookieBasedAuth.Controllers { //b 认证特性 [Authorize] public class AdminController : Controller { public IActionResult Index(

ASP.Net Core 2.1+ Cookie 登录授权验证【简单Cookie验证】

原文:ASP.Net Core 2.1+ Cookie 登录授权验证[简单Cookie验证] 介绍 本文章发布于博客园:https://www.cnblogs.com/fallstar/p/11310749.html 作者:fallstar 本文章适用于:ASP.NET Core 2.1 + 今天想给一个asp.net core 的项目加上权限验证,于是研究了一下怎么加, 折腾了好一阵,发现原来Filter的方式被放弃了,现在使用Policy 和 Scheme 的方式来校验了... 然后开始猛查

ASP.NET与ASP.NET Core用户验证Cookie并存解决方案

在你将现有的用户登录(Sign In)站点从ASP.NET迁移至ASP.NET Core时,你将面临这样一个问题——如何让ASP.NET与ASP.NET Core用户验证Cookie并存,让ASP.NET应用与ASP.NET Core应用分别使用各自的Cookie?因为ASP.NET用的是FormsAuthentication,ASP.NET Core用的是claims-based authentication,而且它们的加密算法不一样. 我们采取的解决方法是在ASP.NET Core中登录成功

ASP.NET Core集成现有系统认证

我们现在大多数转向ASP.NET Core来使用开发的团队,应该都不是从0开始搭建系统,而是老的业务系统已经在运行,ASP.NET Core用来开发新模块.那么解决用户认证的问题,成为我们的第一个拦路虎.本文将给大家简单阐述一下认证与授权的基本概念,以及基于ASP.NET Core 中间件实现的认证和改造JwtBearer 认证中间件来实现的认证达到与老系统(主要是token-based认证)的集成. 目录 认证与授权 什么是认证 何谓授权 用Middleware拦截 定制JWT Bearer 

在ASP.NET Core 中使用Cookie中间件

http://ASP.NET Core 提供了Cookie中间件来序列化用户主题到一个加密的Cookie中并且在后来的请求中校验这个Cookie,再现用户并且分配到HttpContext对象的User属性中.如果你想提供自己的登录方式和用户数据你可以使用Cookie中间件来实现独立的功能. 添加和配置 第一步是增加Cookie中间件到你的应用中.首先使用nuget增加Microsoft.AspNetCore.Authentication.Cookies 程序包.然后添加下面的几行代码到Start

[转]ASP.NET Core 中的那些认证中间件及一些重要知识点

本文转自:http://www.qingruanit.net/c_all/article_6645.html 在读这篇文章之间,建议先看一下我的 ASP.NET Core 之 Identity 入门系列(一,二,三)奠定一下基础. 有关于 Authentication 的知识太广,所以本篇介绍几个在 ASP.NET Core 认证中会使用到的中间件,还有Authentication的一些零碎知识点,这些知识点对于 ASP.NET 认证体系的理解至关重要. 在 Github 中 ASP.NET C

ASP.NET Core CORS 简单使用

CORS 全称"跨域资源共享"(Cross-origin resource sharing). 跨域就是不同域之间进行数据访问,比如 a.sample.com 访问 b.sample.com 中的数据,我们如果不做任何处理的话,就会出现下面的错误: XMLHttpRequest cannot load b.sample.com. No 'Access-Control-Allow-Origin' header is present on the requested resource. O

Asp.net core 中间件简单应用

Asp.net core中间件 ,处理http请求和响应的中间组件,对比起asp.net ,asp.net core 管道机制,可以说是帅气十足,简单直接.下面是通过中间件对一个请求的url 指定路由 新建webapi 项目 Startup类中Configure方法中添加处理中间件代码如下 public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app