BaseController.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Linq;
  5. using System.Net.Http;
  6. using System.Web;
  7. using System.Web.Mvc;
  8. namespace NEducation.Controllers
  9. {
  10. public class BaseController : Controller
  11. {
  12. // GET: Base
  13. // GET: Base
  14. public class ReCaptcha
  15. {
  16. public bool Success { get; set; }
  17. public List<string> ErrorCodes { get; set; }
  18. public static bool Validate(string encodedResponse)
  19. {
  20. if (string.IsNullOrEmpty(encodedResponse)) return false;
  21. var client = new System.Net.WebClient();
  22. var secret = ConfigurationManager.AppSettings[UtilsController.WsType.GoogleCaptcha];
  23. if (string.IsNullOrEmpty(secret)) return false;
  24. var googleReply = client.DownloadString(string.Format("https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}", secret, encodedResponse));
  25. var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
  26. var reCaptcha = serializer.Deserialize<ReCaptcha>(googleReply);
  27. return reCaptcha.Success;
  28. }
  29. }
  30. public static string CheckAutoLogin(log4net.ILog log, String uuid)
  31. {
  32. string res = null;
  33. try
  34. {
  35. HttpClient httpClient = new HttpClient();
  36. MultipartFormDataContent form = new MultipartFormDataContent();
  37. form.Add(new StringContent(uuid), "uuid");
  38. form.Add(new StringContent("mocha"), "username");
  39. form.Add(new StringContent("mocha@%23$2014"), "password");
  40. using (var client = new HttpClient())
  41. {
  42. var response = client.PostAsync(UtilsController.WsType.URL_GET_MSISDN, form).Result;
  43. log.Debug("url: " + UtilsController.WsType.URL_GET_MSISDN);
  44. if (response.IsSuccessStatusCode)
  45. {
  46. var responseContent = response.Content;
  47. // by calling .Result you are synchronously reading the result
  48. string responseString = responseContent.ReadAsStringAsync().Result;
  49. log.Debug("Response: " + responseString);
  50. return responseString;
  51. }
  52. else
  53. {
  54. log.Debug("Response: " + response.StatusCode.ToString());
  55. return response.StatusCode.ToString();
  56. }
  57. }
  58. }
  59. catch (Exception ex)
  60. {
  61. log.Debug("Exp: " + ex);
  62. }
  63. return res;
  64. }
  65. protected bool CheckAuthToken()
  66. {
  67. if (Session["AuthToken"] != null && Request.Cookies["AuthToken"] != null)
  68. {
  69. if (!Session["AuthToken"].ToString().Equals(Request.Cookies["AuthToken"].Value))
  70. {
  71. return false;
  72. }
  73. else
  74. {
  75. return true;
  76. }
  77. }
  78. else
  79. {
  80. return false;
  81. }
  82. }
  83. }
  84. }