zoukankan      html  css  js  c++  java
  • net 网站过滤器 mvc webapi

    WebApi过滤器1

    public class TestController : ApiController
    {
      /// <summary>
      /// 获取用户信息
      /// </summary>
      /// <param name="num">名称</param>
      /// <returns></returns>
      [CustAuthorize]
      public ResultInfo GetUserInfo(int num)
      {
        List<UserInfoViewModel> list=
        new List<UserInfoViewModel>() { new UserInfoViewModel() { Email="960883105@qq.com",HasRegistered=true,LoginProvider="hu"},
        new UserInfoViewModel() { Email="782323@qq.com",HasRegistered=false,LoginProvider="33d"}
      };
      return new ResultInfo(){Status="100", Message = "",Data=list };
      }
      /// <summary>
      /// 测试2
      /// </summary>
      /// <param name="roles">参数2</param>
      /// <returns></returns>
      [CustAuthorize]
      [System.Web.Http.HttpPost]
      public ResultInfo GetUserInfo2()
      {
        List<UserInfoViewModel> list =
          new List<UserInfoViewModel>() { new UserInfoViewModel() { Email="960883105@qq.com",HasRegistered=true,LoginProvider="hu"},
          new UserInfoViewModel() { Email="782323@qq.com",HasRegistered=false,LoginProvider="33d"}
        };
        return new ResultInfo() { Status = "100", Message = "", Data = list };
      }

    }

     

    /// <summary>
    /// 过滤器
    /// </summary>
    public class CustAuthorizeAttribute : AuthorizeAttribute
    {
      //判断权限
      /// <summary>
      ///
      /// </summary>
      /// <param name="actionContext"></param>
      /// <returns></returns>
      protected override bool IsAuthorized(HttpActionContext actionContext)
      {
        string[] rolelist = { "102", "103", "104", "105", "106" };

        var queryString = HttpUtility.ParseQueryString(actionContext.Request.RequestUri.Query);
        var myQueryParam = queryString["roles"];
        if (rolelist.Contains(myQueryParam))
        {
        return true;
        }
        else
        {
        return false;
        }
      }
      //权限为false执行内容
      /// <summary>
      ///
      /// </summary>
      /// <param name="actionContext"></param>
      protected override void HandleUnauthorizedRequest(HttpActionContext actionContext)
      {
        base.HandleUnauthorizedRequest(actionContext);
      }

     WebApi过滤器2

     

    MVC过滤器

    public class TestController : Controller
    {
      [CustAuthorize("102")]
      public ActionResult List()
      {
        return View();
      }
    }

    public class CustAuthorizeAttribute : AuthorizeAttribute
    {
      public String[] roles;

      public CustAuthorizeAttribute(params String[] role)
      {
        roles = role;
      }

      protected override bool AuthorizeCore(HttpContextBase httpContext)
      {
      
        String role = "1012";
        bool b = roles.Contains(role);
          return b;
      }

      protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
      {
        filterContext.HttpContext.Response.Write("<script>alert('无权限')</script>");

      }

    }

     MVC登录验证

    public class UserAuthenAttribute : FilterAttribute, IAuthenticationFilter
    {
      public void OnAuthentication(AuthenticationContext filterContext)
      {

        var user = Cache.ReourseRedis.GetUserIfo();
        var url = filterContext.HttpContext.Request.Url.ToString();
        if (user==null && url.IndexOf("Login")<0)
        {
          filterContext.Result = new RedirectResult("/Test/Login");
        }
      }

      public void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext)
      {

      }
    }

    全局错误信息

    namespace WebApplication1.App_Start
    {
    public class ApiExceptionFilterAttribute : ExceptionFilterAttribute
    {
    public override void OnException(HttpActionExecutedContext context)
    {

    var response = new HttpResponseMessage(HttpStatusCode.OK);
    response.Content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(new ResultInfo() { Status = "102", Message = "error" }));
    context.Response = response;

    base.OnException(context);

    }
    }
    }

     Glob文件=》

     GlobalConfiguration.Configuration.Filters.Add(new ApiExceptionFilterAttribute());

  • 相关阅读:
    归并排序
    将文件存储到数据库中(MySQL)
    JS实现日历控件选择后自动填充
    HDU 1358 Period KMP
    Adobe Flash Player已经终止一项可能不安全的操作,解决方案
    将文件从数据库(MySQL)中进行读取
    Ubuntu java开发环境配置
    硬盘结构和原理
    杭电 HDU 1242 Rescue
    mysql workbench建表时PK,NN,UQ,BIN,UN,ZF,AI
  • 原文地址:https://www.cnblogs.com/lijiasnong/p/6278308.html
Copyright © 2011-2022 走看看