摘要

对第三方开放的接口,处于安全的考虑需要对其进行安全认证,是否是合法的请求。目前在项目中也遇到这种情况,提供的接口因为涉及到客户铭感数据,所以在调用的时候,不能直接暴露,需要有一个认证的机制。所以对接口安全认证的方式,进行了调研,这里提供一个自定义安全认证的Filter例子。

一个例子

在mvc中,如果需要对某个页面如果用户不登录则无法访问,我们的做法可能是校验session或者使用特性[Authorize]

    [Authorize]
public class UserController : Controller
{
// GET: User
public ActionResult Index()
{
//if (Session["user"]==null)
//{
// return RedirectToAction("login");
//}
return View();
}
}

在mvc中,这种认证方式基于windows或者form认证。但在提供web api给移动端app调用的时候,windows或者form认证就不太合适了。但我们可以自定义一种filter对当前访问的用户进行认证。

自定义Filter特性

    /// <summary>
/// 基于http basic认证
/// </summary>
public class CustomerBasicAuthrizeAttribute : AuthorizationFilterAttribute
{
//重写OnAuthorization 方法 public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext)
{
//如果action带有允许匿名访问的特性,则直接返回,不再进行安全认证
if (actionContext.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any())
{
return;
}
if (actionContext.Request.Headers.Authorization != null)
{
if (actionContext.Request.Headers.Authorization.Scheme != "Basic")
{
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.Unauthorized,
new HttpException("no token"));
}
else
{
string base64Para = actionContext.Request.Headers.Authorization.Parameter;
//解码base64字符串
byte[] buffer = Convert.FromBase64String(base64Para);
string decodeBase64 = Encoding.UTF8.GetString(buffer);
if (!string.IsNullOrEmpty(decodeBase64))
{
string[] paras = decodeBase64.Split(':');
if (paras.Length > )
{
string userName = paras[];
string pwd = paras[];
if (userName == "wolfy" && pwd == "")
{
}
else
{
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.Unauthorized,
new HttpException("userName or pwd is error."));
}
}
else
{
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.Unauthorized,
new HttpException("no token"));
} }
else
{
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.Unauthorized,
new HttpException("no token"));
}
} }
else
{
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.Unauthorized,
new HttpException("no Authorization header"));
} base.OnAuthorization(actionContext);
}
}

使用postman测试

认证成功

第三方调用的时候,可以为其生成一对appname和appsecret放在供客户端进行使用。客户端使用的时候在请求头的Authorization中添加base64字符串就可以了。对于basic对应的值,是base64字符串,如果感觉还不安全可以尝试使用SSL方式。

SSL协议详解

最新文章

  1. echarts学习总结
  2. Uncaught SyntaxError: Unexpected token ILLEGAL
  3. Entity Framework with nolock. 允许脏读
  4. OO之美3
  5. kafka分布式消息队列 — 基本概念介绍
  6. apache开源项目 -- Tuscany
  7. 你需要知道的九大排序算法【Python实现】源码
  8. android:强大的图片下载和缓存库Picasso
  9. Sublime Text 3 搭建Go开发环境(Windows)
  10. dpdk环境配置
  11. hive笔记:复杂数据类型-array结构
  12. Linux Makefile 生成 *.d 依赖文件及 gcc -M -MF -MP 等相关选项说明【转】
  13. EBS打补丁参考
  14. 对 String 字符串的理解
  15. [转]抛弃jQuery,使用原生JavaScript
  16. R语言编程艺术(3)R语言编程基础
  17. Linux 系统级别优化_【all】
  18. OGG 问题
  19. Microsoft visual c++ 14.0 is required问题
  20. Kubernetes v1.10----部署kubernetes-dashboard v1.83

热门文章

  1. 深度学习国外课程资料(Deep Learning for Self-Driving Cars)+(Deep Reinforcement Learning and Control )
  2. SQL SERVER中查询某个表或某个索引是否存在
  3. 20165203《Java程序设计》第九周学习总结
  4. mdb数据库文件如何导入Microsoft SQL Server 2008中
  5. java解析Xml格式的字符串
  6. 开源框架:SDWebImage
  7. nginx-request_time和upstream_response_time
  8. Jenkins+Docker持续集成
  9. python使用cookie登陆网页
  10. Ionic实战一:Ionic仿照微信项目