首页 > 其他 > 详细

特性Attribute 的使用

时间:2015-04-01 13:05:54      阅读:87      评论:0      收藏:0      [点我收藏+]

  [IdentityAuthorize]   
        public ActionResult Index()
        {
            return View("~/Views/Index.cshtml");
        }

 

  [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
    public class IdentityAuthorizeAttribute : ActionFilterAttribute, IActionFilter
    {
        /// <summary>
        /// 验证是否登陆,没有登陆跳转到 登陆页
        /// </summary>
        /// <param name="filterContext">上下文</param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            try
            {
                if (!Identity.IsAuthenticated)
                {
                    string url = HttpContext.Current.Request.Url.ToString();
                    HttpContext.Current.Response.Clear();
                    HttpContext.Current.Response.Write("<script>window.top.location=‘" + Config.WapLoginUrl + "?backurl=" + url + "‘</script>");
                    HttpContext.Current.Response.End();
                }
                base.OnActionExecuting(filterContext);
            }
            catch (Exception exception)
            {
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.Write(exception.Message);
                HttpContext.Current.Response.End();
            }
        }
    }

特性Attribute 的使用

原文:http://www.cnblogs.com/huaqiqi/p/4383424.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!