webapi添加basic认证
BasicAbstractAuthorize:抽象类,子类中校验用户名密码,并创建Principal
BasicAuthorize:实现类
//base.OnAuthorization(),此方法内部,调用IsAuthorized()判断是否授权,如果未授权调用HandleUnauthorizedRequest()方法
//base.IsAuthorized(),判断Principal、Identity是否为空,为空则未授权
//base.HandleUnauthorizedRequest(),此方法内部创建Response,状态码401;
//
public abstract class BasicAbstractAuthorize : AuthorizeAttribute
{
public override void OnAuthorization(HttpActionContext actionContext)
{ var authenticationHeader = actionContext.Request.Headers.Authorization; if (actionContext.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>(true).Count >
|| actionContext.ControllerContext.ControllerDescriptor.GetCustomAttributes<AllowAnonymousAttribute>(true).Count > )
{//如果有AllowAnonymous特性,就不检查
base.OnAuthorization(actionContext);
}
else
{
if (authenticationHeader != null && authenticationHeader.Scheme == "Basic" && !string.IsNullOrEmpty(authenticationHeader.Parameter))
{
var userNameAndPassword = this.GetUserNameAndPassword(authenticationHeader.Parameter);
actionContext.RequestContext.Principal = this.Authenticate(userNameAndPassword.Item1, userNameAndPassword.Item2, actionContext);
}
if (actionContext.RequestContext.Principal == null)
{
base.HandleUnauthorizedRequest(actionContext);
}
} }
/// <summary>
/// 校验用户名、密码
/// </summary>
/// <returns></returns>
public abstract IPrincipal Authenticate(string userName, string password, HttpActionContext actionContext);
/// <summary>
/// 获取用户名、密码
/// </summary>
/// <param name="authenticationParameter"></param>
/// <returns></returns>
private Tuple<string, string> GetUserNameAndPassword(string authenticationParameter)
{
if (!string.IsNullOrWhiteSpace(authenticationParameter))
{
var data = Encoding.ASCII.GetString(Convert.FromBase64String(authenticationParameter)).Split(':');
return new Tuple<string, string>(data[], data[]);
}
return null;
}
} public class BasicAuthorize : BasicAbstractAuthorize
{
public override IPrincipal Authenticate(string userName, string password, HttpActionContext actionContext)
{
//校验用户名、密码
if (userName == "zhangsan" && password == "")
{
ClaimsIdentity identity = new ClaimsIdentity(new List<Claim> {
new Claim("UserName",userName)
});
ClaimsPrincipal principal = new ClaimsPrincipal(identity);
return principal;
}
return null; }
}
添加Filter
public static void Register(HttpConfiguration config)
{
// Web API 配置和服务
RegisterFilters(config.Filters);
}
public static void RegisterFilters(HttpFilterCollection filters)
{
filters.Add(new BasicAuthorize());
}
webapi添加basic认证的更多相关文章
- [Nginx]子目录反向代理kibana并添加basic认证
背景 服务器ip:192.168.1.2 安装软件 nginx kibana(默认端口5601) 实现方案:访问http://192.168.1.2/kibana 即可访问到kibana后端,同时需要 ...
- ajax 上传文件给webapi(带basic认证)
$('#btnupload').on('click', function () { var fd = new FormData(); ]; fd.append("report_id" ...
- NetCore+Dapper WebApi架构搭建(六):添加JWT认证
WebApi必须保证安全,现在来添加JWT认证 1.打开appsettings.json添加JWT认证的配置信息 2.在项目根目录下新建一个Models文件夹,添加一个JwtSettings.cs的实 ...
- Basic认证时添加请求头
http Basic认证 http协议定义的一种认证方式,将客户端id和客户端密码按照"客户端ID:客户端密码"的格式拼接,并用base64编 码,放在header中请求服务端, ...
- angularjs+webapi2 跨域Basic 认证授权(二)
在上一篇中大概演示了 整个认证授权的过程.今天在这篇博客中将结合上一篇的例子继续在跨域的情况 我们用ionic 写一个简单的页面 值得注意的是 在ionic.bundle.js 里面集成了angula ...
- iOS进行Basic认证与NTLM认证
一.iOS进行Basic认证 只需要在NSMutableURLRequest的Header中添加认证所需的Username和password. NSMutableURLRequest *webReq ...
- Burpsuite之Http Basic认证爆破
有的时候经常遇到401.今天正好朋友问怎么爆破,也顺便记录一下 怕忘记了 referer:http://www.2cto.com/Article/201303/194449.html 看到Burpsu ...
- angularjs+webapi2 跨域Basic 认证授权(一)
如今的app,利用各种前端框架结合html5的混合开发模式已然盛极一时.其中ionic+angularjs更是如日中天.这种模式利用angularjs $http 请求数据api 以达到前后端分离深得 ...
- WebApi使用JWT认证(一)
这是第一部:先实现NetFramework上的WebApi使用JWT认证 1.VS新建一个WebApi项目 2.项目右键----管理Nuget程序包----找到JWT,然后安装 3.Model文件夹下 ...
随机推荐
- 怎样把ndarray转换为PyQt中的QPixmap
找不到文档,只在网上找到一些语焉不详,执行错误的代码,鼓捣了一个晚上,研(luan)究(gao)成功 def img2pixmap(self, image): Y, X = image.shape[: ...
- centos7下安装php-memcached扩展
-> https://blog.csdn.net/sinat_35861664/article/details/72831556 安装扩展进行编译时如果报错,则将 ./configure --w ...
- Windows 防火墙无法更改某些设置错误代码 0x80070422
Windows 防火墙无法更改某些设置错误代码 0x80070422 解决方法: 1.cmd ->services.msc 按下回车键打开服务 : 2.在服务界面双击打开[Windows F ...
- Azure Automation (7) 执行Azure SQL Job
<Windows Azure Platform 系列文章目录> 之前Automation介绍的内容,是在ASM模式下自动化开关机. 本章将介绍如何在Automation中,设置开关机脚本, ...
- python免安装版(绿色版)制作
一.实验环境 1.Windows7x64_SP1 二.需求背景 个人编写了一个软件安装器,用于一键安装开发软件及工具,该工具基于python + pywinauto. 但问题来了,新电脑上未安装pyt ...
- LeetCode 20:有效的括号 Valid Parentheses
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. Given a string containing just the characters '(', ' ...
- 奥展项目笔记05--域名、端口、Nginx相关知识笔记
1.我国的电信运营商是默认封闭80端口的. 中国电信屏蔽ADSL用户80端口 只是做应用服务器的话你可以使用别的端口. 80端口电脑上同时有各种各样的程序在运行,他们都需要借助网络来进行通信.例如,你 ...
- 异步IO/协程/数据库/队列/缓存(转)
原文:Python之路,Day9 - 异步IO\数据库\队列\缓存 作者:金角大王Alex add by zhj: 文章很长 引子 到目前为止,我们已经学了网络并发编程的2个套路, 多进程,多线程,这 ...
- Grafana的Docker部署方式
docker run -d -p : --name=grafana544 -v D:/grafana/grafana-/data:/var/lib/grafana -v D:/grafana/graf ...
- C#之初识异步
什么是异步 举个例子:小明的妈妈让小明烧一壶水,水烧开后要倒进水壶里,同时还需要把家里打扫一下. 小明的操作流程一:烧水---->等待至水烧开----->水倒进水壶里--------> ...