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.

100 lines
3.7 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. using Common.Helper;
  2. using Microsoft.AspNet.Identity;
  3. using System;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. namespace JMEAgentSystem
  9. {
  10. public partial class SiteMaster : MasterPage
  11. {
  12. private const string AntiXsrfTokenKey = "__AntiXsrfToken";
  13. private const string AntiXsrfUserNameKey = "__AntiXsrfUserName";
  14. private string _antiXsrfTokenValue;
  15. protected void Page_Init(object sender, EventArgs e)
  16. {
  17. IsLogin();
  18. // The code below helps to protect against XSRF attacks
  19. var requestCookie = Request.Cookies[AntiXsrfTokenKey];
  20. Guid requestCookieGuidValue;
  21. if (requestCookie != null && Guid.TryParse(requestCookie.Value, out requestCookieGuidValue))
  22. {
  23. // Use the Anti-XSRF token from the cookie
  24. _antiXsrfTokenValue = requestCookie.Value;
  25. Page.ViewStateUserKey = _antiXsrfTokenValue;
  26. }
  27. else
  28. {
  29. // Generate a new Anti-XSRF token and save to the cookie
  30. _antiXsrfTokenValue = Guid.NewGuid().ToString("N");
  31. Page.ViewStateUserKey = _antiXsrfTokenValue;
  32. var responseCookie = new HttpCookie(AntiXsrfTokenKey)
  33. {
  34. HttpOnly = true,
  35. Value = _antiXsrfTokenValue
  36. };
  37. if (FormsAuthentication.RequireSSL && Request.IsSecureConnection)
  38. {
  39. responseCookie.Secure = true;
  40. }
  41. Response.Cookies.Set(responseCookie);
  42. }
  43. Page.PreLoad += master_Page_PreLoad;
  44. }
  45. protected void master_Page_PreLoad(object sender, EventArgs e)
  46. {
  47. if (!IsPostBack)
  48. {
  49. // Set Anti-XSRF token
  50. ViewState[AntiXsrfTokenKey] = Page.ViewStateUserKey;
  51. ViewState[AntiXsrfUserNameKey] = Context.User.Identity.Name ?? String.Empty;
  52. }
  53. else
  54. {
  55. // Validate the Anti-XSRF token
  56. if ((string)ViewState[AntiXsrfTokenKey] != _antiXsrfTokenValue
  57. || (string)ViewState[AntiXsrfUserNameKey] != (Context.User.Identity.Name ?? String.Empty))
  58. {
  59. throw new InvalidOperationException("Validation of Anti-XSRF token failed.");
  60. }
  61. }
  62. }
  63. protected void Page_Load(object sender, EventArgs e)
  64. {
  65. }
  66. protected void Unnamed_LoggingOut(object sender, LoginCancelEventArgs e)
  67. {
  68. Context.GetOwinContext().Authentication.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
  69. }
  70. private void IsLogin()
  71. {
  72. if (GetStatic.GetUser() != null && GetStatic.GetUser() == "")
  73. Response.Redirect("/Webpages/Account/Login");
  74. agentName1.InnerText = GetStatic.ReadSession("agentName", "");
  75. agentName2.InnerText = GetStatic.ReadSession("agentName", "");
  76. //username1.InnerText = user.Username;
  77. branchName1.InnerText = GetStatic.ReadSession("agentAddress", "");
  78. var pg = this.Page;
  79. string a = Page.Page.AppRelativeVirtualPath;
  80. if (a.ToLower().Equals("~/webpages/account/forcechangepassword.aspx") || a.ToLower().Equals("~/webpages/account/forcechangepassword"))
  81. {
  82. return;
  83. }
  84. if (GetStatic.ReadSession("isForceChangePassword", "") == "True")
  85. Response.Redirect("/Webpages/Account/ForceChangePassword");
  86. }
  87. }
  88. }