/// <summary> /// Create a reset password token for the user in order to allow him to change his password if he lost it. /// </summary> /// <param name="userId"> /// The user id /// </param> /// <returns> /// The token created. /// </returns> /// <remarks> /// </remarks> public override Guid CreateResetPasswordToken(Guid userId) { var newTokenId = Guid.NewGuid(); using (var entities = new AppleseedMembershipEntities(ConfigurationManager.ConnectionStrings["AppleseedMembershipEntities"].ConnectionString)) { var newToken = new aspnet_ResetPasswordTokens { TokenId = newTokenId, UserId = userId, CreationDate = DateTime.UtcNow }; entities.aspnet_ResetPasswordTokens.AddObject(newToken); entities.SaveChanges(); } return newTokenId; }
/// <summary> /// Checks if the users has that token associated. /// </summary> /// <param name="userId"> /// The user id /// </param> /// <param name="tokenId"> /// The token /// </param> /// <returns> /// True if the user has the token specified or false otherwise /// </returns> /// <remarks> /// </remarks> public override bool VerifyTokenForUser(Guid userId, Guid tokenId) { using (var entities = new AppleseedMembershipEntities(ConfigurationManager.ConnectionStrings["AppleseedMembershipEntities"].ConnectionString)) { var maxDays = 7; try{ maxDays = int.Parse(ConfigurationManager.AppSettings["MaxTokenDays"]); } catch(Exception){ maxDays = 7; } try { var token = entities.aspnet_ResetPasswordTokens.Include("aspnet_Membership").Single(t => t.UserId == userId && t.TokenId == tokenId && t.aspnet_Membership.aspnet_Applications.ApplicationName.ToLower() == this.ApplicationName.ToLower()); if (token.CreationDate >= DateTime.Now.AddDays(-maxDays)) { return true; } else { // The token is old entities.aspnet_ResetPasswordTokens.DeleteObject(token); entities.SaveChanges(); return false; } } catch (Exception e) { ErrorHandler.Publish(LogLevel.Error, e); return false; } } }
/// <summary> /// Changes the user password. /// </summary> /// <param name="username"> /// The user username /// </param> /// <param name="tokenId"> /// The token. /// </param> /// <param name="newPassword"> /// The new password the user wants /// </param> /// <returns> /// True if the password is changed, false otherwise /// </returns> /// <remarks> /// </remarks> public override bool ChangePassword(string username, Guid tokenId, string newPassword) { using (var entities = new AppleseedMembershipEntities(ConfigurationManager.ConnectionStrings["AppleseedMembershipEntities"].ConnectionString)) { var token = entities.aspnet_ResetPasswordTokens.Include("aspnet_Membership").FirstOrDefault( t => t.TokenId == tokenId && t.aspnet_Membership.aspnet_Users.LoweredUserName == username.ToLower() && t.aspnet_Membership.aspnet_Applications.LoweredApplicationName == this.ApplicationName.ToLower()); if (token == null) { return false; } var result = this.ChangeUserPassword(this.ApplicationName, username, newPassword); entities.aspnet_ResetPasswordTokens.DeleteObject(token); entities.SaveChanges(); return result; } }
/// <summary> /// Returns the usernames of all the users that are currently online; that is, whose LastActivityDate is /// greater than the current date and time minus the value of the membership service's /// UserIsOnlineTimeWindow property, which can be read from Membership.UserIsOnlineTimeWindow. /// UserIsOnlineTimeWindow specifies a time in minutes and is set using the /// <code> /// <membership> /// </code> /// element's userIsOnlineTimeWindow attribute. /// </summary> /// <returns> /// Returns a list containing the usernames of all the users that are currently online /// </returns> /// <remarks> /// </remarks> public override IList<string> GetOnlineUsers() { var dateActive = DateTime.UtcNow.AddMinutes(-1 * Membership.UserIsOnlineTimeWindow); using (var entities = new AppleseedMembershipEntities(ConfigurationManager.ConnectionStrings["AppleseedMembershipEntities"].ConnectionString)) { var users = entities.aspnet_Users.Include("aspnet_Membership").Include("aspnet_Application"); // to avoid lazy loading return users.Where(u => u.aspnet_Applications.LoweredApplicationName == this.ApplicationName.ToLower()). Where(u => u.LastActivityDate > dateActive).Select(u => u.UserName).ToList(); } }