一、模型验证CoreWebApi 管道方式(非过滤器处理)2(IApplicationBuilder扩展方法的另一种写法)
一、 自定义中间件类的方式用一个单独类文件进行验证处理
Configure下添加配置
//app.AddAuthorize();
AddAuthorize因为参数(this IApplicationBuilder applicationBuilder)来自IApplicationBuilder扩展方法
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks; namespace test.Controllers
{
public static class AuthorizeExtensio
{
public static void AddAuthorize(this IApplicationBuilder applicationBuilder)
{
applicationBuilder.Use(async (currentContext, nextContext) =>
{
if (currentContext.Request.Headers.ContainsKey("Token"))
{
var authorize = currentContext.Request.Headers["Authorization"].ToString();
if (authorize.Contains("Basic"))//如果是Basic 身份认证
{
var info = authorize.Replace("Basic ", string.Empty);
var bytes = Convert.FromBase64String(info);//反解析Basic 64
var contents = Encoding.Default.GetString(bytes);
var dd = contents.Split(":").ToArray();
var userName = dd[];//用户名
var userPwd = dd[];//密码
if (userName == "GeBiLaoWang" && userPwd == "")
{
await currentContext.Response.WriteAsync("验证通过").ConfigureAwait(true); //Response.WriteAsync() 需要引用 Microsoft.AspNetCore.Http;
await nextContext?.Invoke();
return;
}
}
}
currentContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
await currentContext.Response.WriteAsync("See tou tomorrow!").ConfigureAwait(true);
});
}
}
}
一、模型验证CoreWebApi 管道方式(非过滤器处理)2(IApplicationBuilder扩展方法的另一种写法)的更多相关文章
- 一、模型验证CoreWebApi 管道方式(非过滤器处理)
一.新建.Net Core的MVC项目添加WebApi控制器的方式 using System; using System.Collections.Generic; using System.Linq; ...
- ASP.NET Web API模型验证以及异常处理方式
ASP.NET Web API的模型验证与ASP.NET MVC一样,都使用System.ComponentModel.DataAnnotations. 具体来说,比如有:[Required(Erro ...
- 17-EasyNetQ:非泛型的发布&订阅扩展方法
自从EasyNetQ第一个版本开始,它就可以发布/订阅特定类型的消息. bus.Subscribe<MyMessage>("subscriptionId", x =&g ...
- EasyNetQ使用(九)【非泛型的发布&订阅扩展方法,发生错误的情况 】
自从EasyNetQ第一个版本开始,它就可以发布/订阅特定类型的消息. bus.Subscribe<MyMessage>("subscriptionId", x =&g ...
- 一、WebApi模型验证实践项目使用
一.启语 前面我们说到,模型验证的原理(包含1.项目创建,2.模型创建,3.走通测试模型验证,4.在过滤器中处理返回json格式(非控制器内))-完全是新手理解使用的,新番理解 通常情况下,对于那些经 ...
- ASP.NET Core 2.2 WebApi 系列【八】统一返回格式(返回值、模型验证、异常)
现阶段,基本上都是前后端分离项目,这样一来,就需要前后端配合,没有统一返回格式,那么对接起来会很麻烦,浪费时间.我们需要把所有接口及异常错误信息都返回一定的Json格式,有利于前端处理,从而提高了工作 ...
- 一、WebApi模型验证
一.新建项目 选择空的项目webapi 查看启动端口 创建控制器 添加方法 public class VerifController : ApiController { public IHttpAct ...
- Web API中的模型验证
一.模型验证的作用 在ASP.NET Web API中,我们可以使用 System.ComponentModel.DataAnnotations 命名空间中的属性为模型上的属性设置验证规则. 一个模型 ...
- Web API中的模型验证Model Validation
数据注释 在ASP.NET Web API中,您可以使用System.ComponentModel.DataAnnotations命名空间中的属性为模型上的属性设置验证规则. using System ...
随机推荐
- du和df目录大小不一致
最近遇到个问题 df查看基本没有空间了 但是查找大文件 也基本没有 ,原来是之前的大文件删除了 没有生效导致的 用如下命令,查找到相关进程ID 然后kill就行了 lsof | grep delete ...
- C++多重继承实践
实践代码如下: #include <iostream> using namespace std; class Animal { private: int hash; public: Ani ...
- MSTest/NUnit 单元测试 代码覆盖率试用 OpenCover 和ReportGenerator
VS自带是单元测试代码覆盖率(VS自带这个是最佳选择)需要企业版才有.很蛋疼...... 1.下载安装OpenCover 和ReportGenerator. 关于这2个是干啥的百度下.简单说就是可以分 ...
- nodejs之express中间件cookie-parser使用
知识点: * .domain的使用,.aaa.com的域名都共享这个cookie信息 * res.cookie(,domain:'.aaa.com'}); * .获取所有cookie,设置cookie ...
- Linux下搭建Git服务器
1.安装Git 见 Jenkins持续集成环境部署 第四节 2.创建Git用户和用户组 groupadd git useradd git -g git 3.创建证书切换到git用户创建证书 su gi ...
- 微信小程序---绘图
传送门: https://mp.weixin.qq.com/debug/wxadoc/dev/api/canvas/reference.html
- ios app真机测试到上架App Store详细教程-必看
详细操作地址 http://www.applicationloader.net/blog/zh/88.html 苹果iOS APP真机调试测试和上架App Store视频教程 优酷 http://v. ...
- #Java学习之路——基础阶段二(第十三篇)
我的学习阶段是跟着CZBK黑马的双源课程,学习目标以及博客是为了审查自己的学习情况,毕竟看一遍,敲一遍,和自己归纳总结一遍有着很大的区别,在此期间我会参杂Java疯狂讲义(第四版)里面的内容. 前言: ...
- HCL试验3
PC端配置:配置ip地址 配置网关 交换机(左)配置:①创建VLAN system-view vlan 10 vlan 20 ②配置PC端接口 interface gi 1/0/1 port link ...
- 【PyTorch】计算局部相似矩阵
计算局部相似矩阵 代码文档:https://github.com/lartpang/mypython/blob/master/2019-09-25%E8%AE%A1%E7%AE%97%E5%B1%80 ...