WebApi的调用-3.Basic验证

webapi里的特性
/// <summary>
/// Basic验证
/// </summary>
/// <remarks>
///
/// </remarks>
public class BasicAuthorizeAttibute : AuthorizeAttribute
{
public override void OnAuthorization(HttpActionContext actionContext)
{
var authorization = actionContext.Request.Headers.Authorization; //HTTP标头的Authorization值
//ActionDescriptor方法上,ActionDescriptor.ControllerDescriptor 类上
//有[AllowAnonymousAttribute] 的情况下
if (actionContext.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>(true).Count != 0
|| actionContext.ActionDescriptor.ControllerDescriptor.GetCustomAttributes<AllowAnonymousAttribute>(true).Count != 0)
{
base.OnAuthorization(actionContext);
}
else if (authorization != null && authorization.Parameter != null)
{
//用户逻辑验证
if (ValidateTicket(authorization.Parameter))
{
base.IsAuthorized(actionContext);
}
else
{
this.HandleUnauthorizedRequest(actionContext);
}
}
else
{
this.HandleUnauthorizedRequest(actionContext);
}
}
/// <summary>
/// 验证用户逻辑
/// </summary>
/// <param name="encryptTicket" type="string">
///
/// </param>
///
private bool ValidateTicket(string encryptTicket)
{
// var strTicket = FormsAuthentication.Decrypt(encryptTicket.Remove(encryptTicket.Length - 1).Remove(0, 1));
var strTicket = FormsAuthentication.Decrypt(encryptTicket);
return string.Equals(strTicket.UserData, string.Format("{0}&{1}", "admin", "123"));
}
}
获取ticket
[AllowAnonymous]
[HttpGet]
public HttpResponseMessage Login(string account, string password)
{
Model.User user = new User();
if (account == "admin" && password == "123")
{
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(0, account, DateTime.Now,
DateTime.Now.AddHours(1), true, string.Format("{0}&{1}", account, password),
FormsAuthentication.FormsCookiePath);
return Success(user = new User() { name = account, pass = password, ticket = FormsAuthentication.Encrypt(ticket) });
}
else
{
return Msg("登录失败");
}
}
MVC里面请求头(后台请求)
public string GetApi(string method, string queryString)
{
var result = ApiHelper.Instance.RequestApi(method, queryString, GetApiHeader());
return result;
}
private WebHeaderCollection GetApiHeader()
{
string key = string.Format(GlobalVar.UserTiketCacheKey);
var result = CacheHelper.CacheReader(key);
WebHeaderCollection header = new WebHeaderCollection();
header.Add(HttpRequestHeader.Authorization, "BasicAuth " + result);
return header;
}
WebApi的调用-3.Basic验证的更多相关文章
- MVC—WebAPI(调用、授权)
ASP.NET MVC—WebAPI(调用.授权) 本系列目录:ASP.NET MVC4入门到精通系列目录汇总 微软有了Webservice和WCF,为什么还要有WebAPI? 用过WCF的人应该 ...
- 使用ASP.NET Identity 实现WebAPI接口的Oauth身份验证
使用ASP.NET Identity 实现WebAPI接口的Oauth身份验证 目前WEB 前后端分离的开发模式比较流行,之前做过的几个小项目也都是前后分离的模式,后端使用asp.net weba ...
- webapi swagger自定义 HTTP Header验证用户
问题描述 webapi自定义的一种验证方式(token放入header里),使用swagger测试时由于header里没值所以一直拿不到用户. 解决如下:(从标题2开始,标题1处处理全局验证用户) 1 ...
- kibana去掉丑陋的basic验证框,用自定义验证代替。
最近在改写kibana,碰到了验证登录的问题.问题是这样子的,nginx设置了basic认证,然后客户端访问kibana的时候总是会弹出登录框,输入用户名和密码,现在要改写这个登陆框,用bootstr ...
- Java web的安全约束--Basic验证
要进行basic验证是用户名/口令机制,当浏览器要访问受保护的资源时,服务器会要求一个用户名和口令,只有输入了合法的用户名和口令.服务器才发送资源.用户名和口令可以存储在安全域中.安全域是标识一个We ...
- HTTP Basic 验证客户端 C#实现笔记
HTTP Basic 验证客户端的原理:把HTTP头重的ContentType设置为:application/x-www-form-urlencoded如果HTTP头没有Authorization,那 ...
- C#动态调用带有SoapHeader验证的WebServices
http://blog.csdn.net/u012995964/article/details/54573143 本文记录C#中通过反射动态的调用带有SoapHeader验证的WebServices服 ...
- C#静态调用带有SoapHeader验证的WebServices
转自:http://blog.csdn.net/u012995964/article/details/54562111 本文记录带有SoapHeader验证的WebServices服务创建.部署及C# ...
- WebAPI接口调用身份验证
Common public interface ICacheWriter { void AddCache(string key, object value, DateTime expDate); vo ...
随机推荐
- 《Two Dozen Short Lessons in Haskell》所有习题的索引
<Two Dozen Short Lessons in Haskell>(Copyright © 1995, 1996, 1997 by Rex Page,有人翻译为Haskell二十四学 ...
- VUE2.0 饿了吗视频学习笔记(四):颜色、跳转、设置、vue-resource
https://gitee.com/1981633/vue_study.git 源码下载地址,随笔记动态更新中 1.设置选中项颜色 <template> <div id=" ...
- buildroot构建项目(六)--- u-boot 2017.11 适配开发板修改 4 ---- 系统启动初始化之三
一.内存控制器 在关闭了MMU和caches 之后 就进入lowlevel_init 函数,对内存控制器进行初始化.lowlevel_init.S (board\samsung\mini2440) 1 ...
- Nginx实战之让用户通过用户名密码认证访问web站点
1.Nginx实战之让用户通过用户名密码认证访问web站点 [root@master ~]# vim /usr/local/nginx/conf/extra/www.conf server { lis ...
- python队列queue 之优先级队列
import queue as Q def PriorityQueue_int(): que = Q.PriorityQueue() que.put(10) que.put(1) que.put(5) ...
- Debian 9 源配置
Debian 9: deb http://mirrors.163.com/debian/ stretch main non-free contrib deb http://mirrors.163.co ...
- C++11 多线程编程
http://blog.csdn.net/column/details/ccia.html?&page=1
- 搭建Modelsim SE仿真环境-使用do文件仿真
本章我们介绍仿真环境搭建是基于Modelsim SE的.Modelsim有很多版本,比如说Modelsim-Altera,但是笔者还是建议大家使用Modelsim-SE,Modelsim-Altera ...
- Simulink--MATLAB中的一种可视化仿真工具
Simulink是MATLAB中的一种可视化仿真工具, 是一种基于MATLAB的框图设计环境,是实现动态系统建模.仿真和分析的一个软件包,被广泛应用于线性系统.非线性系统.数字控制及数字信号处理的建 ...
- volatile的深入理解--【sky原创】
volatile 所有定义的变量都是在内存上面的, 例如: int i = 10; int j = i; int k = i; i = 10; 是初始化的变量,存放在数据段,未初始化的变量存放在 ...