protected void btnSearch_Click(object sender, EventArgs e) { string searchText = txtSearch.Text.Trim(); txtSearch.Text = ""; if (string.IsNullOrWhiteSpace(searchText)) { return; } Session["lastSearch"] = searchText; txtSearch.Attributes.Add("placeholder", searchText); if (Properties.Settings.Default.EnableStatistic) { string userId = Context.User.Identity.Name; using (var _db = new Kronika106DBContext()) { ApplicationUser user = _db.Users.First(u => u.UserName == userId); if (user != null) { _db.StatisticsSearch.Add(new StatisticsSearch() { ApplicationUser = user, CreatedUTC = DateTime.UtcNow, SearchPattern = searchText }); _db.SaveChanges(); } } } // lgSearch.SearchAll(searchText); Response.Redirect(Server.UrlPathEncode(string.Format("~/SearchResult.aspx?search={0}", searchText))); }
internal void AddUsersToAdminRole() { // Access the application context and create result variables. Models.Kronika106DBContext context = new Kronika106DBContext(); IdentityResult IdRoleResult; IdentityResult IdUserResult; // Create a RoleStore object by using the ApplicationDbContext object. // The RoleStore is only allowed to contain IdentityRole objects. var roleStore = new RoleStore <IdentityRole>(context); // Create a RoleManager object that is only allowed to contain IdentityRole objects. // When creating the RoleManager object, you pass in (as a parameter) a new RoleStore object. var roleMgr = new RoleManager <IdentityRole>(roleStore); // Then, you create the "canEdit" role if it doesn't already exist. if (!roleMgr.RoleExists(GlobalConstants.RoleAdmin)) { IdRoleResult = roleMgr.Create(new IdentityRole { Name = GlobalConstants.RoleAdmin }); } // Create a UserManager object based on the UserStore object and the ApplicationDbContext // object. Note that you can create new objects and use them as parameters in // a single line of code, rather than using multiple lines of code, as you did // for the RoleManager object. var userMgr = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context)); if (!string.IsNullOrEmpty(Properties.Settings.Default.AdminEmails)) { string[] adminMails = Properties.Settings.Default.AdminEmails.Split(';'); if (adminMails.Length > 0) { for (int i = 0; i < adminMails.Length; i++) { var user = userMgr.FindByEmail(adminMails[i]); if (user != null) { if (!userMgr.IsInRole(user.Id, GlobalConstants.RoleAdmin)) { IdUserResult = userMgr.AddToRole(user.Id, GlobalConstants.RoleAdmin); } } } context.SaveChanges(); } } }
protected void Unnamed_LoggingOut(object sender, LoginCancelEventArgs e) { adminMenu.Visible = false; if (Properties.Settings.Default.EnableStatistic) { using (var _db = new Kronika106DBContext()) { string userId = Context.User.Identity.Name; ApplicationUser aUser = _db.Users.FirstOrDefault(u => u.UserName == userId); if (aUser != null) { aUser.LastLogOffUTC = DateTime.UtcNow; _db.SaveChanges(); } } } LogOutAll(); }
protected void SetPassword_Click(object sender, EventArgs e) { if (IsValid) { // Create the local login info and link the local account to the user var manager = Context.GetOwinContext().GetUserManager <ApplicationUserManager>(); var _db = new Kronika106DBContext(); string userId = Context.User.Identity.Name; ApplicationUser user = _db.Users.First(u => u.UserName == userId); IdentityResult result = manager.AddPassword(user.Id, password.Text); if (result.Succeeded) { user.EmailConfirmed = true; _db.SaveChanges(); Response.Redirect("~/Account/Manage?m=SetPwdSuccess"); } else { AddErrors(result); } } }
protected void LogIn(object sender, EventArgs e) { if (IsValid) { // Validate the user password var manager = Context.GetOwinContext().GetUserManager <ApplicationUserManager>(); var signinManager = Context.GetOwinContext().GetUserManager <ApplicationSignInManager>(); // This doen't count login failures towards account lockout // To enable password failures to trigger lockout, change to shouldLockout: true //find user by eMail var user = manager.FindByEmail(Email.Text); if (user != null) { if (!user.EmailConfirmed) { FailureText.Text = "Neuspešný pokus o prihlásenie, najskôr musíte potvrdiť email."; ErrorMessage.Visible = true; ResendConfirm.Visible = true; } else { var result = signinManager.PasswordSignIn(user.UserName, Password.Text, RememberMe.Checked, shouldLockout: false); switch (result) { case SignInStatus.Success: //FormsAuthentication.SetAuthCookie(user.UserName, RememberMe.Checked); //Session[GlobalConstants.UserNick] = !string.IsNullOrEmpty(user.ScoutNickName) ? user.ScoutNickName : user.FirstName; if (Properties.Settings.Default.EnableStatistic) { using (var _db = new Kronika106DBContext()) { ApplicationUser aUser = _db.Users.FirstOrDefault(u => u.UserName == user.UserName); if (aUser != null) { aUser.LastLogInUTC = DateTime.UtcNow; if (aUser.LoginCount.HasValue) { aUser.LoginCount++; } else { aUser.LoginCount = 1; } _db.SaveChanges(); } } } string returnUrl = Request.QueryString["ReturnUrl"]; if (string.IsNullOrEmpty(returnUrl)) { returnUrl = (string)Session[GlobalConstants.RedirectURLKey]; } IdentityHelper.RedirectToReturnUrl(returnUrl, Response); break; case SignInStatus.LockedOut: Response.Redirect("/Account/Lockout"); break; case SignInStatus.RequiresVerification: Response.Redirect(String.Format("/Account/TwoFactorAuthenticationSignIn?ReturnUrl={0}&RememberMe={1}", Request.QueryString["ReturnUrl"], RememberMe.Checked), true); break; case SignInStatus.Failure: default: FailureText.Text = "Chybný pokus pre prihlásenie"; ErrorMessage.Visible = true; break; } } } else { FailureText.Text = string.Format("Užívateľ neexistuje, prosím zeregistrujete sa"); ErrorMessage.Visible = true; } } }
// public void ScrollTo(string controllCLientId) // { // this.RegisterClientScriptBlock("ScrollTo", string.Format(@" // <script type='text/javascript'> // $(document).ready(function() {{ // var element = document.getElementById('{0}'); // element.scrollIntoView(); // element.focus(); // }}); // </script> //", controllCLientId)); // } ////TimerRefreshForum.Interval = Properties.Settings.Default.ForumAutoRefrestInterval; ////TimerRefreshForum.Enabled = true; protected void Page_Init(object sender, EventArgs e) { if (!IsPostBack) { string pageName = string.Empty; try { //zruisenie cache Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache); Response.Cache.SetNoStore(); //kontrola ci bol poslany request param Session[GlobalConstants.RedirectURLKey] = HttpContext.Current.Request.Url.PathAndQuery; if (Request.QueryString.Count == 0 || string.IsNullOrEmpty(QueryStringHelper.GetIdFromRequest(Request))) { Response.Redirect(GlobalConstants.urlDefault, true); return; } //kontrola ci je user prihlaseny if (!Context.User.Identity.IsAuthenticated) { Response.Redirect(GlobalConstants.urlForbidden, true); return; } //nacitanie filesystem query string trueQuery = QueryStringHelper.GetIdFromRequest(Request); //kontrola ci existuje filesystem struktura podla query RelativePath = string.Format("{0}/{1}", GlobalConstants.PthFileSystemRoot, trueQuery); FileSystemPath = Server.MapPath(RelativePath); if (!System.IO.Directory.Exists(FileSystemPath)) { Response.Redirect(GlobalConstants.urlDefault, true); return; } EventIdParams = trueQuery.Split(GlobalConstants.EventIdSeparator, StringSplitOptions.RemoveEmptyEntries); //pre akcia, akcia popis if (EventIdParams != null && EventIdParams.Length == NumberOfParams) { StrPageHeader = EventIdParams[1]; PageTitleBase = string.Format("{0} - {1}", EventIdParams[0], EventIdParams[1]); } else { Response.Redirect(GlobalConstants.urlDefault, true); return; } Navigator.GenerateNavigation(Page.Master); if (Properties.Settings.Default.EnableStatistic) { string userId = Context.User.Identity.Name; using (var _db = new Kronika106DBContext()) { ApplicationUser user = _db.Users.First(u => u.UserName == userId); if (user != null) { _db.StatisticBrowse.Add(new StatisticBrowse() { ApplicationUser = user, CreatedUTC = DateTime.UtcNow, Url = Server.UrlDecode(HttpContext.Current.Request.Url.PathAndQuery) }); _db.SaveChanges(); } } } } catch (ThreadAbortException) { } } }