以前项目的代码比较陈旧,今天抽空优化了一下.作为记录.

以前每次请求一个方法都要验证是否登录 if xxx等  现在通过global文件中的改进 反射这个方法的属性是否需要权限

要的话先验证权限.以下代码 只提供思路和演示.

如何使用

global中的写法是

  protected void Application_AuthenticateRequest(object sender, EventArgs e)
{ if (HttpContext.Current != null)
{
byte[] byts = new byte[HttpContext.Current.Request.InputStream.Length]; HttpContext.Current.Request.InputStream.Read(byts, , byts.Length);
string req = System.Text.Encoding.Default.GetString(byts);
req = HttpContext.Current.Server.UrlDecode(req);
if (!string.IsNullOrEmpty(req))
{
req = req.Replace("data=", "");
var ajaxModel = Utils.JsonHelper.FromJson<AjaxRequestModel>(req);//把请求的流转换为json
string methodName = ajaxModel.MethodAlias;
var className = AjaxCache.GetClassName(methodName); string assemblyName = "Test.Module"; if (!String.IsNullOrEmpty(assemblyName) && !String.IsNullOrEmpty(className))
{
Assembly assembly = GetAssembly(assemblyName);//我这里用的缓存来实现资源加载的不然每次都需要反射
Type type = assembly.GetType(className, true, true);
if (type != null)
{
MethodInfo[] methodInfos = type.GetMethods();
foreach (MethodInfo mi in methodInfos)
{
System.Attribute[] attrs = System.Attribute.GetCustomAttributes(mi); //反射获得用户自定义属性
foreach (System.Attribute attr in attrs)
{
if (attr is CheckLoginAttribute)
{
CheckLoginAttribute a = (CheckLoginAttribute)attr;
System.Console.WriteLine("过了没? ", a.IsLogin);//这里也可以处理 也可以不处理.
}
} } }
} }
}
}
   /// <summary>
/// 反射资源缓存调用
/// </summary>
/// <param name="assemblyName"></param>
/// <returns></returns>
private static Assembly GetAssembly(string assemblyName)
{
object assemblyObject = CacheHelper.GetCache(assemblyName);//这里可以用 iis缓存来实现 if (assemblyObject == null)
{
Assembly assembly = null;
assembly = Assembly.Load(assemblyName);
CacheHelper.SetCache(assemblyName, assembly, DateTime.Now.AddMinutes());
return assembly;
}
else
{
return (Assembly)assemblyObject;
}
}
 [AttributeUsage(AttributeTargets.Method,AllowMultiple=false, Inherited=true  )]
public class CheckLoginAttribute : Attribute
{ /// <summary>
/// 检测是否登录
/// </summary> public bool IsLogin { get; set; }
public CheckLoginAttribute( )
{
try
{
if (==)
{
IsLogin = true;
//throw new Exception("登录错啦");
//var model = new ResponseInfo { State = ResultState.Failed, ErrorMessage = "您未登录,请登录!" };
//HttpContext.Current.Response.Write(JsonConvert.SerializeObject(model));
//HttpContext.Current.Response.End();
}
else
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write("{State:1,Msg='未登录'}");
HttpContext.Current.Response.End(); } }
catch (Exception ex)
{
LogHelper.WriteExceptionLog("CheckLoginAttribute", ex);
throw;
} } }

asp.net web form中 用attribute实现权限验证方式的更多相关文章

  1. 在 ASP.NET Web API 中使用 Attribute 统一处理异常

    并非所有的异常都需要 try-catch 进行重复的处理,这会导致大量的重复性代码,一旦后续系统出现异常处理机制的修改,随着代码量增多,修改也会变的更加困难. ASP.NET Web API 中特别增 ...

  2. ASP.NET Web API中实现版本的几种方式

    在ASP.NET Web API中,当我们的API发生改变,就涉及到版本问题了.如何实现API的版本呢? 1.通过路由设置版本 最简单的一种方式是通过路由设置,不同的路由,不同的版本,不同的contr ...

  3. Asp.net Mvc4 基于Authorize实现的模块权限验证方式

    在MVC中,我们可以通过在action或者controller上设置Authorize[Role="xxx"] 的方式来设置用户对action的访问权限.显然,这样并不能满足我们的 ...

  4. ASP.NET Web Form和MVC中防止F5刷新引起的重复提交问题

    转载 http://www.cnblogs.com/hiteddy/archive/2012/03/29/Prevent_Resubmit_When_Refresh_Reload_In_ASP_NET ...

  5. 在asp.net web form项目中添加webapi接口

    我有一个支付宝服务网关是ASP.NET WEB FORM项目,但是最近这个网关需要对外提供几个接口,想了下,使用web api比较合适,实现很简单,GO 1,首先添加一个文件夹名字叫App_Start ...

  6. 如何在ASP.NET Web站点中统一页面布局[Creating a Consistent Layout in ASP.NET Web Pages(Razor) Sites]

    如何在ASP.NET Web站点中统一页面布局[Creating a Consistent Layout in ASP.NET Web Pages(Razor) Sites] 一.布局页面介绍[Abo ...

  7. Asp.net web form url route使用总结

    asp.net web form 使用URL路由 注不是mvc中的路由 一.前台控件使用路由,通过表达式生成url地址,注意给路由参数赋值,防止使用了其他路由表达式值方式1:<asp:Hyper ...

  8. ASP.Net Web Form<一> aspx文件编译及呈现

    对比复习下JSP 1.jsp的本质是Servlet ,会在第一次被访问时会被翻译成一个类文件,从此对这个页面的访问都是由这个类文件执行后进行输出. aspx 本质是IHttpHandler 2.jsp ...

  9. 【ASP.NET Web API教程】6.2 ASP.NET Web API中的JSON和XML序列化

    谨以此文感谢关注此系列文章的园友!前段时间本以为此系列文章已没多少人关注,而不打算继续下去了.因为文章贴出来之后,看的人似乎不多,也很少有人对这些文章发表评论,而且几乎无人给予“推荐”.但前几天有人询 ...

随机推荐

  1. The Romantic Hero

    Problem Description There is an old country and the king fell in love with a devil. The devil always ...

  2. Old Calculator

    描述 szhhck have an old calculator bought 5 years ago.he find the old machine can just calculate expre ...

  3. 高可用软件Keepalived

    关于高可用软件Keepalived的使用及配置请参见:http://www.voidcn.com/blog/9124573/article/p-5990263.html

  4. 关于css制作圆角

    三个阶段: 1.背景图片: 2.css2.0+标签模拟圆角: 3.css3.0圆角属性(border-radius). 1.1.背景图片--宽度固定,高度自适应圆角 为容器设置宽度 在主体的上方加一个 ...

  5. React History

    1. browerhistory 1. 推荐使用, 2. nginx服务器要配置try-file,当找不到路由时跳转到首页 3. 使用浏览器history对象,不再允许直接访问nn 所以不能直接拷贝地 ...

  6. 媒体查询的应用以及在css3中的变革

    CSS一直都支持设置与媒体相关联的样式表.它们可以适应不同媒体类型的显示.例如,文档在屏幕显示时使用sans-serif字体,在打印时则使用serif字体.screen和print是两种预定义的媒体类 ...

  7. Jackson - Date Handling

    Handling dates on Java platform is complex business. Jackson tries not to make it any harder than it ...

  8. Wireshark - 观察 ARP 地址解析过程

    下面使用 Wireshark 分析 ARP 的工作过程.试验的机器:发送者机器(IP 地址:10.21.28.47,MAC 地址:68:f7:28:0f:32:2e)下文称为 HOSTA:目标机器(I ...

  9. ubuntu(16.04.01)学习-day2

    1.建立硬链接: ln main.c mainsoft 2.建立软链接:ln -s main.c soft 3.对文档进行统计 wc main.c返回行.单词数和字符数 4.查看相应命令的描述 wha ...

  10. SQL自动补充其他月份为0

    ,) ), Sales int,Dates datetime) insert into ProductSale ,'2014-01-05' UNION ALL ,'2014-02-05' UNION ...