• 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);
            }
        }
    }
  • 相关阅读:
    c# 三种取整方法 向上取整 向下取整 四舍五入
    Lambda表达式对DataRow处理
    Dapper数据库字段和model属性映射
    union limit
    北邮五十题
    搜索____深搜 学易错点
    动态规划____有重叠子问题的搜索,都可以转为记忆化搜索
    64位 __int 与 long long写法
    做做 卡特兰数 与 卡米歇尔数
    vector 有点麻烦啊 能简单点么?
  • 原文地址:https://www.cnblogs.com/xsj1989/p/8000320.html
Copyright © 2020-2023  润新知