WebAPI的AuthorizeAttribute扩展类中获取POST提交的数据

在WEBAPI中,AuthorizeAttribute类重写时,如何获取post数据是个难题,网上找资料也不好使,只能自己研究,通过研究发现,WEBAPI给了我们获取POST数据的可能,下面介绍一下:

//将POST数据以字符串的形式读取,例如post的json数据,就可以以这种方式读取 
actionContext.Request.Content.ReadAsStringAsync();


//将POST数据以内容流的形式读取 
actionContext.Request.Content.ReadAsStreamAsync();


//将POST数据以FORM键值对的形式读取,但是提交的数据须得是FORM提交的才可以 
actionContext.Request.Content.ReadAsFormDataAsync();

代码具体如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Net.Http;
 5 using System.Web;
 6 using System.Web.Http;
 7 using System.Web.Http.Controllers;
 8 using BoxSecurity;
 9
10 namespace BoxWebApiFilter
11 {
12     [AttributeUsage(AttributeTargets.Interface | AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
13     public class ApiAuthorizeFilter : AuthorizeAttribute
14     {
15         private static List<string> PublicCmds = new List<string>()
16         {
17             "XXXX"
18         };
19         protected override bool IsAuthorized(HttpActionContext actionContext)
20         {
21             // 验证token
22             //var token = actionContext.Request.Headers.Authorization;
23             var ts = actionContext.Request.Headers.Where(c => c.Key.ToLower() == "tokenxxx").FirstOrDefault().Value;
24             var requestContent = actionContext.Request.Content.ReadAsStringAsync();//读取post提交的json数据
25             requestContent.Wait();//等待异步读取结束
26             var inputJson = requestContent.Result;
27             //此接口无需token验证
28             BoxCommand.CmdRequest request =
29                 Newtonsoft.Json.JsonConvert.DeserializeObject<BoxCommand.CmdRequest>(inputJson);
30             if (PublicCmds.Contains(request.Cmd))
31             {
32                 return true;
33             }
34
35             if (ts != null && ts.Count() > 0)
36             {
37                 var token = ts.First<string>();
38                 // 验证token
39                 return TokenHelper.IsExistToken(token);
40             }
41
42             if (actionContext.Request.Method == HttpMethod.Options)
43                 return true;
44             return false;
45
46         }
47     }
48 }

原文地址:https://www.cnblogs.com/zhifengfly/p/9272829.html

时间: 2024-08-26 20:59:47

WebAPI的AuthorizeAttribute扩展类中获取POST提交的数据的相关文章

跟王老师学反射(四):Class类:从Class类中获取信息

跟王老师学反射(四)Class类:从Class类中获取信息 主讲教师:王少华   QQ群号:483773664 学习内容 获得class类中的信息 根据我们以前学过的一个Java类有以下几部组成,如下代码所示 一.访问Class对应的类所包含的构造方法 (一)public Constructor<T> getConstructor(Class<?>... parameterTypes) 返回此Class对象所表示的类的指定public构造方法. parameterTypes参数是按

在普通类中获取Spring管理的bean

1.在项目中添加下面的类: import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; /** * 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext. * */ public class SpringContextHolder implem

spring在普通类中获取session和request

在使用spring时,经常需要在普通类中获取session,request等对像.比如一些AOP拦截器类,在有使用struts2时,因为struts2有一个接口使用org.apache.struts2.ServletActionContext即可很方便的取到session对像.用法:ServletActionContext.getRequest().getSession(); 但在单独使用spring时如何在普通类中获取session,reuqest呢?首先要在web.xml增加如下代码: <l

【原创】大叔问题定位分享(36)openresty(nginx+lua)中获取不到post数据,ngx.req.get_body_data返回nil

openresty(nginx+lua)中获取不到post数据,ngx.req.get_body_data返回nil This function returns nil if the request body has not been read, the request body has been read into disk temporary files, or the request body has zero size. 打开nginx调试日志 error_log /var/log/ng

java类中获取ServletContext的方法

起因是我想要获取一个相对路径,需要用到servletContext的getRealPath()方法,于是上网搜索,找到两种方法来获取ServletContext. 第一种方法是这样的: ServletActionContext.getServletContext(): 后来发现这种方法只有在从浏览器打开的时候才能获取到ServletContext,否则在普通的java类中会报空指针错误(找不到ServletContext),猜测可能是因为ServletActionContext是struts2封

如何在java类中获取javaWeb的根路径

我们有时候需要在java类中(包括util类等)获取web的根路径,可以通过class类路径来获取: public static String getRealPath(Class clazz) { String url = clazz.getResource("").getPath(); int displace = url.indexOf("WEB-INF"); StringBuffer buffer = new StringBuffer(); for (int

Action类中获取request等对象的方法

struts2中的action类中,SevletActionContext可以获取 session对象则通过ActionContext.getContext().getSession().put("name",value);存放值  ActionContext.getContext().getSession().get("name"); 获取值

5.struts2中Action类中获取ServletAPI的三种方式

**Servlet的API的访问(开发中偶尔会使用到)** 1.在Action类中也可以获取到Servlet一些常用的API,有如下三种方式获取 * 完全解耦合的方式 * 使用接口注入的方式 * 使用ServletActionContext中静态方法直接访问Servlet的API * 需求:提供JSP的表单页面的数据,在Action中使用Servlet的API接收到,然后保存到三个域对象中,最后再显示到JSP的页面上. * 提供JSP注册的页面,演示下面这三种方式: <h3>注册页面</

.NET CORE 2.0之 依赖注入在类中获取IHostingEnvironment,HttpContext

在.NET CORE 中,依赖注入非常常见, 在原先的 HttpContext中常用的server.Mappath已经么有了如下: HttpContext.Current.Server.MapPath("xx") 取而代之的是IHostingEnvironment 环境变量 可以通过依赖注入方式来使用,不过在大多数的情况下 我们需要在,类中使用,通过传统入的方式就不太合适,如下: 可以换一种方式来处理 新建一个类如下: public static class MyServiceProv