在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,表示 ...
随机推荐
- Cocos2dx 小技巧(十六)再谈visit(getDescription)
之前两篇都是介绍与Value相关的,这篇我继续这个话题吧,正好凑个"Value三板斧系列...".在非常久非常久曾经.我用写过一篇博客,关于怎样查看CCArray与CCDictio ...
- debian 下的vi 上下左右键问题
小白一只,查了一下vi的版本信息 发现好像是vim 于是把~/.vimrc 变量设置了一下就好了。 将set compatible 设置成set nocompatible . 这是因为系统会默认vim ...
- VS2010下配置Opencv2.4.3 .
VS2008下OpenCV的配置过程在OpenCV论坛上写的很详细,具体过程可以见如下链接http://www.opencv.org.cn/index.php/VC_2008_Express%E4%B ...
- 8大排序算法图文讲解 分类: B10_计算机基础 2014-08-18 15:36 243人阅读 评论(0) 收藏
排序算法可以分为内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存. 常见的内部排序算法有:插入排序.希尔排序. ...
- [Angular] Enable router tracing
To enable router tracing is really simple: RouterModule.forRoot(ROUTES, { enableTracing: true }) Whe ...
- QT学习记录之理解信号槽机制
作者:朱金灿 来源:http://blog.csdn.net/clever101 QT的事件机制采用的信号槽机制.所谓信号槽机制,简而言之就是将信号和信号处理函数绑定在一起,比如一个按钮被单击是一个信 ...
- MySQL 监控-innotop
innotop 编写者Balon Schwartz,<高性能MySQL>的作者之一. innotop的作用为实时地展示服务器正在发生的事情,监控innodb,监控多个MySQL实例,是一款 ...
- jQuery 淡入淡出
演示 jQuery fadeIn() 方法: <!DOCTYPE html> <html> <head> <meta charset="utf-8& ...
- VMWare提供三种工作模式桥接(bridge)、NAT(网络地址转换)和host-only(主机模式)
1.桥接模式 在桥接模式下,VMWare虚拟出来的操作系统就像是局域网中的一台独立的主机(主机和虚拟机处于对等地位),它可以访问网内任何一台机器.在桥接模式下,我们往往需要为虚拟主机配置IP地址.子网 ...
- 【32.89%】【codeforces 719A】Vitya in the Countryside
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...