[C#]Webapi在筛选器中对Get与Post请求方式的接口验证方式

之前一直写Get方式的接口,最近在写个推的群推接口和头像上传接口时需要用Post方式进行请求。由于用Get请求进行接口的权限验证是通过拦截url中的token参数,在通过token参数去数据库中获得权限列表。但Post请求如果是参数放在请求体中那就不能用这种方式了。而且有的开发者post方式验证直接放在请求方法中验证,而不是放在filter类中。

一、拦截url中token方式的权限验证

//url获取token
           var content = actionContext.Request.Properties[ConfigParas.MS_HttpContext] as HttpContextBase;
           var getToken = content.Request.QueryString[ConfigParas.Token];
           //如果登录不用验证
           if (actionContext.ActionDescriptor.ActionName == "Login")
           {
               base.IsAuthorized(actionContext);
               return;
           }

如果是get方式或者public Task<Hashtable> ImgUpload([FromUri]string token)这种方式的post方式请求是可以拦截到Url,继而可以获得token进行验证.验证方式

if (!string.IsNullOrEmpty(getToken))
            {             
                //进行token校验
                if (!string.IsNullOrEmpty(getToken))
                {
                    //解密用户ticket,并校验用户名密码是否匹配
                    var routes = new RouteCollection();
                    RouteConfig.RegisterRoutes(routes);
                    RouteData routeData = routes.GetRouteData(content);
                    //取出区域的控制器Action,id
                    string controller = actionContext.ActionDescriptor.ControllerDescriptor.ControllerName;
                    string action = actionContext.ActionDescriptor.ActionName;
                    //URL路径
                    string filePath = HttpContext.Current.Request.FilePath;
                    if (LoginUserManage.ValidateTicket(getToken) && ValiddatePermission(getToken, controller, action, filePath)) 
                    {
                        //已经登录,有权限,且没有单机登录限制
                        base.IsAuthorized(actionContext);
                    }
                    else
                    {
                        HandleUnauthorizedRequest(actionContext);
                    }
                }
                //如果取不到身份验证信息,并且不允许匿名访问,则返回未验证401
                else
                {
                    var attributes = actionContext.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().OfType<AllowAnonymousAttribute>();
                    bool isAnonymous = attributes.Any(a => a is AllowAnonymousAttribute);
                    if (isAnonymous) base.OnAuthorization(actionContext);
                    else HandleUnauthorizedRequest(actionContext);
                }
            }

上图:拦截的token及获得的权限列表,请求参数放在url的post方式以同样的拦截方式

gg.png

![4LTAKB{8]P]8TRHL(R%S406.png](http://upload-images.jianshu.io/upload_images/2087602-e78e92ab1dd6eb16.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

获取Post请求体中token对接口进行权限验证

if (content.Request.HttpMethod == "POST")
                {
                    base.OnAuthorization(actionContext);
                    //获取请求消息提数据  
                    Stream stream = actionContext.Request.Content.ReadAsStreamAsync().Result;
                    Encoding encoding = Encoding.UTF8;
                    stream.Position = 0;
                    string responseData = "";
                    using (StreamReader reader = new StreamReader(stream, encoding))
                    {
                        responseData = reader.ReadToEnd().ToString();
                    }
                    //反序列化进行处理  
                    var serialize = new JavaScriptSerializer();
                    var obj = serialize.Deserialize<PushBody>(responseData);
                    var psotToken = obj.token;
                    //进行token校验
 if (!string.IsNullOrEmpty(psotToken))
                    {
                        //解密用户ticket,并校验用户名密码是否匹配
                        //读取请求上下文中的Controller,Action,Id
                        var routes = new RouteCollection();
                        RouteConfig.RegisterRoutes(routes);
                        RouteData routeData = routes.GetRouteData(content);
                        //取出区域的控制器Action,id
                        string controller = actionContext.ActionDescriptor.ControllerDescriptor.ControllerName;
                        string action = actionContext.ActionDescriptor.ActionName;
                        //URL路径
                        string filePath = HttpContext.Current.Request.FilePath;
                        if (LoginUserManage.ValidateTicket(psotToken) && ValiddatePermission(psotToken, controller, action, filePath)) //if (LoginUserManage.ValidateTicket(token) && ValiddatePermission(token, controller, action, filePath))  
                        {
                            //已经登录,有权限,且没有单机登录限制
                            base.IsAuthorized(actionContext);
                        }
                        else
                        {
                            HandleUnauthorizedRequest(actionContext);
                        }
                    }
                    //如果取不到身份验证信息,并且不允许匿名访问,则返回未验证401
                    else
                    {
                        var attributes = actionContext.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().OfType<AllowAnonymousAttribute>();
                        bool isAnonymous = attributes.Any(a => a is AllowAnonymousAttribute);
                        if (isAnonymous) base.OnAuthorization(actionContext);
                        else HandleUnauthorizedRequest(actionContext);
                    }
                }

上图:

rr.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,268评论 19 139
  • iOS网络架构讨论梳理整理中。。。 其实如果没有APIManager这一层是没法使用delegate的,毕竟多个单...
    yhtang阅读 5,332评论 1 23
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,593评论 25 709
  • 感觉自己过了那种天天煲剧,煲那种帅哥美女的偶像剧的年龄了,也可能真的是年龄大了,虽然我很不想承认这个,上了大学才慢...
    scarJJ阅读 213评论 0 0
  • 现在主流的Android开发环境有: ①Eclipse + ADT + SDK ②Android Studio +...
    neatline阅读 336评论 0 1