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.

UserHelper.cs 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Web;
  7. using System.Web.Security;
  8. using Teknik.Areas.Profile.Models;
  9. using Teknik.Configuration;
  10. using Teknik.Models;
  11. namespace Teknik.Areas.Profile.Utility
  12. {
  13. public static class UserHelper
  14. {
  15. public static User GetUser(string username)
  16. {
  17. TeknikEntities db = new TeknikEntities();
  18. User user = db.Users.Where(b => b.Username == username).FirstOrDefault();
  19. return user;
  20. }
  21. public static bool UserExists(string username)
  22. {
  23. User user = GetUser(username);
  24. if (user != null)
  25. {
  26. return true;
  27. }
  28. return false;
  29. }
  30. public static HttpCookie CreateAuthCookie(string username, bool remember, string domain, bool local)
  31. {
  32. Config config = Config.Load();
  33. HttpCookie authcookie = FormsAuthentication.GetAuthCookie(username, remember);
  34. authcookie.Name = "TeknikAuth";
  35. authcookie.HttpOnly = true;
  36. authcookie.Secure = true;
  37. // Set domain dependent on where it's being ran from
  38. if (local) // localhost
  39. {
  40. authcookie.Domain = null;
  41. }
  42. else if (config.DevEnvironment) // dev.example.com
  43. {
  44. authcookie.Domain = string.Format("dev.{0}", domain);
  45. }
  46. else // A production instance
  47. {
  48. authcookie.Domain = string.Format(".{0}", domain);
  49. }
  50. return authcookie;
  51. }
  52. }
  53. }