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.

TeknikAuthorizeAttribute.cs 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using Teknik.Areas.Error.Controllers;
  6. using Teknik.Utilities;
  7. using Teknik.Areas.Users.Controllers;
  8. using Teknik.Models;
  9. using Teknik.Areas.Users.Utility;
  10. using Teknik.Areas.Users.Models;
  11. using Teknik.Configuration;
  12. using Microsoft.AspNetCore.Authorization;
  13. using Microsoft.AspNetCore.Mvc;
  14. using Microsoft.AspNetCore.Mvc.Filters;
  15. using Microsoft.Extensions.Logging;
  16. using Teknik.Logging;
  17. using Teknik.Data;
  18. using Teknik.Security;
  19. namespace Teknik.Attributes
  20. {
  21. public enum AuthType
  22. {
  23. Basic,
  24. Forms
  25. }
  26. [AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
  27. public class TeknikAuthorizeAttribute : AuthorizeAttribute, IAuthorizationFilter
  28. {
  29. private AuthType m_AuthType { get; set; }
  30. public TeknikAuthorizeAttribute() : this(AuthType.Forms)
  31. {
  32. }
  33. public TeknikAuthorizeAttribute(AuthType authType)
  34. {
  35. m_AuthType = authType;
  36. }
  37. public void OnAuthorization(AuthorizationFilterContext context)
  38. {
  39. var user = context.HttpContext.User;
  40. if (!user.Identity.IsAuthenticated)
  41. {
  42. //if (m_AuthType == AuthType.Forms)
  43. //{
  44. // var logger = (ILogger<Logger>)context.HttpContext.RequestServices.GetService(typeof(ILogger<Logger>));
  45. // var config = (Config)context.HttpContext.RequestServices.GetService(typeof(Config));
  46. // var dbContext = (TeknikEntities)context.HttpContext.RequestServices.GetService(typeof(TeknikEntities));
  47. // var logoutSession = (LogoutSessionManager)context.HttpContext.RequestServices.GetService(typeof(LogoutSessionManager));
  48. // var userController = new UserController(logger, config, dbContext, logoutSession);
  49. // if (userController != null)
  50. // {
  51. // // auth failed, redirect to login page
  52. // var request = context.HttpContext.Request;
  53. // string redirectUrl = (request.Host != null && request.Path != null) ? request.Host + request.Path : string.Empty;
  54. // context.Result = userController.Login(redirectUrl);
  55. // return;
  56. // }
  57. //}
  58. }
  59. }
  60. }
  61. }