zoukankan      html  css  js  c++  java
  • 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);
            }
        }
    }
  • 相关阅读:
    人事面试13
    人事面试测试篇1
    人事面试16
    人事面试15
    人事面试测试篇3
    人事面试测试篇2
    人事面试14
    Oracle Compile 编译 无效对象
    Oracle 移动数据文件的操作方法
    Oracle 9i 从9.2.0.1升级到 9.2.0.6 步骤
  • 原文地址:https://www.cnblogs.com/xsj1989/p/8000320.html
Copyright © 2011-2022 走看看