代码:

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 > )
{
stream.Position = ; //当你读取完之后必须把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 > )
{
string token = list[].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);
}
}
}

WebApi的过滤器的更多相关文章

  1. 一、WebAPI自定义过滤器的使用

    一.WebAPI自定义过滤器的使用 1.注册过滤器 using System.Web.Http; using KYINT.WebAPIService.Handler; namespace KYINT. ...

  2. WebApi:过滤器的种类

    WebApi:筛选器的种类 授权筛选器:这些筛选器用于实现IAuthorizationFilter和做出关于是否执行操作方法(如执行身份验证或验证请求的属性)的安全决策.AuthorizeAttrib ...

  3. WebApi 自定义过滤器实现支持AJAX跨域的请求

    我想关于此类话题的文章,大家一搜铺天盖地都是,我写此文的目的,只是对自己学习过程的记录,能对需要的朋友有所帮助,也百感荣幸!!!废话不多说,直接上代码! 客户端:很简单的AJAX请求 <html ...

  4. 【C#】WebApi 添加过滤器,实现对请求参数和响应内容的日志记录

    filter的介绍 filter在Web API中经常会用到,主要用于记录日志,安全验证,全局错误处理等:Web API提供两种过滤器的基本类型:actionfilterattribute,excep ...

  5. .net core webapi 使用过滤器。

    过滤器一般用于权限校验.日志处理... 一:ActionFilterAttribute过滤器. 1:建一个类,继承于ActionFilterAttribute抽象类. public class Log ...

  6. WEBAPI使用过滤器对API接口进行验证

    用户登录控制器:[ActionFilter]自定义过滤器 用户信息:var userData = new JObject();                   userData.Add(" ...

  7. webapi filter过滤器中获得请求的方法详情(方法名,Attributes)

    public class GlobalActionFilter : ActionFilterAttribute { private string _requestId; public override ...

  8. mvc 5 的过滤器和webapi 过滤器 对应实现的action过滤器区别

     asp.net webapi  Action过滤器实现这个: #region 程序集 System.Web.Http, Version=5.2.3.0, Culture=neutral, Publi ...

  9. MVC与WebApi中的异常过滤器

    一.MVC的异常过滤器   1.自定义MVC异常过滤器 创建一个类,继承HandleErrorAttribute即可,如果不需要作为特性使用直接实现IExceptionFilter接口即可, 注意,该 ...

随机推荐

  1. ASP.NET Form身份验证方式详解

    注:不会涉及ASP.NET的登录系列控件以及membership的相关话题, 我只想用比较原始的方式来说明在ASP.NET中是如何实现身份认证的过程.   ASP.NET身份认证基础 在开始今天的博客 ...

  2. 直接在浏览器运行jsx及高版本的js代码

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  3. [Shell]Bash基本功能:多命令顺序执行与管道符

    /*----------------------------------------------------------------------------------------------- @黑 ...

  4. (转) 为什么选择.NETCore?

    https://www.cnblogs.com/xiaoliangge/p/8373100.html 为什么选择.NETCore?    为什么选择.NETCore? 学习新的开发框架是一项巨大的投资 ...

  5. eclipse maven项目 热部署

    热部署:本地项目一键发布到远程服务器中 热部署步骤: 1. 在tomat/conf/tomcat-users.xml添加 <role rolename="manager-gui&quo ...

  6. 学JS的心路历程 -物件与原型(二)

    昨天有提到说Object.setPrototypeOf可以指定一个物件为另一个物件的原型,但有想过到底这个原型,也就是[[Prototype]]最终会到何处吗? 答案是Object.prototype ...

  7. java-学习2

    第一节 Java语言介绍 1.Java的起源 Oak-->Java      交互式操作智能家居 2.Java的发展 Java1.0 Java1.2    JavaSE  :Java平台标准版  ...

  8. 终止执行js的方法

    (一)在function里面 (1)return;(2)return false; (二)非function方法里面 alert("before error.");throw Sy ...

  9. 1.3.3、CDH 搭建Hadoop在安装之前(端口---CDH组件使用的端口)

    列出的所有端口都是TCP. 在下表中,每个端口的“ 访问要求”列通常是“内部”或“外部”.在此上下文中,“内部”表示端口仅用于组件之间的通信; “外部”表示该端口可用于内部或外部通信. Compone ...

  10. Light Probe

    [Light Probe] Light Probes provide a way to capture and use information about light that is passing ...