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验证的更多相关文章

  1. MVC—WebAPI(调用、授权)

    ASP.NET MVC—WebAPI(调用.授权)   本系列目录:ASP.NET MVC4入门到精通系列目录汇总 微软有了Webservice和WCF,为什么还要有WebAPI? 用过WCF的人应该 ...

  2. 使用ASP.NET Identity 实现WebAPI接口的Oauth身份验证

    使用ASP.NET Identity 实现WebAPI接口的Oauth身份验证   目前WEB 前后端分离的开发模式比较流行,之前做过的几个小项目也都是前后分离的模式,后端使用asp.net weba ...

  3. webapi swagger自定义 HTTP Header验证用户

    问题描述 webapi自定义的一种验证方式(token放入header里),使用swagger测试时由于header里没值所以一直拿不到用户. 解决如下:(从标题2开始,标题1处处理全局验证用户) 1 ...

  4. kibana去掉丑陋的basic验证框,用自定义验证代替。

    最近在改写kibana,碰到了验证登录的问题.问题是这样子的,nginx设置了basic认证,然后客户端访问kibana的时候总是会弹出登录框,输入用户名和密码,现在要改写这个登陆框,用bootstr ...

  5. Java web的安全约束--Basic验证

    要进行basic验证是用户名/口令机制,当浏览器要访问受保护的资源时,服务器会要求一个用户名和口令,只有输入了合法的用户名和口令.服务器才发送资源.用户名和口令可以存储在安全域中.安全域是标识一个We ...

  6. HTTP Basic 验证客户端 C#实现笔记

    HTTP Basic 验证客户端的原理:把HTTP头重的ContentType设置为:application/x-www-form-urlencoded如果HTTP头没有Authorization,那 ...

  7. C#动态调用带有SoapHeader验证的WebServices

    http://blog.csdn.net/u012995964/article/details/54573143 本文记录C#中通过反射动态的调用带有SoapHeader验证的WebServices服 ...

  8. C#静态调用带有SoapHeader验证的WebServices

    转自:http://blog.csdn.net/u012995964/article/details/54562111 本文记录带有SoapHeader验证的WebServices服务创建.部署及C# ...

  9. WebAPI接口调用身份验证

    Common public interface ICacheWriter { void AddCache(string key, object value, DateTime expDate); vo ...

随机推荐

  1. Nginx的alias的用法及与root的区别

    以前只知道Nginx的location块中的root用法,用起来总是感觉满足不了自己的一些想法.然后终于发现了alias这个东西. 先看toot的用法 location /request_path/i ...

  2. Linux系统加固

    iptables 初始化 > iptables -F #清空所有的链 > iptables -X #清空所有自定义的链 关掉全部端口 > iptables -P INPUT DROP ...

  3. Hibernate添加日志--log4j

    需要导入 slf4j-log4j12-1.6.2.jar slf4j-api-1.6.2.jar log4j-1.2.16.jar 三个jar文件 编写properties文件,建议将日志输出级别设置 ...

  4. Linux 文件日志筛选操作

    统计查看文件以及筛选日志 1.*.log 日志文件中 统计独立ip的个数: awk '{print $1}' test.log | sort | uniq | wc -l 2.#查询访问最多的前10个 ...

  5. MySQL指令

    在mysql里:文件夹就是数据库      文件就是表 创建用户: 格式:create user '用户名'@'IP地址' identified by '密码'; 说明:IP地址是用来限制用户只能在哪 ...

  6. Sql server 查询某个时间段,分布有几周,几月和几日

    1. 查询:以“周”为单位 --查询以下时间段内分别有几周 --时间段:“2017-09-01”到“2017-10-1” select number as wknum from master..spt ...

  7. python(七) Python中单下划线和双下划线

    Python中单下划线和双下划线: 一.分类 (1).以单下划线开头,表示这是一个保护成员,只有类对象和子类对象自己能访问到这些变量. 以单下划线开头的变量和函数被默认是内部函数,使用from mod ...

  8. Freemarker导出word的简单使用

    1.模板 username:${username} password:${password} <#list mylist as item> ${item.name!} ${item.pas ...

  9. pandas 定位 loc,iloc,ix

    In [114]: df Out[114]: A B C D 2018-06-30 0.318501 0.613145 0.485612 0.918663 2018-07-31 0.614796 0. ...

  10. 跟踪OceanLotus的新下载程序KerrDown

    攻击的方法 两种方法将KerrDown下载器传递给目标.一个是使用带有恶意宏的Microsoft Office文档,另一个是包含带有DLL side-loading合法程序的RAR存档 .对于RAR存 ...