The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultController.cs 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Web;
  3. using System.Web.Mvc;
  4. using System.Web.Routing;
  5. using Teknik.Areas.Error.Controllers;
  6. using Teknik.Configuration;
  7. using Teknik.Filters;
  8. using Teknik.Security;
  9. using Teknik.Utilities;
  10. namespace Teknik.Controllers
  11. {
  12. [CORSActionFilter]
  13. public class DefaultController : Controller
  14. {
  15. private Config _config;
  16. protected string Subdomain
  17. {
  18. get { return (string)Request.RequestContext.RouteData.Values["sub"]; }
  19. }
  20. protected Config Config
  21. {
  22. get
  23. {
  24. if (_config == null)
  25. {
  26. _config = Config.Load();
  27. }
  28. return _config;
  29. }
  30. }
  31. protected virtual new TeknikPrincipal User
  32. {
  33. get { return HttpContext.User as TeknikPrincipal; }
  34. }
  35. public object ObjectFactory { get; private set; }
  36. public DefaultController()
  37. {
  38. ViewBag.Title = Config.Title;
  39. ViewBag.Description = Config.Description;
  40. if (Response != null)
  41. {
  42. Response.SuppressFormsAuthenticationRedirect = true;
  43. }
  44. }
  45. protected override void HandleUnknownAction(string actionName)
  46. {
  47. this.InvokeHttp404(HttpContext);
  48. }
  49. [HttpGet]
  50. [AllowAnonymous]
  51. public ActionResult InvokeHttp404(HttpContextBase httpContext)
  52. {
  53. IController errorController = new ErrorController();
  54. var errorRoute = new RouteData();
  55. errorRoute.DataTokens.Add("namespaces", new[] { typeof(ErrorController).Namespace });
  56. errorRoute.DataTokens.Add("area", "Error");
  57. errorRoute.Values.Add("controller", "Error");
  58. errorRoute.Values.Add("action", "Http404");
  59. errorRoute.Values.Add("exception", null);
  60. errorController.Execute(new RequestContext(
  61. httpContext, errorRoute));
  62. return new EmptyResult();
  63. }
  64. // Get the Favicon
  65. [HttpGet]
  66. [AllowAnonymous]
  67. public ActionResult Favicon()
  68. {
  69. // Get favicon
  70. string imageFile = Server.MapPath(Constants.FAVICON_PATH);
  71. return File(imageFile, "image/x-icon");
  72. }
  73. // Get the Logo
  74. [HttpGet]
  75. [AllowAnonymous]
  76. public ActionResult Logo()
  77. {
  78. // Get favicon
  79. string imageFile = Server.MapPath(Constants.LOGO_PATH);
  80. return File(imageFile, "image/svg+xml");
  81. }
  82. // Get the Robots.txt
  83. [HttpGet]
  84. [AllowAnonymous]
  85. public ActionResult Robots()
  86. {
  87. // Get favicon
  88. string file = Server.MapPath(Constants.ROBOTS_PATH);
  89. return File(file, "text/plain");
  90. }
  91. [HttpGet]
  92. [AllowAnonymous]
  93. public ActionResult NotFound()
  94. {
  95. return InvokeHttp404(HttpContext);
  96. }
  97. protected ActionResult GenerateActionResult(object json)
  98. {
  99. return GenerateActionResult(json, View());
  100. }
  101. protected ActionResult GenerateActionResult(object json, ActionResult result)
  102. {
  103. if (Request.IsAjaxRequest())
  104. {
  105. return Json(json);
  106. }
  107. return result;
  108. }
  109. }
  110. [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
  111. public class MyAuthorizeAttribute : AuthorizeAttribute
  112. {
  113. protected override bool AuthorizeCore(HttpContextBase httpContext)
  114. {
  115. if (httpContext == null)
  116. throw new ArgumentNullException("httpContext");
  117. var user = httpContext.User;
  118. if (!user.Identity.IsAuthenticated)
  119. return false;
  120. return true;
  121. }
  122. }
  123. }