using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Reflection; namespace Song.ViewData.Attri { /// /// 请求的区间限制 /// public class RangeAttribute : WeishaAttr { /// /// 验证是否满足特性的限定 /// /// 请求 /// 执行的方法 /// public static bool Verify(Letter letter, MemberInfo method) { List ranges = WeishaAttr.GetAttrs(method); if (ranges.Count < 1) return true; //任意范围,有此特性则直接跳过 AnywhereAttribute any = WeishaAttr.GetAttr(method); if (any != null && !any.Ignore) return true; //局域网内访问 if (letter.Sever.IsIntranetIP) { IntranetAttribute intranet = WeishaAttr.GetAttr(method); if (any != null && !any.Ignore) return true; } //本机访问 if (letter.Sever.IsLocalIP) { LocalhostAttribute local = WeishaAttr.GetAttr(method); if (local != null && !local.Ignore) return true; } //限制同域 DomainAttribute domainAttr = WeishaAttr.GetAttr(method); if (domainAttr != null && !domainAttr.Ignore) { string host = letter.Sever.Domain.ToLower(); if (letter.HTTP_REFERER.ToLower().StartsWith("http://" + host)) return true; if (letter.HTTP_REFERER.ToLower().StartsWith("https://" + host)) return true; } //没有通过,则返回异常 string msg = string.Empty; for (int i = 0; i < ranges.Count; i++) { if (ranges[i] is LocalhostAttribute) msg += "本机"; if (ranges[i] is IntranetAttribute) msg += "局域网"; if (ranges[i] is DomainAttribute) msg += "同域"; if (i < ranges.Count - 1) msg += ","; } throw new Exception(string.Format("当前方法访问受限,仅限{0}访问",msg)); return false; } /// /// 将执行结果写入日志 /// /// public void LogWrite(object execResult) { } } }