zoukankan      html  css  js  c++  java
  • ASP.Net 过滤器

    授权过滤器

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Http;
    using System.Web.Http.Controllers;
    using System.Web.Http.Filters;
    using WebApplication17.Models;
    using Newtonsoft.Json;
    using System.Net.Http;
    
    namespace WebApplication17.Models
    {
        public class MyAuthrizeAttribute:AuthorizeAttribute
        {
            public override void OnAuthorization(HttpActionContext actionContext)
            {
                //base.OnAuthorization(actionContext);
                if (actionContext.Request.Headers.Contains("BWAUTH"))
                {
                    var headers = actionContext.Request.Headers.GetValues("BWAUTH");
                    JWTHelper helper = new JWTHelper();
                    string playload = helper.GetPayload(headers.FirstOrDefault());
                    UserInfo user = JsonConvert.DeserializeObject<UserInfo>(playload);
                    actionContext.ControllerContext.RouteData.Values.Add("User", user);
    
                }
                else
                {
                    var msg = new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.OK);
                    BWActionResult result = new BWActionResult() { Code = 1, Msg = "未认证", Data = null };
                    msg.Content = new StringContent(JsonConvert.SerializeObject(result));
                    actionContext.Response = msg;
    
                }
            }
        }
    }
    View Code

    结果过滤器

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Http.Filters;
    using System.Net.Http;
    using Newtonsoft.Json;
    
    namespace WebApplication17.Models
    {
        public class BWActionFilterAttribute:ActionFilterAttribute
        {
            public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
            {
                var result =   actionExecutedContext.ActionContext.Response.Content.ReadAsAsync<object>().Result;
                BWActionResult data = new BWActionResult() { Code = 0, Msg = "Ok", Data = result };
                actionExecutedContext.Response.Content = new StringContent( JsonConvert.SerializeObject(data), System.Text.Encoding.UTF8,"application/json" );
            }
        }
    }
    View Code
  • 相关阅读:
    Codeforces 1244C The Football Season (解方程)
    BZOJ 2006 [NOI2010]超级钢琴(ST表+堆)
    51Nod 1681 公共祖先(dfs+树状数组/主席树)
    CF 1076E Vasya and a Tree(dfs+树状数组)
    CF 1076 D Edge Deletion(最短路径+bfs)
    CF Gym 102059I Game on Plane(sg函数)
    POJ 2311 Cutting Game(二维sg)
    POJ 2960 S-Nim(SG函数模板题)
    CF Gym 102059F Fake Plastic Trees
    CF Gym 102059H Fractions(思维题)
  • 原文地址:https://www.cnblogs.com/XJNB/p/13343127.html
Copyright © 2011-2022 走看看