• WebApi的过滤器


    代码:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Security.Principal;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Web;
    using System.Web.Http.Controllers;
    using System.Web.Http.Filters;
    using System.Web.Script.Serialization;
    using Ninject;
    using System.Net.Http;
    
    namespace ADT.TuDou.OAuthWebAPI.Package
    {
        public class SessionKeyModel
        {
            public string UserToken { get; set; }
        }
        public class SessionValidateAttribute : ActionFilterAttribute
        {
            public const string SessionKeyName = "UserToken";
            public const string LoginUserName = "LoginUser";
    
            public override void OnActionExecuting(HttpActionContext filterContext)
            {
                try
                {
                    bool isok = false;
    
                    string requestDataStr = "";
                    JavaScriptSerializer jsr = new JavaScriptSerializer();
    
                    string method = filterContext.Request.Method.Method.ToLower();
                    if (method == "post")
                    {
                        //获取POST请求数据
                        Task<Stream> task = filterContext.Request.Content.ReadAsStreamAsync();
                        task.Wait();
                        Stream stream = task.Result;
                        if (stream != null && stream.Length > 0)
                        {
                            stream.Position = 0; //当你读取完之后必须把stream的读取位置设为开始
                            using (StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8))
                            {
                                requestDataStr = reader.ReadToEnd().ToString();
                            }
                        }
                    }
                    else
                    {
                        //获取GET请求数据
                        List<KeyValuePair<string, string>> list = filterContext.Request.GetQueryNameValuePairs().Where(c => c.Key == SessionKeyName).ToList();
                        if (list.Count > 0)
                        {
                            string token = list[0].Value;
                            if (!string.IsNullOrEmpty(token))
                            {
                                requestDataStr = "{"" + SessionKeyName + "":"" + token + ""}";
                            }
                        }
                    }
    
                    if (!string.IsNullOrEmpty(requestDataStr))
                    {
                        SessionKeyModel model = jsr.Deserialize<SessionKeyModel>(requestDataStr);
                        if (model != null && !string.IsNullOrEmpty(model.UserToken))
                        {
                            IKernel kernel = new StandardKernel();
                            kernel.Bind<ADT.TuDou.IBLL.IUserInfo>().To<ADT.TuDou.BLL.UserInfo>();
                            kernel.Bind<ADT.TuDou.IDAL.IUserInfo>().To<ADT.TuDou.DAL.UserInfo>();
    
                            kernel.Bind<ADT.TuDou.IBLL.IUserLoginLog>().To<ADT.TuDou.BLL.UserLoginLog>();
                            kernel.Bind<ADT.TuDou.IDAL.IUserLoginLog>().To<ADT.TuDou.DAL.UserLoginLog>();
    
                            kernel.Bind<ADT.TuDou.IBLL.IScoreRecords>().To<ADT.TuDou.BLL.ScoreRecords>();
                            kernel.Bind<ADT.TuDou.IDAL.IScoreRecords>().To<ADT.TuDou.DAL.ScoreRecords>();
    
                            IBLL.IUserInfo UserInfoBLL = kernel.Get<IBLL.IUserInfo>();
                            ADT.TuDou.Models.UserInfo userInfo = UserInfoBLL.GetUserByToken(model.UserToken);
                            if (userInfo != null)
                            {
                                //正常
                                isok = true;
                            }
                        }
                    }
                    
                    if (!isok)
                    {
                        filterContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.NoContent);
                        filterContext.Response.Content = new StringContent("{"ERROR":"用户票证不正确"}");
                    }
                }
                catch (Exception ex)
                {
                    filterContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.InternalServerError);
                    filterContext.Response.Content = new StringContent("{"ERROR":"" + ex.Message + ""}");
                }
                base.OnActionExecuting(filterContext);
            }
        }
    }
  • 相关阅读:
    socket编程之二:两种链接类型tcp和udp
    屌丝、小白怎么拿国内巨头offer
    python调用tcpdump抓包过滤
    设计模式6大原则
    Javascript中bind()方法的使用与实现
    vue-cli中的check-versions.js配置文件包括semver,chalk,shell插件的解释
    基础组件(二)
    浏览器跨域请求之credentials
    跨域资源共享 CORS 详解
    PHP从入门到精通(六)
  • 原文地址:https://www.cnblogs.com/xsj1989/p/8000320.html
Copyright © 2020-2023  润新知