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.

Global.asax.cs 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using System.Web.Optimization;
  7. using System.Web.Routing;
  8. using Teknik.Models;
  9. using System.Data.Entity;
  10. using System.Web.Security;
  11. using Teknik.Migrations;
  12. using System.Data.Entity.Migrations;
  13. using Teknik.Areas.Profile.Models;
  14. namespace Teknik
  15. {
  16. public class MvcApplication : System.Web.HttpApplication
  17. {
  18. protected void Application_Start()
  19. {
  20. Database.SetInitializer(new MigrateDatabaseToLatestVersion<TeknikEntities, Configuration>());
  21. AreaRegistration.RegisterAllAreas();
  22. FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
  23. RouteConfig.RegisterRoutes(RouteTable.Routes);
  24. BundleConfig.RegisterBundles(BundleTable.Bundles);
  25. }
  26. protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
  27. {
  28. if (FormsAuthentication.CookiesSupported == true)
  29. {
  30. if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
  31. {
  32. //let us take out the username now
  33. string username = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
  34. List<string> roles = new List<string>();
  35. using (TeknikEntities entities = new TeknikEntities())
  36. {
  37. User user = entities.Users.Include("Groups").Include("Groups.Roles").SingleOrDefault(u => u.Username == username);
  38. foreach (Group grp in user.Groups)
  39. {
  40. foreach (Role role in grp.Roles)
  41. {
  42. if (!roles.Contains(role.Name))
  43. {
  44. roles.Add(role.Name);
  45. }
  46. }
  47. }
  48. }
  49. //Let us set the Pricipal with our user specific details
  50. HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(
  51. new System.Security.Principal.GenericIdentity(username, "Forms"), roles.ToArray());
  52. }
  53. }
  54. }
  55. }
  56. }