在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()
{
return View();
}
}
}
在加了Authorize特性之后,访问admin控制器的index方法,页面会显示异常,异常告诉我们,没有指定认证框架,它不知道如何来认证挑战

2.为程序注入Cookie认证框架
a.首先要引入两个程序集
//1 为了Cookie认证证明 引入的以下两个命名空间 认证命名空间 和 认证Cookie命名空间
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
认证和cookie框架命名空间的引入
b.在服务中注入认证
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
//2 CookieAuthenticationDefaults.AuthenticationScheme的值是一个字符串常量 "Cookies"
//AddAuthentication 方法要求传入的是一个字符串 我传入的字符串是Cookies 就是说 使用Cookie 认证
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
注入cookie认证
此时再访问admin控制器的index方法,已经不报异常了,而是直接跳转到account/login
http://localhost:60208/Account/Login?ReturnUrl=%2Fadmin
3.创建Account控制器 在此不挑战,直接授予权限
//引入认证相关的命名空间
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
//引入安全相关的命名空间
using System.Security.Claims; namespace CookieBasedAuth.Controllers
{
public class AccountController : Controller
{
public IActionResult Login()
{
var claims = new List<Claim>{
new Claim(ClaimTypes.Name,"应龙"),
new Claim(ClaimTypes.Role,"国王")
};
//千万要注意下面的new ClaimsIdentity的构造函数,第二个参数要指明认证框架,要和服务中注入的框架一致 都是cookie
var claimIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimIdentity));
return Ok();
}
}
}
授权
即便授权了,我们请求admin控制器的index方法,会跳转到account控制器的login方法。按想象的此时授权了,就应该可以访问admin控制器的index了,然而我们会发现没有用,也就是说没有授权,还是会跳转到account/login
4.将cookie验证加入到中间件
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
} app.UseStaticFiles();
app.UseCookiePolicy(); //将认证中间件弄进来 使得我们的请求会进入认证管道
app.UseAuthentication(); app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
将认证授权加入到中间件
完成 !
在asp.net core中使用cookie认证的更多相关文章
- ASP.NET CORE中使用Cookie身份认证
大家在使用ASP.NET的时候一定都用过FormsAuthentication做登录用户的身份认证,FormsAuthentication的核心就是Cookie,ASP.NET会将用户名存储在Cook ...
- 在ASP.NET Core 中使用Cookie中间件
在ASP.NET Core 中使用Cookie中间件 ASP.NET Core 提供了Cookie中间件来序列化用户主题到一个加密的Cookie中并且在后来的请求中校验这个Cookie,再现用户并且分 ...
- 在ASP.NET Core 中使用Cookie中间件 (.net core 1.x适用)
在ASP.NET Core 中使用Cookie中间件 ASP.NET Core 提供了Cookie中间件来序列化用户主题到一个加密的Cookie中并且在后来的请求中校验这个Cookie,再现用户并且分 ...
- ASP.NET Core 中的那些认证中间件及一些重要知识点
前言 在读这篇文章之间,建议先看一下我的 ASP.NET Core 之 Identity 入门系列(一,二,三)奠定一下基础. 有关于 Authentication 的知识太广,所以本篇介绍几个在 A ...
- [转]ASP.NET Core 中的那些认证中间件及一些重要知识点
本文转自:http://www.qingruanit.net/c_all/article_6645.html 在读这篇文章之间,建议先看一下我的 ASP.NET Core 之 Identity 入门系 ...
- 如何简单的在 ASP.NET Core 中集成 JWT 认证?
前情提要:ASP.NET Core 使用 JWT 搭建分布式无状态身份验证系统 文章超长预警(1万字以上),不想看全部实现过程的同学可以直接跳转到末尾查看成果或者一键安装相关的 nuget 包 自上一 ...
- ASP.NET Core 中jwt授权认证的流程原理
目录 1,快速实现授权验证 1.1 添加 JWT 服务配置 1.2 颁发 Token 1.3 添加 API访问 2,探究授权认证中间件 2.1 实现 Token 解析 2.2 实现校验认证 1,快速实 ...
- asp.net core中使用cookie身份验证
配置 在 Startup.ConfigureServices 方法中,创建具有 AddAuthentication 和 AddCookie 方法的身份验证中间件服务: services.AddAuth ...
- 在ASP.NET Core中添加的Cookie如果含有特殊字符,会被自动转义
我们知道在Cookie中有些字符是特殊字符,这些字符是不能出现在Cookie的键值中的. 比如"="是Cookie中用来分隔键和值的特殊字符,例如:Key01=Value01,表示 ...
随机推荐
- 【Codeforces Round #435 (Div. 2) B】Mahmoud and Ehab and the bipartiteness
[链接]h在这里写链接 [题意] 让你在一棵树上,加入尽可能多的边. 使得这棵树依然是一张二分图. [题解] 让每个节点的度数,都变成二分图的对方集合中的点的个数就好. [错的次数] 0 [反思] 在 ...
- eclipse 更换国内镜像
大家在用eclipse下载插件,或更新插件的时候,有木有觉得速度贼慢,蜗牛似的速度简直让习惯了4G时代的我们抓狂到底,废话不说,先给大家奉献解决办法 网上找到的国内镜像总结: 1.企业贡献: 搜狐开源 ...
- CTR深度学习
深度学习在 CTR 中应用 一. Wide&&Deep 模型 首先给出Wide && Deep [1] 网络结构: 本质上是线性模型(左边部分, Wide model) ...
- word多出空标题,样式是列出段落 - -显示时,选择不勾选“隐藏文字”
word多出空标题,样式是列出段落
- HTTP协议和HTTPS协议初探
概况 HTTP是hypertext transfer protocol(超文本传输协议)的简写.它是TCP/IP协议的一个应用层协议,用于定义WEB浏览器与WEBserver之间交换数据的过程. HT ...
- 小雷FansUnion:我有了第一个付费客户(第一个徒弟)
很高兴地告诉大家一个振奋人心的消息,我刚刚拥有了第一个付费客户. 第一个付费客户是山东青岛的一个上班族,有2年.Net经验,今年转Java开发.对我比较信任,在我的建议下,选择了"拜师学艺& ...
- Java获取文件路径的几种方法
第一种: File f = new File(this.getClass().getResource("/").getPath()); System.out.println(f); ...
- Android-通过Java代码来实现属性动画
Android-通过Java代码来实现属性动画 除了能够使用定义xml文件来设置动画之外.还能够使用java代码来进行控制动画. 示比例如以下: 布局文件: <RelativeLayout xm ...
- HDU 3215 The first place of 2^n (数论-水题)
The first place of 2^n Problem Description LMY and YY are mathematics and number theory lovers. They ...
- js进阶 10-3 jquery中为什么用document.ready方法
js进阶 10-3 jquery中为什么用document.ready方法 一.总结 一句话总结: 1.document.ready和window.onload的区别:用哪个好? document. ...