代码:

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);
        }
    }
}

 

posted on 2017-12-07 17:57  邢帅杰  阅读(209)  评论(0编辑  收藏  举报