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.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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(TeknikEntities db, string username)
  16. {
  17. User user = db.Users.Where(b => b.Username == username).FirstOrDefault();
  18. return user;
  19. }
  20. public static bool UserExists(TeknikEntities db, string username)
  21. {
  22. User user = GetUser(db, username);
  23. if (user != null)
  24. {
  25. return true;
  26. }
  27. return false;
  28. }
  29. public static HttpCookie CreateAuthCookie(string username, bool remember, string domain, bool local)
  30. {
  31. Config config = Config.Load();
  32. HttpCookie authcookie = FormsAuthentication.GetAuthCookie(username, remember);
  33. authcookie.Name = "TeknikAuth";
  34. authcookie.HttpOnly = true;
  35. authcookie.Secure = true;
  36. // Set domain dependent on where it's being ran from
  37. if (local) // localhost
  38. {
  39. authcookie.Domain = null;
  40. }
  41. else if (config.DevEnvironment) // dev.example.com
  42. {
  43. authcookie.Domain = string.Format("dev.{0}", domain);
  44. }
  45. else // A production instance
  46. {
  47. authcookie.Domain = string.Format(".{0}", domain);
  48. }
  49. return authcookie;
  50. }
  51. }
  52. }