以Attribute加上Header验证
建立新FilterAttribute继承AuthorizationFilterAttribute,覆写OnAuthorization拦截传入的HttpActionContext内容判断是否有传入指定的资料
public override void OnAuthorization(HttpActionContext filterContext)
{
var identity = FetchAuthHeader(filterContext); //取得資料內容
if (identity == null)
{
ChallengeAuthRequest(filterContext); //回傳錯誤訊息
return;
}
var genericPrincipal = new GenericPrincipal(identity, null);
//針對目前連線的使用者做授權
Thread.CurrentPrincipal = genericPrincipal;
if (!OnAuthorizeUser(identity.Name, identity.Password, filterContext)) //驗證
{
ChallengeAuthRequest(filterContext);
return;
}
base.OnAuthorization(filterContext);
}
解析HttpActionContext内容取得指定的资料
protected virtual BasicAuthenticationIdentity FetchAuthHeader(HttpActionContext filterContext)
{
string customer = "";
string pwd = "";
IEnumerable<string> authRequest = filterContext.Request.Headers.GetValues("指定的資料名稱");
IEnumerable<string> authRequest2 = filterContext.Request.Headers.GetValues("指定的資料名稱2");
try
{
customer = authRequest.FirstOrDefault();
pwd = authRequest2.FirstOrDefault();
}
catch { }
return new BasicAuthenticationIdentity(customer, pwd);
}
验证解析出来的资料是否符合需求
protected override bool OnAuthorizeUser(string username, string password, HttpActionContext actionContext)
{
if (username == "驗證資料" && password == "驗證碼")
return true;
return false;
}
建立验证失败时要回传的讯息
private static void ChallengeAuthRequest(HttpActionContext filterContext)
{
var dnsHost = filterContext.Request.RequestUri.DnsSafeHost;
filterContext.Response = filterContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
filterContext.Response.Headers.Add("WWW-Authenticate", string.Format("validate failed", dnsHost));
}
于WebApiConfig.cs中注册新增的Filter
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
GlobalConfiguration.Configuration.Filters.Add(new WebApi.Filters.ApiAuthenticationFilter());
}
}
最后在需要验证的API加上该Filter即可
[WebApi.Filters.ApiAuthenticationFilter]
public object QueryApi(string pInput)
{
return null;
}
转载自:AlenWu的程式学习笔记
以Attribute加上Header验证的更多相关文章
- webapi swagger自定义 HTTP Header验证用户
问题描述 webapi自定义的一种验证方式(token放入header里),使用swagger测试时由于header里没值所以一直拿不到用户. 解决如下:(从标题2开始,标题1处处理全局验证用户) 1 ...
- asp.net web form中 用attribute实现权限验证方式
以前项目的代码比较陈旧,今天抽空优化了一下.作为记录. 以前每次请求一个方法都要验证是否登录 if xxx等 现在通过global文件中的改进 反射这个方法的属性是否需要权限 要的话先验证权限.以下 ...
- Webservice加上SoapHeader验证方式
提供一种基于SoapHeader的自定义验证方式,代码如下: public class MySoapHeader : System.Web.Services.Protocols.SoapHeader ...
- webservice jaxws header验证
@WebService @HandlerChain public class UserService { ... } package com.xx.ws.header; import org.w3c. ...
- 【微信小程序】request请求POST提交数据,记得要加上header
wx.request({ url: '*******', data: { "type":"nearest_village", "district&qu ...
- Python爬虫--抓取糗事百科段子
今天使用python爬虫实现了自动抓取糗事百科的段子,因为糗事百科不需要登录,抓取比较简单.程序每按一次回车输出一条段子,代码参考了 http://cuiqingcai.com/990.html 但该 ...
- Net中Attribute特性的高级使用及自定义验证实现
好久没写博客了,今天在百忙之中抽空来写篇文章,记录一下最近深入学习Attribute特性的笔记及心得.~~ 一.什么是特性? 特性(Attribute)是用于在运行时传递程序中各种元素(比如类.方法. ...
- Asp.net MVC验证那些事(4)-- 自定义验证特性
在项目的实际使用中,MVC默认提供的Validation Attribute往往不够用,难以应付现实中复杂多变的验证需求.比如, 在注册用户的过程中,往往需要用户勾选”免责声明”,这个checkbox ...
- SAE微信公众号PHP SDK, token一直验证失败
用的是SAE,创建的是微信公众号PHP SDK框架,里面example文件夹下有server.php用来验证token的.但是问题来了,无论我怎么输入URL和token,一直告诉我token验证失败. ...
随机推荐
- HTML5经典案例学习-----新元素添加文档结构
直接上代码了,大家如果发现问题了,记得提醒我哦,谢谢啦,嘻嘻 <!DOCTYPE html> <!-- 不区分大小写 --> <html lang="en&qu ...
- springboot的几种启动方式
一:IDE 运行Application这个类的main方法 二:在springboot的应用的根目录下运行mvn spring-boot:run 三:使用mvn install 生成jar后运行 先到 ...
- spring boot+mybatis+generator生成domain大小写问题
之前遇到一个问题,用generator生成数据库对应的domain,以前都是好好的,那天突然生成的domain都是小写的,因为我数据库里是大写的,后来找到解决办法, <table tableNa ...
- /proc/diskstats
读取磁盘统计信息,如下所示: linux-HpdBKE:~ # cat /proc/diskstats sda sda1 sda2 dm- dm- dm- sda为整个硬盘的统计信息,sda1为第一个 ...
- spring 启动脚本分析
参考:JVM 参数使用总结 参考:java -Xms -Xmx -XX:PermSize -XX:MaxPermSize 参考:JVM调优总结 -Xms -Xmx -Xmn -Xss 参考:JAVA ...
- Java中的super()使用注意
1)super(参数):调用基类中的某一个构造函数(应该为构造函数中的第一条语句)2)this(参数):调用本类中另一种形成的构造函数(应该为构造函数中的第一条语句)3)super: 它引用当前对象的 ...
- css繼承
概念:就是上級設置了css屬性,下級默認有同樣的樣式,如果下級需要不一樣的樣式,那麼就需要單獨對下級設置. 範圍: font-family:字體 font-weight:粗細 font-size:尺寸 ...
- C#后台绑定select
- Oracle 查询字段不包含多个字符串方法
开发过程中遇到个需求,用户要提取的数据列中不包含 YF.ZF.JD的字符串, 方法1:select * from table where order_no not like '%YF%' and ...
- How to remove unwant Internet Explorer Context Menu
HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\MenuExt