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 4.0KB

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