Asp.net Vnext 中间件实现基本验证
概述
本文已经同步到《Asp.net Vnext 系列教程 》中]
vnext 没有 web.config 可以配置基本验证,本文使用中间件实现基本验证
实现
通过Startup(启动类)Configure 方法加入中间件
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerfactory)
{ app.UseMyMiddleware();
//通过扩展注册中间件
public static class BasicAuthenticationExtensions
{
public static IApplicationBuilder UseMyMiddleware(this IApplicationBuilder app)
{
return app.UseMiddleware<BasicAuthentication>();
}
}
//中间件类
public class BasicAuthentication
{
private readonly RequestDelegate next; public BasicAuthentication(RequestDelegate next)
{
this.next = next;
} public async Task Invoke(HttpContext context,
IAuthenticationService authenticationService)
{
try
{
var parser = new BasicAuthenticationParser(context);
var username = parser.GetUsername();
var password = parser.GetPassword(); await authenticationService.AuthenticateAsync(username, password);
await next(context);
}
catch (Exception e)
{
context.Response.StatusCode = ;
context.Response.Headers.Add("WWW-Authenticate",
new[] { "Basic" });
}
}
}
//解析上下文获取 密码 用户名
internal class BasicAuthenticationParser
{
private readonly string credentials; public BasicAuthenticationParser(HttpContext context)
{
credentials = GetCredentials(context);
} public string GetUsername()
{
return GetValue(credentials, );
} public string GetPassword()
{
return GetValue(credentials, );
} private static string GetValue(string credentials, int index)
{
if (string.IsNullOrWhiteSpace(credentials))
return null; var parts = credentials.Split(':');
return parts.Length == ? parts[index] : null;
} private static string GetCredentials(HttpContext context)
{
try
{
string[] authHeader;
if (context.Request.Headers.TryGetValue("Authorization", out authHeader) &&
authHeader.Any() &&
authHeader[].StartsWith("Basic "))
{
var value = Convert.FromBase64String(authHeader[].Split(' ')[]);
return Encoding.UTF8.GetString(value);
} return null;
}
catch
{
return null;
}
}
}
//认证服务接口
public interface IAuthenticationService
{
Task AuthenticateAsync(string username, string password);
}
public class SimpleAuthenticationService : IAuthenticationService
{
public Task AuthenticateAsync(string username, string password)
{
//判读密码
if ("".Equals(username, StringComparison.OrdinalIgnoreCase) &&
"".Equals(password))
{
return Task.FromResult();
}
throw new Exception();
}
}
运行效果

Asp.net Vnext 中间件实现基本验证的更多相关文章
- ASP.NET Core 1.1 静态文件、路由、自定义中间件、身份验证简介
概述 之前写过一篇关于<ASP.NET Core 1.0 静态文件.路由.自定义中间件.身份验证简介>的文章,主要介绍了ASP.NET Core中StaticFile.Middleware ...
- ASP.NET Core 1.0 静态文件、路由、自定义中间件、身份验证简介
概述 ASP.NET Core 1.0是ASP.NET的一个重要的重新设计. 例如,在ASP.NET Core中,使用Middleware编写请求管道. ASP.NET Core中间件对HttpCon ...
- Asp.net Vnext Filters
ASP.NET MVC 提供Filters(筛选器)之前或之后调用操作方法执行筛选逻辑,和AOP面向切面编程一样. 本文已经同步到<Asp.net Vnext 系列教程 >中] 本章主要介 ...
- Mac OS X上编写 ASP.NET vNext(一)KRE环境搭建
最新的asp.net vnext已经可以支持在mac上运行了,当然用的是mono.相比linux来说,mac的安装略显繁琐.对于大部分用Windows开发asp.net的程序员来说,初次配置还是很费时 ...
- ASP.NET Core 中间件Diagnostics使用
ASP.NET Core 中间件(Middleware)Diagnostics使用.对于中间件的介绍可以查看之前的文章ASP.NET Core 开发-中间件(Middleware). Diagnost ...
- ASP.NET vNext 概述
兼容Mono的下一代云环境Web开发框架ASP.NET vNext 我们知道了ASP.NET vNext是一个全新的框架,是一个与时俱进的框架.这篇文章将深入讨论在整体架构更多的细节,文档参照 ASP ...
- [转]ASP.NET Core 中间件详解及项目实战
本文转自:http://www.cnblogs.com/savorboard/p/5586229.html 前言 在上篇文章主要介绍了DotNetCore项目状况,本篇文章是我们在开发自己的项目中实际 ...
- 振奋人心啊!!!!下一代.NET——ASP.NET vNext
这两天看到的.NET的新闻都好振奋人心啊!微软北美技术大会带来了好多好消息! 看到一篇博客园的文章,感觉太棒了.摘录下来.原文链接:http://news.cnblogs.com/n/208133/ ...
- ASP.NET Core中间件实现分布式 Session
1. ASP.NET Core中间件详解 1.1. 中间件原理 1.1.1. 什么是中间件 1.1.2. 中间件执行过程 1.1.3. 中间件的配置 1.2. 依赖注入中间件 1.3. Cookies ...
随机推荐
- bzoj千题计划172:bzoj1192: [HNOI2006]鬼谷子的钱袋
http://www.lydsy.com/JudgeOnline/problem.php?id=1192 1,2,4,8,…… n-2^k 可以表示n以内的任意数 若n-2^k 和 之前的数相等,一个 ...
- 浅谈splay的双旋
昨晚终于明白了splay双旋中的一些细节,今日整理如下 注:题目用的2002HNOI营业额统计,测试结果均来及codevs 网站的评测结果 http://codevs.cn/problem/1296/ ...
- poj 1182/codevs 1074 食物链
http://poj.org/problem?id=1182 http://codevs.cn/problem/1074/ 动物王国中有三类动物 A,B,C,这三类动物的食物链构成了有趣的环形.A吃B ...
- java8 write file 写文件
1.用BufferedWriter写入文件 //Get the file reference Path path = Paths.get("c:/output.txt"); //U ...
- Web Api问题汇总
在公网上布署Web Api的时候,不能调用,返回404 在web.config中 Adding the following to the web.config file worked for me: ...
- 【leetcode 简单】 第七十二题 各位相加
给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. 示例: 输入: 38 输出: 2 解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2. 由于 2 是一位数,所 ...
- 对某道ctf的一点点记录
题目:http://ctf5.shiyanbar.com/web/pcat/index.php 是一道注入的题,主要利用了offset 和 group by with rollup的知识 1.offs ...
- tomcat集群及session共享
一般来说,java web app主要用作两个领域: 1.api.api一般是无状态的,所以无需考虑session共享的问题 2.传统web应用和网站,如crm,oa,erp,b2c,bbs等.尤其b ...
- TCP三次握手与四次挥手过程
TCP连接的建立(三次握手) 首先,客户端与服务器均处于未连接状态,并且是客户端主动向服务器请求建立连接: 客户端将报文段中的SYN=1(同步位),并选择一个seq=x,(即该请求报文的序号为x) ...
- java数字转字符串前面自动补0或者其他数字
/** * Java里数字转字符串前面自动补0的实现. * * @author xiaomo * */ public class TestStringFormat { public ...