任务36:应用Jwtbearer Authentication
任务36:应用Jwtbearer Authentication
D:\MyDemos\jesse
新建项目:dotnet new webapi --name JwtAuthSample

VS2017运行项目:http://localhost:5429/api/values

using Microsoft.AspNetCore.Authorization;

因为我本机安装的是asp.net core 2.2的项目,所以用VScode去dotnet run的方式运行不行,默认创建的api项目是https的
于是我改用VS2017去打开创建的项目:项目的属性 取消SSL

然后用VS运行项目:
加上authorize以后返回 500:

引入

Startup.cs内加上这个middleware

新建文件夹Models并在里面创建类:
JwtSettings.cs


namespace JwtAuthSample{
public class JwtSettings
{
//token是谁颁发的
public string Issuer{get;set;}
//token可以给哪些客户端使用
public string Audience{get;set;}
//加密的密钥
public string SecretKey{get;set;}
}
}
需要给jwt设置一些配置信息:

"JwtSettings": {
"Audience": "http://localhost:5000",
"Issuer": "http://localhost:5000",
"SecretKey": "Hello-Key"
}

services.Configure<JwtSettings>(Configuration);
var jwtSettings=new JwtSettings();
Configuration.Bind("JwtSettings",jwtSettings);
上面是认证middleware的配置
下面是认证jwt middleware的配置
在这个命名空间内:using Microsoft.IdentityModel.Tokens; 有对称加密的一种方式:SymmetricSecurityKey
IssuerSigningKey=new SymmetricSecurityKey(这里的参数要用utf-8),所以要引入命名空间system.Text;

这样就完成了jwt的配置
public void ConfigureServices(IServiceCollection services)
{
services.Configure<JwtSettings>(Configuration.GetSection("JwtSettings")); var jwtSettings = new JwtSettings();
Configuration.Bind("JwtSettings", jwtSettings);
services.AddAuthentication(Options =>
{
Options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
Options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(o =>
{
o.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
{
ValidIssuer = jwtSettings.Issuer,
ValidAudience = jwtSettings.Audience,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings.SecretKey))
};
}); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

任务36:应用Jwtbearer Authentication的更多相关文章
- C# 实现Jwtbearer Authentication
Jwtbearer Authentication 什么是JWT JWT(JSON Web Token), 顾名思义就是在Web上以JSON格式传输的Token(RFC 7519). 该Token被设计 ...
- 【ASP.NET Core快速入门】(十一)应用Jwtbearer Authentication、生成jwt token
准备工作 用VSCode新建webapi项目JwtAuthSample,并打开所在文件夹项目 dotnet new webapi --name JwtAuthSample 编辑JwtAuthSampl ...
- 菜鸟入门【ASP.NET Core】11:应用Jwtbearer Authentication、生成jwt token
准备工作 用VSCode新建webapi项目JwtAuthSample,并打开所在文件夹项目 dotnet new webapi --name JwtAuthSample 编辑JwtAuthSampl ...
- 36-应用Jwtbearer Authentication
新建.net core webapi项目 E:\coding\netcore>dotnet new webapi --name JwtAuthSample 创建需要用到的实体对象类 namesp ...
- Nancy基于JwtBearer认证的使用与实现
前言 最近在看JSON Web Token(Jwt)相关的东西,但是发现在Nancy中直接使用Jwt的组件比较缺乏,所以就在空闲时间写了一个. 这个组件是开源的,不过目前只支持.NET Core,后续 ...
- [转]C# 实现Jwt bearer Authentication
本文转自:https://www.cnblogs.com/aishangyipiyema/p/9262642.html 什么是JWT JWT(JSON Web Token), 顾名思义就是在Web上以 ...
- NET CORE Learning
ASP.NET Core 基础教程https://www.cnblogs.com/lonelyxmas/tag/ASP.NET%20Core%20%E5%9F%BA%E7%A1%80%E6%95%99 ...
- ASP.NET Core快速入门_学习笔记汇总
第2章 配置管理 任务12:Bind读取配置到C#实例 任务13:在Core Mvc中使用Options 任务14:配置的热更新 任务15:配置框架设计浅析 第3章 依赖注入 任务16:介绍- 任务1 ...
- ASP.NET Core快速入门(第5章:认证与授权)--学习笔记
课程链接:http://video.jessetalk.cn/course/explore 良心课程,大家一起来学习哈! 任务31:课时介绍 1.Cookie-based认证与授权 2.Cookie- ...
随机推荐
- C# UserControl 判断是否是设计模式中
In Windows Forms application, we can use Control.IsInDesignMode or LicenseManager.UsageMode == Licen ...
- Codeforces Round #266 (Div. 2) C. Number of Ways
You've got array a[1], a[2], ..., a[n], consisting of n integers. Count the number of ways to split ...
- 下篇:express、koa1、koa2的中间件原理
本作品采用知识共享署名 4.0 国际许可协议进行许可.转载联系作者并保留声明头部与原文链接https://luzeshu.com/blog/express-koa 本博客同步在http://www.c ...
- vim调用python格式化json数据
vim调用python格式化json数据 November 30, 2013GNU/Linuxpython3, Vimopenwares python有个标准模块叫json,用于编码/解码,序列化/按 ...
- 走入asp.net mvc不归路:[2]控制器概览
asp.net mvc中最灵活的地方就是控制器,这里可以验证数据,可以跳转视图,还可以访问数据库等等.所以,我们要先从这里说起. 1 控制器就是继承了Controller的类,一般来说,类名后面都会增 ...
- Intel Naming Strategy--1
http://en.wikipedia.org/wiki/Mobile_Internet_device Computer sizes Classes of computers Larger S ...
- 【iOS开源码】(1):CCLHTTPServer
我从没有见过比 CCLHTTPServer 更简单的 iOS HTTP server了.你甚至不用创建不论什么子类就可以使用它.对于大部分任务,这个小巧但强悍的server能够满足你的须要. 它简单到 ...
- Centos6.5 安装 Oracle11gR2(64位)
Centos6.5安装 Oracle11gR2(64位) 安装centos6.5 (我的是虚拟机环境) 1. 下载centos6.5的安装包,不解释. 例如以下图: 2. 下载oracle安装包, ...
- Seesion和Cookie详解2
转载来自: https://www.toutiao.com/a6693986851193094664/?tt_from=weixin&utm_campaign=client_share& ...
- thinkphp Class 'PDO' not found 错误
thinkphp Class 'PDO' not found 错误,原因mysql5.7.26缺少pdo驱动,需要安装php的pdo和pdo_mysql扩展 本文以centOS为例 1.进入PHP源码 ...