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. mysql数据库的增量备份和全备

    还有一种简单的方法 参考 https://blog.csdn.net/u010098331/article/details/50932064 (注意:5.6版本以上新加了gtid 功能,gtid开启之 ...

  2. Altium Designer 10 执行DRC发现有 Length Constraint 解决办法

    在PCB布局连线结束后,执行DRC,结果Length Constraint 报错,如图: 在Design Rules中找对应的规则约束,怎么也找不见. 其实是,在差分对进行等长蛇形绕线的步骤中,约束了 ...

  3. paddlepaddle

    1. 训练过程中cost出现nan 可能是因为有脏数据,寻找脏数据的方法就是,设置batch_size=1, paddle.reader.shuffle 中buf_size=1 一条一条的进行训练,看 ...

  4. windows 通过scoop安装yarn

    首先进入cmd,输入powershell指令,如图 Prompt should now start with "PS " 然后run iex (new-object net.web ...

  5. git 入门教程

    git 入门教程之协同开发 前面我们已经介绍过远程仓库的相关概念,不过那时并没有深入探讨,只是讲解了如何创建远程仓库以及推送最新工作成果到远程仓库,实际上远程仓库对于团队协同开发很重要,不仅仅是团队协 ...

  6. Git创建本地仓库、与远程仓库关联

    不知道对不对,不过我这么干能用了嘿嘿 下载好git以及配置密钥什么的就不说了,网上一p眼子 在本地找个变成仓库的文件夹,打开git命令行工具cd到这个目录,然后git init创建本地仓库 然后上gi ...

  7. 用turtle实现动态汉诺塔

    代码如下: (此代码最多可支持七层) import turtle class Stack: def __init__(self): self.items = [] def isEmpty(self): ...

  8. Jrebel 配置

    先下载插件 http://139.199.89.239:1008/88414687-3b91-4286-89ba-2dc813b107ce http://jrebel.autoseasy.cn/xix ...

  9. vue简单实例代码

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  10. ifconfig 网卡 下面的参数

    ifconfig  eth1 eth1 Link encap:Ethernet HWaddr 20:12:07:04:05:00 inet addr:172.16.77.174 Bcast:172.1 ...