using ADT.Core.Encrypt;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Security; namespace ADT.API.App_Start
{
/// <summary>
/// 自定义此特性用于接口的身份验证
/// </summary>
public class RequestAuthorizeAttribute : AuthorizeAttribute
{
/// <summary>
/// 重写基类的验证方式,加入我们自定义的Ticket验证
/// </summary>
/// <param name="actionContext"></param>
public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext)
{
//signature:(控制器+方法+jinyuanbaoapp)Md5加密
string controlName = actionContext.ActionDescriptor.ControllerDescriptor.ControllerName;
string actionName = actionContext.ActionDescriptor.ActionName;
var content = actionContext.Request.Properties["MS_HttpContext"] as HttpContextBase;
var signature = content.Request.QueryString["signature"];
if (!string.IsNullOrEmpty(signature))
{
string token = "jinyuanbaoapp";
string inspect = controlName + actionName + token;
string Md5 = MD5Encrypt.MD5(inspect);
if (signature == Md5)
{
base.IsAuthorized(actionContext);
}
else
{
HandleUnauthorizedRequest(actionContext);
}
}
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);
} } } }
using ADT.API.Infrastructure;
using ADT.Core.ApiResult;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Script.Serialization; namespace ADT.API.App_Start
{
public class IsLoginAuthorizeAttribute : AuthorizeAttribute
{
private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext)
{
//判断本地有无缓存判断用户有无登陆
var content = actionContext.Request.Properties["MS_HttpContext"] as HttpContextBase;
var token = content.Request.QueryString["token"];
if (!string.IsNullOrEmpty(token))
{
string[] ArrayToken = token.Split(new string[] { "F" }, StringSplitOptions.RemoveEmptyEntries); if(ADT.Cache.Cache.CAC.Instance.IsExCache(ArrayToken[]) && ADT.Cache.Cache.CAC.Instance.GetWCache(ArrayToken[]).Equipment == ArrayToken[])
{
HttpContext.Current.Session["UserInfo"] = ADT.Cache.Cache.CAC.Instance.GetWCache(ArrayToken[]).CuserID; //用户id
base.IsAuthorized(actionContext);
}
else {
HandleUnauthorizedRequest(actionContext);
}
}
else
{
//自定义一个json返回给客户端
HandleUnauthorizedRequest(actionContext);
} }
protected override void HandleUnauthorizedRequest(HttpActionContext filterContext)
{
base.HandleUnauthorizedRequest(filterContext);
var response = filterContext.Response = filterContext.Response ?? new HttpResponseMessage();
response.StatusCode = HttpStatusCode.OK;
MessagesCode obj = new MessagesCode(false, "该操作必须登陆", );
JavaScriptSerializer serializer = new JavaScriptSerializer();
string str = serializer.Serialize(obj);
response.Content = new StringContent(str, Encoding.UTF8, "application/json");
}
}
}

用Ping++做支付成功返回的数据类型的定义

using ADT.API.Models;
using ADT.Concrete.App;
using ADT.Core.ApiResult;
using ADT.Entities.App;
using Newtonsoft.Json.Linq;
using Swashbuckle.Swagger;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Transactions;
using System.Web;
using System.Web.Http;
using System.Web.UI;
namespace ADT.API.Controllers
{
public class WebhooksController : ApiController
{
private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
[HttpPost]
public HttpResponseMessage Index()
{
MessagesCode result = new MessagesCode(false, "无效的参数", -);
try
{ if (HttpContext.Current.Request.RequestType.ToUpper().Equals("POST"))
{
//获取 post 的 event对象
var inputData = ReadStream(HttpContext.Current.Request.InputStream);
//获取 header 中的签名
var sig = HttpContext.Current.Request.Headers.Get("x-pingplusplus-signature"); //公钥路径(请检查你的公钥 .pem 文件存放路径) var path = HttpContext.Current.Server.MapPath("/Lib/public_key.pem"); //验证签名
if (RSACryptoServiceProviderExtension.VerifySignedHash(inputData, sig, path))
{ var jObject = JObject.Parse(inputData);
var type = jObject.SelectToken("type");
var eventType = jObject.SelectToken("object");
if (eventType.ToString() == "event")//验证接收到的是否为 Event 对象。
{
if (type.ToString() == "charge.succeeded")
{ //在这里做支付成功的逻辑处理 1.订单状态改变 2.支付目的改变 3.这里要限制一下更新的次数,只有当订单是未支付时在进行下面的操作
var data = jObject.SelectToken("data");
var credentialObject = data["object"];//凭据对象
var PayNo = credentialObject["order_no"];//订单号
logger.Error("接受到支付成功的事件");
bool IsOk = false;
PayLogBean model = new PayLog().GetModelById(PayNo.ToString());
if (model != null)
{
if (model.Valid)
{
IsOk = true;
}
else
{
using (TransactionScope ts = new TransactionScope())
{
bool OneTrans = false;
bool TwoTrans = false;
OneTrans = new PayLog().UpdateValid(PayNo.ToString(), true);
UserBean userModel = new User().GetModelById((int)model.UserId);
if (userModel != null)
{
if (model.OrderType ==)
{ if (Convert.IsDBNull(userModel.Ranketime)|| userModel.RankId==)//一定不是会员
{ userModel.Rankbtime = DateTime.Now;
userModel.Ranketime = DateTime.Now.AddMonths((int)model.ChangeNum);
userModel.RankId = ;
}
else
{//代表历史上是有会员的记录的
//判断当前是否为会员
if (userModel.RankId == && userModel.Ranketime >= DateTime.Now)
{
userModel.Ranketime = userModel.Ranketime.AddMonths((int)model.ChangeNum);
}
else
{
userModel.RankId = ;
userModel.Ranketime = DateTime.Now.AddMonths((int)model.ChangeNum);
userModel.Ranketime = DateTime.Now; } }
//更新用户信息
TwoTrans = new User().UpdateMemRoleByRMB(userModel);
}
else
{ userModel.Currency = userModel.Currency + model.ChangeNum;
TwoTrans = new User().UpdateCurrency(model.UserId, userModel.Currency);
}
}
if (OneTrans&&TwoTrans)
{ IsOk = true;
ts.Complete(); } }
}
}
if (IsOk)
{
//在这里写日志
if (model.OrderType == ) //购买会员
{
//在这里加一个消费日志
//需要拿这个有效时间去获得人民币的价格是多少
List<ProductBean> productList = new Product().GetAllList().Where(p => p.ValidTime == model.ChangeNum).ToList();
if (productList.Count > )
{
CreateBase64.CreateReclog(, "人民币购买会员", Convert.ToInt64(productList[].ProdutPrice), (int)model.UserId);
} }
else
{//购买虚拟币
CreateBase64.CreateReclog(, "充虚拟币", model.ChangeNum, (int)model.UserId);
}
return Request.CreateResponse(HttpStatusCode.OK, "接受成功");
} }
} } }
} catch (Exception ex)
{
logger.Error("接受ping++的支付订单消息发生异常:" + ex);
}
return Request.CreateResponse(HttpStatusCode.InternalServerError, "接受失败"); }
private static string ReadStream(Stream stream)
{
using (var reader = new StreamReader(stream, Encoding.UTF8))
{
return reader.ReadToEnd();
}
} }
}

在写WebApi判断用户权限时返回数据和接受支付结果 定义返回数据类型的更多相关文章

  1. SharePoint 2013 JavaScript 对象判断用户权限

    场 景 近期有个场景,判断当前用户对项目有没有编辑权限,使用JavaScript完成,弄了好久才弄出来,分享一下,有需要的自行扩展吧,具体如下: 代 码 function getPermissions ...

  2. js通过生成临时表单再删除的方式向后台提交数据(模拟ajax的post提交但还要跳转页面不返回数据)以及 struts向前台返回文件下载及防止中文乱码处理

    为了避免发送数据中有特殊字符,发送时用 encodeURIComponent 编码 (其实这个 if中是直接通过浏览器下载文件的方法,else是向后台传数据的方法) struts后台Action处理接 ...

  3. android 学习随笔十五(Activity的生命周期与摧毁时返回数据 )

    1.Activity的生命周期 onCreate:创建时调用 onStart:在屏幕上可见,但是还没有获得焦点 onResume:可见并且获得焦点 onPause:可见,但是失去焦点 onStop:不 ...

  4. Python 学习 第十篇 CMDB用户权限管理

    Python 学习 第十篇 CMDB用户权限管理 2016-10-10 16:29:17 标签: python 版权声明:原创作品,谢绝转载!否则将追究法律责任. 不管是什么系统,用户权限都是至关重要 ...

  5. 项目一:第十三天 1、菜单数据管理 2、权限数据管理 3、角色数据管理 4、用户数据管理 5、在realm中动态查询用户权限,角色 6、Shiro中整合ehcache缓存权限数据

    1 课程计划 菜单数据管理 权限数据管理 角色数据管理 用户数据管理 在realm中动态查询用户权限,角色 Shiro中整合ehcache缓存权限数据         2 菜单数据添加 2.1 使用c ...

  6. Docker 记一次容器内部修改宿主机挂载目录用户权限后宿主机目录变化

    一.需求: 因公司需求,需制作mysql5.7.22 docker基础镜像,每个项目以此镜像启动一个数据库容器,并且每个项目挂载一个宿主机目录到镜像中数据存储下面用于数据持久化保存以便后期迁移至阿里云 ...

  7. Java多线程初学者指南(8):从线程返回数据的两种方法

    从线程中返回数据和向线程传递数据类似.也可以通过类成员以及回调函数来返回数据.但类成员在返回数据和传递数据时有一些区别,下面让我们来看看它们区别在哪. 一.通过类变量和方法返回数据 使用这种方法返回数 ...

  8. Transfer-Encoding:chunked 返回数据过长导致中文乱码

    最近在写一个项目的后台时,前端请求指定资源后,返回JSON格式的数据,突然发现在返回的字节数过大时,最后的message中文数据乱码了,对于同一个接口的请求:当数据小时不会乱码,当数据量大了中文就乱码 ...

  9. 快捷标签和ajax、json返回数据

    <if 判断条件>标签</if><import>标签可以链接外部的样式表,和js<import file="js.util.Array" ...

随机推荐

  1. 小程序——如何引入外部js

    当写小程序需要引入一些额外的js文件时,可以这样: 一.先把外部js用一个函数封闭起来: test.js function myfunc() { console.log("myfunc... ...

  2. 【消息中间件之RabbitMQ学习】-开篇-001

    写在前面的话 项目中因为要用到消息中间件,当初极力推荐RabbitMq.但因为种种原因,最终选型为java+mongodb自实现一套分布式的消.没有用RabbitMq工作过,实属遗憾.因为个人来说实在 ...

  3. sync.Pool的使用

    一定要搞明白sync.Pool的正确用法,避免出现以下问题: kline := this.pool.Get() defer this.pool.Put(kline) kline.UnMarshal(d ...

  4. Day8 linux软件包管理

    软件包的两种形式      qq.tar.gz  (需要编译 源码翻译成二进制)/ rpm (直接安装) rpm的文件名分为5部分 name名称  version版本编号  release发布次数  ...

  5. shell脚本-2

    http://www.runoob.com/linux/linux-shell-variable.html 字符串可以用单引号,也可以用双引号,也可以不用引号.单双引号的区别跟PHP类似. 单引号字符 ...

  6. 多线程——C++

    线程: 先说进程,进程是应用程序的执行实例,每个进程拥有其私有的虚拟地址空间.代码.数据和其它系统资源组成.进程在运行时创建的资源随着进程的终止而死亡. 而线程是一个独立的执行流,是进程内部的一个独立 ...

  7. QT5.10+VS2013 TCP 一对一简单C/S架构通信

    ---恢复内容开始--- QT~俺老孙又回来啦~ 买的那本书上面关于tcp的内容就七八页,而且都是过于简单的东西,想进一步就要度娘很久很麻烦,还是喜欢看书(嘿嘿嘿~) 大致的思路就是两个项目,一个cl ...

  8. vue-----表单与组件

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

  9. redis key命令

    key命令主要用于管理redis中的key del key //删除key, 不存在的key会忽略 dump key //序列化key,不存在的key返回nil exists key //判断key是 ...

  10. java_oop_接口

    接口    难的是在系统设计里怎么样使用接口,主要在语法,不在系统架构与设计    概念    声明    语法        只有抽象方法的抽象类?可以用接口来表示,用接口来代替这样的抽象类,是因为 ...