ASP.NET MVC-在从服务器的ActionResult方法回调中,会话变量为null

时间:2018-09-25 15:53:25

标签: asp.net asp.net-mvc asp.net-mvc-4 controller session-variables

我正在我的应用程序中实现CoinPayments IPN,但我无法将数据传递给进行回调的方法。我已经尝试过所有可以找到的东西:TempData,SessionVariables,尝试以某种形式在表单中实现它,甚至可以请求它,但是这对我没有用。因此,我也尝试使用全局静态变量来实现它。而且有效!但是随之而来的另一个问题是:如果多个用户同时(甚至在两次回调之间)从网站上购买商品,他们的数据将会混合在一起。所以我在这里,再次尝试使会话变量起作用,但是不知道为什么它们没有像我以前使用的那样起作用。我大概能想到的是因为它是CoinPayments的回调,而我处理错误。

这是我现在拥有的代码:我尝试了各种变体,例如在Get Payment方法中实现Session。现在我结束了它,它在POST METHOD中仍然为空。

用于处理会话变量的类:

 public static class MyGlobalVariables
    {
        public static int TokenChoice
        {
            get
            {
                if (System.Web.HttpContext.Current.Session["TokenChoice"] == null)
                {
                    return -1;
                }
                else
                {
                    return (int)System.Web.HttpContext.Current.Session["TokenChoice"];
                }
            }
            set
            {
                System.Web.HttpContext.Current.Session["TokenChoice"] = value;
            }
        }
        public static int PaymentChoice
        {
            get
            {
                if (System.Web.HttpContext.Current.Session["PaymentChoice"] == null)
                {
                    return -1;
                }
                else
                {
                    return (int)System.Web.HttpContext.Current.Session["PaymentChoice"];
                }
            }
            set
            {
                System.Web.HttpContext.Current.Session["PaymentChoice"] = value;
            }
        }
        public static string CurrentUser
        {
            get
            {
                System.Web.HttpContext.Current.Session["CurrentUser"] = System.Web.HttpContext.Current.User.Identity.Name;
                return (string)System.Web.HttpContext.Current.Session["CurrentUser"];
            }
        }

    }

返回视图的类,您在其中单击CoinPayments按钮:

 public ActionResult Payment(int tokenChoice, int paymentChoice)
    {

        ViewBag.Payment = paymentChoice;
        MyGlobalVariables.PaymentChoice = paymentChoice;
        MyGlobalVariables.TokenChoice = tokenChoice;

        return View();
    }

用于处理来自CoinPayments的回调的回调类:

  [HttpPost]
    public ActionResult Payment()
    {
        NameValueCollection nvc = Request.Form;


        var merchant_id = id;
            var ipn_secret = secret;
        var order_total = MyGlobalVariables.PaymentChoice;

            if (String.IsNullOrEmpty(nvc["ipn_mode"]) || nvc["ipn_mode"] != "hmac")
            {
                Trace.WriteLine("IPN Mode is not HMAC");
                return View();
            }

            if (String.IsNullOrEmpty(HTTP_HMAC))
            {
                Trace.WriteLine("No HMAC signature sent");
                return View();
            }

            if (String.IsNullOrEmpty(nvc["merchant"]) || nvc["merchant"] != merchant_id.Trim())
            {
                Trace.WriteLine("No or incorrect Merchant ID passed");
                return View();
            }

            //var hmac = hash_hmac("sha512", request, ipn_secret.Trim());

            var txn_id = nvc["txn_id"]; 
            var item_name = nvc["item_name"]; 
            var item_number = nvc["item_number"];
            var amount1 = nvc["amount1"];
            var amount2 = float.Parse(nvc["amount2"], CultureInfo.InvariantCulture.NumberFormat);
            var currency1 = nvc["currency1"];
            var currency2 = nvc["currency2"];
            var status = Convert.ToInt32(nvc["status"]);
            var status_text = nvc["status_text"];
            Trace.WriteLine(status);
            if (currency1 != "USD") {
                Trace.WriteLine("Original currency mismatch!");
                return View();
            }

        if (Convert.ToInt32(amount1) < Convert.ToInt32(order_total))
        {
            Trace.WriteLine("Amount is less than order total!");
            return View();
        }

        if (status >= 100 || status == 2) {

                using (MyDatabaseEntities1 dc = new MyDatabaseEntities1())
                {
                var account = dc.Users.Where(a => a.Username == MyGlobalVariables.CurrentUser).FirstOrDefault();
                if (account != null && account.Paid == 0)
                {
                    Trace.WriteLine("Payment Completed");
                    Trace.WriteLine("Tokens to add: " + MyGlobalVariables.TokenChoice);
                    account.Tokens += MyGlobalVariables.TokenChoice;
                    account.Paid = 1;
                    dc.Configuration.ValidateOnSaveEnabled = false;
                    dc.SaveChanges();
                }
                }
              } else if (status < 0)
            {
                Trace.WriteLine(
                    "payment error, this is usually final but payments will sometimes be reopened if there was no exchange rate conversion or with seller consent");
            } else {
            using (MyDatabaseEntities1 dc = new MyDatabaseEntities1())
            {
                var account = dc.Users.Where(a => a.Username == MyGlobalVariables.CurrentUser).FirstOrDefault();
                if (account != null)
                {
                    account.Paid = 0;
                    dc.Configuration.ValidateOnSaveEnabled = false;
                    dc.SaveChanges();
                }
            }
            Trace.WriteLine("Payment is pending");
            }

        return View();
    }

如您所见,我只需要处理3个变量。 也有人可能会问为什么我对Current.User使用会话变量? 出于某种原因,Callback方法无法读取Current.User,因为它返回null。好吧……到目前为止,真的没有什么改变。

如果您曾经经历过类似的事情或可以找到问题,我将非常感激,因为我已经在该问题上浪费了超过2天的时间。

编辑: 经过一些测试,我发现如果我自己运行Post方法,变量可以正常工作。因此,问题在于处理来自CoinPayments的回调。有没有解决这个问题的具体方法?

0 个答案:

没有答案