WebApi限制IP地址请求(读取XML)
来源:互联网 发布:淘宝申诉小票 编辑:程序博客网 时间:2024/06/07 14:01
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Http.Filters;
using System.Net.Http;
using System.Net;
using System.Collections;
using System.Text.RegularExpressions;
using System.IO;
using System.Web.UI.WebControls;
using System.Text;
using System.Web.Caching;
using System.Xml.Linq;
namespace IM.WeiXinApi.Filter
{
public class AuthorizeAttrbute : System.Web.Http.Filters.AuthorizationFilterAttribute
{
public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext)
{
var ipaddress = GetIpaddress(); //用户的ip
object obj = null;
obj = GetCache(ipaddress+"api"); //获取请求api的ip列表
{
if (obj == null)
{
if (!IpConfig(ipaddress))
{
//返回401错误
actionContext.Response = new HttpResponseMessage
{
Content = new StringContent("当前ip地址" + ipaddress + "无访问权限",
Encoding.GetEncoding("UTF-8"), "application/json"),
StatusCode = HttpStatusCode.Unauthorized
};
return;
}
else
{
SetCache(ipaddress+"api",1, 6);
}
}
}
base.OnAuthorization(actionContext);
}
public static bool IpConfig(string ip)
{
string urlIndex = "~/Xml/roleip.xml";
string FileName = System.Web.HttpContext.Current.Server.MapPath(urlIndex);
XDocument doc = XDocument.Load(FileName);
var rel = from p in doc.Descendants("item") where p.Attribute("ip").Value.ToLower() == ip select p;
return rel != null && rel.Count() > 0 ? true : false;
}
/// <summary>
/// 获取当前应用程序指定CacheKey的Cache值
/// </summary>
/// <param name="CacheKey"></param>
/// <returns></returns>
public static object GetCache(string CacheKey)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
return objCache[CacheKey];
}
/// <summary>
/// 设置缓存
/// </summary>
/// <param name="CacheKey"></param>
/// <param name="objObject"></param>
/// <param name="expires_in"></param>
public static void SetCache(string CacheKey, object objObject, double expires_in)
{
Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject, null, DateTime.Now.AddHours(expires_in), Cache.NoSlidingExpiration);
}
/// <summary>
/// 获取IP地址
/// </summary>
/// <returns></returns>
public static string GetIpaddress()
{
string result = String.Empty;
result = HttpContext.Current.Request.ServerVariables["HTTP_CDN_SRC_IP"];
if (string.IsNullOrEmpty(result))
result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
if (string.IsNullOrEmpty(result))
result = HttpContext.Current.Request.UserHostAddress;
if (string.IsNullOrEmpty(result) || !IsIP(result))
return "127.0.0.1";
return result;
}
public static bool IsIP(string ip)
{
return Regex.IsMatch(ip, "^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$");
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Http.Filters;
using System.Net.Http;
using System.Net;
using System.Collections;
using System.Text.RegularExpressions;
using System.IO;
using System.Web.UI.WebControls;
using System.Text;
using System.Web.Caching;
using System.Xml.Linq;
namespace IM.WeiXinApi.Filter
{
public class AuthorizeAttrbute : System.Web.Http.Filters.AuthorizationFilterAttribute
{
public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext)
{
var ipaddress = GetIpaddress(); //用户的ip
object obj = null;
obj = GetCache(ipaddress+"api"); //获取请求api的ip列表
{
if (obj == null)
{
if (!IpConfig(ipaddress))
{
//返回401错误
actionContext.Response = new HttpResponseMessage
{
Content = new StringContent("当前ip地址" + ipaddress + "无访问权限",
Encoding.GetEncoding("UTF-8"), "application/json"),
StatusCode = HttpStatusCode.Unauthorized
};
return;
}
else
{
SetCache(ipaddress+"api",1, 6);
}
}
}
base.OnAuthorization(actionContext);
}
public static bool IpConfig(string ip)
{
string urlIndex = "~/Xml/roleip.xml";
string FileName = System.Web.HttpContext.Current.Server.MapPath(urlIndex);
XDocument doc = XDocument.Load(FileName);
var rel = from p in doc.Descendants("item") where p.Attribute("ip").Value.ToLower() == ip select p;
return rel != null && rel.Count() > 0 ? true : false;
}
/// <summary>
/// 获取当前应用程序指定CacheKey的Cache值
/// </summary>
/// <param name="CacheKey"></param>
/// <returns></returns>
public static object GetCache(string CacheKey)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
return objCache[CacheKey];
}
/// <summary>
/// 设置缓存
/// </summary>
/// <param name="CacheKey"></param>
/// <param name="objObject"></param>
/// <param name="expires_in"></param>
public static void SetCache(string CacheKey, object objObject, double expires_in)
{
Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject, null, DateTime.Now.AddHours(expires_in), Cache.NoSlidingExpiration);
}
/// <summary>
/// 获取IP地址
/// </summary>
/// <returns></returns>
public static string GetIpaddress()
{
string result = String.Empty;
result = HttpContext.Current.Request.ServerVariables["HTTP_CDN_SRC_IP"];
if (string.IsNullOrEmpty(result))
result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
if (string.IsNullOrEmpty(result))
result = HttpContext.Current.Request.UserHostAddress;
if (string.IsNullOrEmpty(result) || !IsIP(result))
return "127.0.0.1";
return result;
}
public static bool IsIP(string ip)
{
return Regex.IsMatch(ip, "^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$");
}
}
}
http://www.cnblogs.com/nzcblog/p/5073871.html
阅读全文
0 0
- WebApi限制IP地址请求(读取XML)
- ASP.NET WebApi实现请求频率限制
- IP地址限制函数
- oracle IP地址限制
- nginx限制ip地址
- 获取请求ip地址
- 获取请求IP地址
- 获取请求IP地址
- java 读取请求地址
- Nginx 限制 IP 请求数
- 限制某段IP地址
- 巧破IP地址下载限制
- IP 地址限制保护站点
- 限制IP地址访问数据库
- WebAPI请求
- 读取请求中的xml
- Tomcat配置IP访问限制与允许(server.xml)
- java 获取请求IP地址
- ubuntu14编译python2.7.13
- Spring MVC的配置
- 总结篇-SpringMVC
- 前端模块化
- linux常用系统命令
- WebApi限制IP地址请求(读取XML)
- ping: unknown host www.baidu.com
- mongodb学习历程(原创总结+转载收集)
- spring mvc注解搭建
- 算法引论--多项式求值
- WinForm条件下构建自己的流程图引擎
- angular1项目相关扩展及库整理
- 白沙洲新平台搭建-问题总结
- 01、使用OpenStack4j快速开发对外接口