WebApi限制IP地址请求
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",, );
}
}
}
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() > ? 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?)$");
}
}
}
WebApi限制IP地址请求的更多相关文章
- 2-4-搭建DHCP服务实现动态分配IP地址-NTP网络时间同步
本节所讲内容: •DHCP服务器工作原理 •使用DHCP为局域网中的机器分配IP地址 •使用DHCP为服务器分配固定IP地址 •ntpdate加计划任务同步服务器时间 ---------------- ...
- 网络通讯与IP地址
1.主机:Host,台式机,笔记本,手机等设备 2.网络介质:网线,光纤,无线网Wi-Fi.移动网络 3.数据:一串子节 网络通讯的检测:安装wireshark,通常称为抓包工具 抓,capture ...
- 园区IP地址规划(非常详细)
转:https://mp.weixin.qq.com/s/Zlm7x5eunIYLAG7Sp0yVCQ 经过这些年工作,接触从几万.几十万到上亿的项目都有: 我简单总结了接触的大部分的项目,将园区网核 ...
- 获取IP地址 & 伪装IP地址发送请求
//获取请求客户端IP地址 public final static String getIpAddress(HttpServletRequest request) throws IOExcepti ...
- 多级反向代理下,Java获取请求客户端的真实IP地址多中方法整合
在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的.但是在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实I ...
- java 获取请求客户端的真实IP地址
转载自:http://leiyongping88.iteye.com/blog/1545930 用request.getRemoteAddr();方法获取的IP地址是:127.0.0.1或192.16 ...
- 获取请求主机IP地址,如果通过代理进来,则透过防火墙获取真实IP地址;
package com.utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.htt ...
- 服务器使用nginx做代理,通过HttpServletRequest获取请求用户真实IP地址
首先,在nginx配置中添加如下配置 server { listen ; server_name www.wenki.info; #要访问的域名 charset utf8; location / { ...
- JAVA之旅(三十二)——JAVA网络请求,IP地址,TCP/UDP通讯协议概述,Socket,UDP传输,多线程UDP聊天应用
JAVA之旅(三十二)--JAVA网络请求,IP地址,TCP/UDP通讯协议概述,Socket,UDP传输,多线程UDP聊天应用 GUI写到一半电脑系统挂了,也就算了,最多GUI还有一个提示框和实例, ...
随机推荐
- [RxJS] Creation operator: create()
We have been using Observable.create() a lot in previous lessons, so let's take a closer look how do ...
- PowerDesigner 生成带凝视SQL 各个版本号通用10(12、15)
做数据库是设计时最苦恼的事就是用PowerDesigner工具设计完数据库运行SQL文件后没有凝视.那么怎么才干让PowerDesigner设计完有凝视呢,下边教你一个笨的方法,方法尽管笨,可是能实现 ...
- Android 从清单配置文件元数据中获取值
最近在上班工作当中,也尝到了一些新的知识,现总结如下(1)从AndroidManifest.xml配置文件中获取meta数据 // 从Manifest.xml配置文件中获取数据 public stat ...
- Android文档资源大放送 感兴趣的话可以网盘下载(个人收集)
Google.Android.SDK开发范例大全.第3版源码.rar http://pan.baidu.com/s/1c0epYzm 精通Android 3中文版(Pro Android 3).pdf ...
- 人工智能2:智能Agent
一.Agent基本定义 基于理性行为的Agent是本书人工智能方法的核心.Agent由传感器.执行器两个重要元件组成,具有与环境交互的能力,其能力是通过分析感知序列,经过Agent函数映射到相应的行动 ...
- poj 1988 Cube Stacking && codevs 1540 银河英雄传说(加权并茶几)
#include<iostream> #include<cstdio> #include<cstring> #define maxn 30010 using nam ...
- Maven 镜像
http://mvnrepository.com/http://search.maven.org/http://repository.sonatype.org/content/groups/publi ...
- 我对Backbone中url属性的理解
Model中有一个url属性,而且有一个urlRoot属性. Collection中也有一个url属性. // 这是Model中的url方法 url: function() { var base = ...
- 修复 status 为 unusable 的 index
以DBA权限登陆,执行以下脚本即可. declare -- 指向所有 UNUSABLE 状态的 index 的游标 cursor c is select index_n ...
- Mac系统配置多个git账号
1.进入ssh目录 #cd ~/.ssh/ 2.用ssh-keygen命令生成一组新的id_rsa_new和id_rsa_new.pub #ssh-keygen -t rsa -C"new ...