Ewl » Canonical The canonical version of EWL Read More
Clone URL:  
EwfPage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using EnterpriseWebLibrary.Configuration; using EnterpriseWebLibrary.DataAccess; using EnterpriseWebLibrary.EnterpriseWebFramework.DisplayLinking; using EnterpriseWebLibrary.EnterpriseWebFramework.UserManagement; using EnterpriseWebLibrary.WebSessionState; using Humanizer; using NodaTime; using StackExchange.Profiling; namespace EnterpriseWebLibrary.EnterpriseWebFramework { /// <summary> /// A System.Web.UI.Page that contains special Red Stapler Enterprise Web Framework logic. Requires that view state and session state be enabled. /// </summary> public abstract class EwfPage: Page { // This string is duplicated in the JavaScript file. private const string postBackHiddenFieldName = "ewfPostBack"; internal const string ButtonElementName = "ewfButton"; private static Func<IEnumerable<EtherealComponent>> browsingModalBoxCreator; private static Func<IEnumerable<ResourceInfo>> cssInfoCreator; internal new static void Init( Func<IEnumerable<EtherealComponent>> browsingModalBoxCreator, Func<IEnumerable<ResourceInfo>> cssInfoCreator ) { EwfValidation.Init( () => Instance.formState.ValidationPredicate, () => Instance.formState.DataModifications, () => Instance.formState.DataModificationsWithValidationsFromOtherElements, () => Instance.formState.ReportValidationCreated() ); PostBack.Init( () => Instance.formState.DataModifications ); PostBackFormAction.Init( postBack => { if( !Instance.postBacksById.TryGetValue( postBack.Id, out var existingPostBack ) ) Instance.postBacksById.Add( postBack.Id, postBack ); else if( existingPostBack != postBack ) throw new ApplicationException( "A post-back with an ID of \"{0}\" already exists in the page.".FormatWith( existingPostBack.Id ) ); }, postBack => { if( Instance.GetPostBack( postBack.Id ) != postBack ) throw new ApplicationException( "The post-back must have been added to the page." ); if( ( postBack as ActionPostBack )?.ValidationDm is PostBack validationPostBack && Instance.GetPostBack( validationPostBack.Id ) != validationPostBack ) throw new ApplicationException( "The post-back's validation data-modification, if it is a post-back, must have been added to the page." ); } ); FormState.Init( () => Instance.formState, dataModifications => { if( dataModifications.Contains( Instance.dataUpdate ) && dataModifications.Any( i => i != Instance.dataUpdate && !( (ActionPostBack)i ).IsIntermediate ) ) throw new ApplicationException( "If the data-update modification is included, it is meaningless to include any full post-backs since these inherently update the page's data." ); }, dataModification => dataModification == Instance.dataUpdate ? Instance.dataUpdatePostBack : (ActionPostBack)dataModification ); EwfPage.browsingModalBoxCreator = browsingModalBoxCreator; EwfPage.cssInfoCreator = cssInfoCreator; } /// <summary> /// Returns the currently executing EwfPage, or null if the currently executing page is not an EwfPage. /// </summary> public static EwfPage Instance => HttpContext.Current.CurrentHandler as EwfPage; /// <summary> /// Add a status message of the given type to the status message collection. /// </summary> public static void AddStatusMessage( StatusMessageType type, string message ) { Instance.statusMessages.Add( new Tuple<StatusMessageType, string>( type, message ) ); } internal static void AssertPageTreeNotBuilt() { if( Instance.formState == null ) throw new ApplicationException( "The page tree has already been built." ); } internal static void AssertPageTreeBuilt() { if( Instance.formState != null ) throw new ApplicationException( "The page tree has not yet been built." ); } private Control contentContainer; private Control etherealPlace; private FormState formState; internal readonly ModalBoxId BrowsingModalBoxId = new ModalBoxId(); private readonly BasicDataModification dataUpdate = new BasicDataModification(); private readonly PostBack dataUpdatePostBack = PostBack.CreateDataUpdate(); internal readonly Dictionary<PageComponent, IReadOnlyCollection<Control>> ControlsByComponent = new Dictionary<PageComponent, IReadOnlyCollection<Control>>(); private readonly Dictionary<Control, List<EtherealControl>> etherealControlsByControl = new Dictionary<Control, List<EtherealControl>>(); private readonly Dictionary<string, PostBack> postBacksById = new Dictionary<string, PostBack>(); private readonly List<FormValue> formValues = new List<FormValue>(); private readonly List<DisplayLink> displayLinks = new List<DisplayLink>(); private readonly List<LegacyUpdateRegionLinker> updateRegionLinkers = new List<LegacyUpdateRegionLinker>(); private readonly Dictionary<EwfValidation, List<string>> modErrorDisplaysByValidation = new Dictionary<EwfValidation, List<string>>(); internal readonly HashSet<EwfValidation> ValidationsWithErrors = new HashSet<EwfValidation>(); internal readonly Dictionary<Control, List<AutofocusCondition>> AutofocusConditionsByControl = new Dictionary<Control, List<AutofocusCondition>>(); private readonly List<Action> controlTreeValidations = new List<Action>(); internal PostBack SubmitButtonPostBack; private readonly List<Tuple<StatusMessageType, string>> statusMessages = new List<Tuple<StatusMessageType, string>>(); /// <summary> /// Returns the entity setup for this page, if one exists. /// </summary> public abstract EntitySetupBase EsAsBaseType { get; } /// <summary> /// Gets the page info object for this page. Necessary so we can validate query parameters and ensure authenticated user can access the page. /// </summary> public abstract PageInfo InfoAsBaseType { get; } /// <summary> /// Gets the parameters modification object for this page. /// </summary> public abstract ParametersModificationBase ParametersModificationAsBaseType { get; } /// <summary> /// Gets the page state for this page. /// </summary> public PageState PageState => AppRequestState.Instance.EwfPageRequestState.PageState; /// <summary> /// Creates a new page. Do not call this yourself. /// </summary> protected EwfPage() { // Use the entire page as the default content container. contentContainer = this; // We suspect that this disables browser detection for the entire request, not just the page. ClientTarget = "uplevel"; } /// <summary> /// Executes EWF logic in addition to the standard ASP.NET PreInit logic. /// </summary> protected sealed override void OnPreInit( EventArgs e ) { // This logic should happen before the page gets the PreInit event in case it wants to determine the master based on parameters. // NOTE: If the entity setup is a master page, we need to delay this call until after PreInit. initEntitySetupAndCreateInfoObjects(); base.OnPreInit( e ); } private void initEntitySetupAndCreateInfoObjects() { AppRequestState.Instance.UserDisabledByPage = true; try { initEntitySetup(); if( EsAsBaseType != null ) using( MiniProfiler.Current.Step( "EWF - Create entity-setup info" ) ) EsAsBaseType.CreateInfoFromQueryString(); using( MiniProfiler.Current.Step( "EWF - Create page info" ) ) createInfoFromQueryString(); // If the request doesn't match the page's specified security level, redirect with the proper level. Do this before ensuring that the user can access the // page since in certificate authentication systems this can be affected by the connection security level. // // When goal 448 (Clean URLs) is complete, we want to do full URL normalization during request dispatching, like we do with shortcut URLs. We probably // should only do this on GET requests since we don't want to wipe out post backs. bool connectionSecurityIncorrect; using( MiniProfiler.Current.Step( "EWF - Check connection security" ) ) connectionSecurityIncorrect = getConnectionSecurityIncorrect(); if( connectionSecurityIncorrect ) NetTools.Redirect( InfoAsBaseType.GetUrl( false, false, true ) ); } finally { AppRequestState.Instance.UserDisabledByPage = false; } // This logic depends on the authenticated user and on page and entity setup info objects. bool userCanAccessResource; using( MiniProfiler.Current.Step( "EWF - Check page authorization" ) ) userCanAccessResource = InfoAsBaseType.UserCanAccessResource; if( !userCanAccessResource ) throw new AccessDeniedException( ConfigurationStatics.IsIntermediateInstallation && !InfoAsBaseType.IsIntermediateInstallationPublicResource && !AppRequestState.Instance.IntermediateUserExists, InfoAsBaseType.LogInPage ); DisabledResourceMode disabledMode; using( MiniProfiler.Current.Step( "EWF - Check alternative page mode" ) ) disabledMode = InfoAsBaseType.AlternativeMode as DisabledResourceMode; if( disabledMode != null ) throw new PageDisabledException( disabledMode.Message ); var cachedRequestHandler = requestHandler; if( cachedRequestHandler != null ) { Response.ClearHeaders(); Response.ClearContent(); cachedRequestHandler.WriteResponse(); // Calling Response.End() is not a good practice; see http://stackoverflow.com/q/1087777/35349. We should be able to remove this call when we separate // EWF from Web Forms. This is EnduraCode goal 790. Response.End(); } } /// <summary> /// Loads the entity display setup for the page, if one exists. /// </summary> protected abstract void initEntitySetup(); /// <summary> /// Gets the request handler for this page, which will override the page. /// </summary> protected virtual EwfSafeRequestHandler requestHandler => null; /// <summary> /// Performs EWF activities in addition to the normal InitComplete activities. /// </summary> protected sealed override void OnInitComplete( EventArgs e ) { base.OnInitComplete( e ); if( IsPostBack ) return; if( AppRequestState.Instance.EwfPageRequestState != null ) PageState.ClearCustomStateControlKeys(); else if( StandardLibrarySessionState.Instance.EwfPageRequestState != null ) { AppRequestState.Instance.EwfPageRequestState = StandardLibrarySessionState.Instance.EwfPageRequestState; StandardLibrarySessionState.Instance.EwfPageRequestState = null; PageState.ClearCustomStateControlKeys(); } else AppRequestState.Instance.EwfPageRequestState = new EwfPageRequestState( PageState.CreateForNewPage(), null, null ); var requestState = AppRequestState.Instance.EwfPageRequestState; var dmIdAndSecondaryOp = requestState.DmIdAndSecondaryOp; // Page-view data modifications. All data modifications that happen simply because of a request and require no other action by the user should happen once // per page view, and prior to LoadData so that the modified data can be used in the page if necessary. if( requestState.StaticRegionContents == null || ( !requestState.ModificationErrorsExist && dmIdAndSecondaryOp != null && new[] { SecondaryPostBackOperation.Validate, SecondaryPostBackOperation.ValidateChangesOnly }.Contains( dmIdAndSecondaryOp.Item2 ) ) ) { var modMethods = new List<Action>(); modMethods.Add( EwfApp.Instance.GetPageViewDataModificationMethod() ); if( AppRequestState.Instance.UserAccessible ) { if( AppTools.User != null ) modMethods.Add( getLastPageRequestTimeUpdateMethod( AppTools.User ) ); if( AppRequestState.Instance.ImpersonatorExists && AppRequestState.Instance.ImpersonatorUser != null ) modMethods.Add( getLastPageRequestTimeUpdateMethod( AppRequestState.Instance.ImpersonatorUser ) ); } modMethods.Add( getPageViewDataModificationMethod() ); modMethods = modMethods.Where( i => i != null ).ToList(); if( modMethods.Any() ) { DataAccessState.Current.DisableCache(); try { foreach( var i in modMethods ) i(); AppRequestState.AddNonTransactionalModificationMethod( () => { StandardLibrarySessionState.Instance.StatusMessages.AddRange( statusMessages ); statusMessages.Clear(); } ); AppRequestState.Instance.CommitDatabaseTransactionsAndExecuteNonTransactionalModificationMethods(); } finally { DataAccessState.Current.ResetCache(); } } // Re-create info objects. A big reason to do this is that some info objects execute database queries or other code in order to prime the data-access // cache. The code above resets the cache and we want to re-prime it right away. EsAsBaseType?.ClearInfo(); clearInfo(); AppRequestState.Instance.UserDisabledByPage = true; try { if( EsAsBaseType != null ) using( MiniProfiler.Current.Step( "EWF - Create entity-setup info after page-view data modifications" ) ) EsAsBaseType.CreateInfoFromQueryString(); using( MiniProfiler.Current.Step( "EWF - Create page info after page-view data modifications" ) ) createInfoFromQueryString(); bool connectionSecurityIncorrect; using( MiniProfiler.Current.Step( "EWF - Check connection security after page-view data modifications" ) ) connectionSecurityIncorrect = getConnectionSecurityIncorrect(); if( connectionSecurityIncorrect ) throw getPossibleDeveloperMistakeException( "The connection security of the page changed after page-view data modifications." ); } finally { AppRequestState.Instance.UserDisabledByPage = false; } bool userCanAccessResource; using( MiniProfiler.Current.Step( "EWF - Check page authorization after page-view data modifications" ) ) userCanAccessResource = InfoAsBaseType.UserCanAccessResource; DisabledResourceMode disabledMode; using( MiniProfiler.Current.Step( "EWF - Check alternative page mode after page-view data modifications" ) ) disabledMode = InfoAsBaseType.AlternativeMode as DisabledResourceMode; if( !userCanAccessResource || disabledMode != null ) throw getPossibleDeveloperMistakeException( "The user lost access to the page or the page became disabled after page-view data modifications." ); } onLoadData(); if( requestState.StaticRegionContents != null ) { var updateRegionLinkersByKey = updateRegionLinkers.ToDictionary( i => i.Key ); var updateRegionControls = requestState.UpdateRegionKeysAndArguments.SelectMany( keyAndArg => { LegacyUpdateRegionLinker linker; if( !updateRegionLinkersByKey.TryGetValue( keyAndArg.Item1, out linker ) ) throw getPossibleDeveloperMistakeException( "An update region linker with the key \"{0}\" does not exist.".FormatWith( keyAndArg.Item1 ) ); return linker.PostModificationRegionGetter( keyAndArg.Item2 ); } ); var staticRegionContents = getStaticRegionContents( updateRegionControls ); if( staticRegionContents.Item1 != requestState.StaticRegionContents || formValues.Any( i => i.GetPostBackValueKey().Any() && i.PostBackValueIsInvalid( requestState.PostBackValues ) ) ) throw getPossibleDeveloperMistakeException( requestState.ModificationErrorsExist ? "Form controls, modification-error-display keys, and post-back IDs may not change if modification errors exist." + " (IMPORTANT: This exception may have been thrown because EWL Goal 588 hasn't been completed. See the note in the goal about the EwfPage bug and disregard the rest of this error message.)" : new[] { SecondaryPostBackOperation.Validate, SecondaryPostBackOperation.ValidateChangesOnly }.Contains( dmIdAndSecondaryOp.Item2 ) ? "Form controls outside of update regions may not change on an intermediate post-back." : "Form controls and post-back IDs may not change during the validation stage of an intermediate post-back." ); } if( !requestState.ModificationErrorsExist && dmIdAndSecondaryOp != null && dmIdAndSecondaryOp.Item2 == SecondaryPostBackOperation.Validate ) { var secondaryDm = dmIdAndSecondaryOp.Item1.Any() ? GetPostBack( dmIdAndSecondaryOp.Item1 ) as DataModification : dataUpdate; if( secondaryDm == null ) throw getPossibleDeveloperMistakeException( "A data modification with an ID of \"{0}\" does not exist.".FormatWith( dmIdAndSecondaryOp.Item1 ) ); var navigationNeeded = true; executeWithDataModificationExceptionHandling( () => { if( secondaryDm == dataUpdate ) navigationNeeded = dataUpdate.Execute( true, formValues.Any( i => i.ValueChangedOnPostBack( requestState.PostBackValues ) ), handleValidationErrors, performValidationOnly: true ); else { var formValuesChanged = GetDescendants( contentContainer ) .OfType<FormValueControl>() .Any( i => i.FormValue != null && i.FormValue.ValueChangedOnPostBack( requestState.PostBackValues ) ); navigationNeeded = ( (ActionPostBack)secondaryDm ).Execute( formValuesChanged, handleValidationErrors, null ); } if( navigationNeeded ) { requestState.DmIdAndSecondaryOp = Tuple.Create( dmIdAndSecondaryOp.Item1, SecondaryPostBackOperation.NoOperation ); requestState.SetStaticAndUpdateRegionState( getStaticRegionContents( new Control[ 0 ] ).Item1, new Tuple<string, string>[ 0 ] ); } } ); if( navigationNeeded ) navigate( null, null ); } } /// <summary> /// It's important to call this from EwfPage instead of EwfApp because requests for some pages, with their associated images, CSS files, etc., can easily /// cause 20-30 server requests, and we only want to update the time stamp once for all of these. /// </summary> private Action getLastPageRequestTimeUpdateMethod( User user ) { // Only update the request time if a significant amount of time has passed since we did it last. This can dramatically reduce concurrency issues caused by // people rapidly assigning tasks to one another in the System Manager or similar situations. if( AppRequestState.RequestTime - user.LastRequestTime < Duration.FromMinutes( 60 ) ) return null; // Now we want to do a timestamp-based concurrency check so we don't update the last login date if we know another transaction already did. // It is not perfect, but it reduces errors caused by one user doing a long-running request and then doing smaller requests // in another browser window while the first one is still running. // We have to query in a separate transaction because otherwise snapshot isolation will result in us always getting the original LastRequestTime, even if // another transaction has modified its value during this transaction. var newlyQueriedUser = new DataAccessState().ExecuteWithThis( () => { Func<User> userGetter = () => UserManagementStatics.GetUser( user.UserId, false ); return ConfigurationStatics.DatabaseExists ? DataAccessState.Current.PrimaryDatabaseConnection.ExecuteWithConnectionOpen( userGetter ) : userGetter(); } ); if( newlyQueriedUser == null || newlyQueriedUser.LastRequestTime > user.LastRequestTime ) return null; return () => { Action userUpdater = () => { if( FormsAuthStatics.FormsAuthEnabled ) { var formsAuthCapableUser = (FormsAuthCapableUser)user; FormsAuthStatics.SystemProvider.InsertOrUpdateUser( user.UserId, user.Email, user.Role.RoleId, AppRequestState.RequestTime, formsAuthCapableUser.Salt, formsAuthCapableUser.SaltedPassword, formsAuthCapableUser.MustChangePassword ); } else ( UserManagementStatics.SystemProvider as ExternalAuthUserManagementProvider )?.InsertOrUpdateUser( user.UserId, user.Email, user.Role.RoleId, AppRequestState.RequestTime ); }; if( ConfigurationStatics.DatabaseExists ) DataAccessState.Current.PrimaryDatabaseConnection.ExecuteInTransaction( () => { try { userUpdater(); } catch( DbConcurrencyException ) { // Since this method is called on every page request, concurrency errors are common. They are caused when an authenticated user makes one request // and then makes another before ASP.NET has finished processing the first. Since we are only updating the last request date and time, we don't // need to get an error email if the update fails. throw new DoNotCommitException(); } } ); else userUpdater(); }; } // The warning below also appears on EwfApp.GetPageViewDataModificationMethod. /// <summary> /// Returns a method that executes data modifications that happen simply because of a request and require no other action by the user. Returns null if there /// are no modifications, which can improve page performance since the data-access cache does not need to be reset. /// /// WARNING: Don't ever use this to correct for missing loadData preconditions. For example, do not create a page that requires a user preferences row to /// exist and then use a page-view data modification to create the row if it is missing. Page-view data modifications will not execute before the first /// loadData call on post-back requests, and we provide no mechanism to do this because it would allow developers to accidentally cause false user /// concurrency errors by modifying data that affects the rendering of the page. /// </summary> protected virtual Action getPageViewDataModificationMethod() { return null; } /// <summary> /// EWF use only. /// </summary> protected abstract void clearInfo(); /// <summary> /// Creates the info object for this page based on the query parameters of the request. /// </summary> protected abstract void createInfoFromQueryString(); private bool getConnectionSecurityIncorrect() { return InfoAsBaseType.ShouldBeSecureGivenCurrentRequest != EwfApp.Instance.RequestIsSecure( Request ); } /// <summary> /// Loads hidden field state. We use this instead of LoadViewState because the latter doesn't get called during post backs on which the page structure /// changes. /// </summary> protected sealed override object LoadPageStateFromPersistenceMedium() { string formValueHash = null; string lastPostBackFailingDmId = null; try { // Based on our implementation of SavePageStateToPersistenceMedium, the base implementation of LoadPageStateFromPersistenceMedium will return a Pair // with no First object. var pair = base.LoadPageStateFromPersistenceMedium() as Pair; var savedState = PageState.CreateFromViewState( (object[])pair.Second ); AppRequestState.Instance.EwfPageRequestState = new EwfPageRequestState( savedState.Item1, Request.Form[ "__SCROLLPOSITIONX" ], Request.Form[ "__SCROLLPOSITIONY" ] ); formValueHash = (string)savedState.Item2[ 0 ]; lastPostBackFailingDmId = (string)savedState.Item2[ 1 ]; } catch { // Set a 400 status code if there are any problems loading hidden field state. We're assuming these problems are never the developers' fault. if( AppRequestState.Instance.EwfPageRequestState == null ) AppRequestState.Instance.EwfPageRequestState = new EwfPageRequestState( PageState.CreateForNewPage(), null, null ); Response.StatusCode = 400; Response.TrySkipIisCustomErrors = true; AppRequestState.Instance.EwfPageRequestState.FocusKey = ""; AppRequestState.Instance.EwfPageRequestState.GeneralModificationErrors = Translation.ApplicationHasBeenUpdatedAndWeCouldNotInterpretAction.ToCollection(); resetPage(); } onLoadData(); var requestState = AppRequestState.Instance.EwfPageRequestState; ResourceInfo redirectInfo = null; FullResponse fullSecondaryResponse = null; executeWithDataModificationExceptionHandling( () => { validateFormSubmission( formValueHash ); // Get the post-back object and, if necessary, the last post-back's failing data modification. var postBackId = Request.Form[ postBackHiddenFieldName ]; // returns null if field missing var postBack = postBackId != null ? GetPostBack( postBackId ) : null; if( postBack == null ) throw new DataModificationException( Translation.AnotherUserHasModifiedPageAndWeCouldNotInterpretAction ); var lastPostBackFailingDm = postBack.IsIntermediate && lastPostBackFailingDmId != null ? lastPostBackFailingDmId.Any() ? GetPostBack( lastPostBackFailingDmId ) as DataModification : dataUpdate : null; if( postBack.IsIntermediate && lastPostBackFailingDmId != null && lastPostBackFailingDm == null ) throw new DataModificationException( Translation.AnotherUserHasModifiedPageAndWeCouldNotInterpretAction ); // Execute the page's data update. var dmExecuted = false; if( !postBack.IsIntermediate ) try { dmExecuted |= dataUpdate.Execute( !postBack.ForcePageDataUpdate, formValues.Any( i => i.ValueChangedOnPostBack( requestState.PostBackValues ) ), handleValidationErrors ); } catch { requestState.DmIdAndSecondaryOp = Tuple.Create( "", SecondaryPostBackOperation.NoOperation ); throw; } // Execute the post-back. var actionPostBack = postBack as ActionPostBack; if( actionPostBack != null ) { var formValuesChanged = GetDescendants( contentContainer ) .OfType<FormValueControl>() .Any( i => i.FormValue != null && i.FormValue.ValueChangedOnPostBack( requestState.PostBackValues ) ); requestState.FocusKey = ""; try { dmExecuted |= actionPostBack.Execute( formValuesChanged, handleValidationErrors, postBackAction => { redirectInfo = postBackAction?.Resource; requestState.FocusKey = postBackAction?.ReloadBehavior?.FocusKey ?? ""; fullSecondaryResponse = postBackAction?.ReloadBehavior?.SecondaryResponse?.GetFullResponse(); } ); } catch { requestState.DmIdAndSecondaryOp = Tuple.Create( actionPostBack.Id, SecondaryPostBackOperation.NoOperation ); throw; } } if( dmExecuted ) { AppRequestState.AddNonTransactionalModificationMethod( () => StandardLibrarySessionState.Instance.StatusMessages.AddRange( statusMessages ) ); try { AppRequestState.Instance.CommitDatabaseTransactionsAndExecuteNonTransactionalModificationMethods(); } finally { DataAccessState.Current.ResetCache(); } } if( postBack.IsIntermediate ) { var regionSets = new HashSet<UpdateRegionSet>( actionPostBack.UpdateRegions ); var preModRegions = updateRegionLinkers.SelectMany( i => i.PreModificationRegions, ( linker, region ) => new { region.Sets, region.ControlGetter, linker.Key, region.ArgumentGetter } ) .Where( i => regionSets.Overlaps( i.Sets ) ) .ToArray(); var staticRegionContents = getStaticRegionContents( preModRegions.SelectMany( i => i.ControlGetter() ) ); requestState.PostBackValues.RemoveExcept( staticRegionContents.Item2.Select( i => i.GetPostBackValueKey() ) ); requestState.DmIdAndSecondaryOp = Tuple.Create( actionPostBack.ValidationDm == dataUpdate ? "" : ( (ActionPostBack)actionPostBack.ValidationDm ).Id, actionPostBack.ValidationDm == lastPostBackFailingDm ? SecondaryPostBackOperation.Validate : SecondaryPostBackOperation.ValidateChangesOnly ); requestState.SetStaticAndUpdateRegionState( staticRegionContents.Item1, preModRegions.Select( i => Tuple.Create( i.Key, i.ArgumentGetter() ) ).ToArray() ); } else requestState.PostBackValues = null; } ); navigate( redirectInfo, requestState.ModificationErrorsExist ? null : fullSecondaryResponse ); return null; } /// <summary> /// This needs to be called after the page state dictionary has been created or restored. /// </summary> private void onLoadData() { // This can go anywhere in the lifecycle. addMetadataAndFaviconLinks(); addTypekitLogicIfNecessary(); Header.AddControlsReturnThis( from i in cssInfoCreator() select getStyleSheetLink( this.GetClientUrl( i.GetUrl( false, false, false ) ) ) ); addModernizrLogic(); addGoogleAnalyticsLogicIfNecessary(); addJavaScriptIncludes(); // Set the page title. This should be done before LoadData to support pages or entity setups that want to set their own title. Title = StringTools.ConcatenateWithDelimiter( " - ", EwfApp.Instance.AppDisplayName.Length > 0 ? EwfApp.Instance.AppDisplayName : ConfigurationStatics.SystemName, ResourceInfo.CombineResourcePathStrings( ResourceInfo.ResourcePathSeparator, InfoAsBaseType.ParentResourceEntityPathString, InfoAsBaseType.ResourceFullName ) ); Form.Controls.Add( etherealPlace = new PlaceHolder() ); formState = new FormState(); browsingModalBoxCreator().AddEtherealControls( Form ); FormState.ExecuteWithDataModificationsAndDefaultAction( DataUpdate.ToCollection(), () => { if( EsAsBaseType != null ) using( MiniProfiler.Current.Step( "EWF - Load entity-setup data" ) ) EsAsBaseType.LoadData(); using( MiniProfiler.Current.Step( "EWF - Load page data" ) ) loadData(); } ); using( MiniProfiler.Current.Step( "EWF - Load control data" ) ) loadDataForControlAndChildren( this ); formState = null; foreach( var i in controlTreeValidations ) i(); var duplicatePostBackValueKeys = formValues.Select( i => i.GetPostBackValueKey() ).Where( i => i.Any() ).GetDuplicates().ToArray(); if( duplicatePostBackValueKeys.Any() ) throw new ApplicationException( "Duplicate post-back-value keys exist: " + StringTools.ConcatenateWithDelimiter( ", ", duplicatePostBackValueKeys ) + "." ); ClientScript.RegisterHiddenField( postBackHiddenFieldName, "" ); foreach( var i in formValues ) i.SetPageModificationValues( AppRequestState.Instance.EwfPageRequestState.PostBackValues ); // Web Forms compatibility. Remove when EnduraCode goal 790 is complete. foreach( var displayLink in displayLinks ) displayLink.SetInitialDisplay( AppRequestState.Instance.EwfPageRequestState.PostBackValues ); foreach( var displayLink in displayLinks ) displayLink.AddJavaScript(); // This must happen after LoadData and before modifications are executed. statusMessages.Clear(); } private void addMetadataAndFaviconLinks() { Header.Controls.Add( new HtmlMeta { Name = "application-name", Content = EwfApp.Instance.AppDisplayName.Length > 0 ? EwfApp.Instance.AppDisplayName : ConfigurationStatics.SystemName } ); // Chrome start URL Header.Controls.Add( new HtmlMeta { Name = "application-url", Content = this.GetClientUrl( NetTools.HomeUrl ) } ); // IE9 start URL Header.Controls.Add( new HtmlMeta { Name = "msapplication-starturl", Content = this.GetClientUrl( NetTools.HomeUrl ) } ); var faviconPng48X48 = EwfApp.Instance.FaviconPng48X48; if( faviconPng48X48 != null && faviconPng48X48.UserCanAccessResource ) { var link = new HtmlLink { Href = this.GetClientUrl( faviconPng48X48.GetUrl( true, true, false ) ) }; link.Attributes.Add( "rel", "icon" ); link.Attributes.Add( "sizes", "48x48" ); Header.Controls.Add( link ); } var favicon = EwfApp.Instance.Favicon; if( favicon != null && favicon.UserCanAccessResource ) { var link = new HtmlLink { Href = this.GetClientUrl( favicon.GetUrl( true, true, false ) ) }; link.Attributes.Add( "rel", "shortcut icon" ); // rel="shortcut icon" is deprecated and will be replaced with rel="icon". Header.Controls.Add( link ); } Header.Controls.Add( new HtmlMeta { Name = "viewport", Content = "initial-scale=1" } ); } private void addTypekitLogicIfNecessary() { if( EwfApp.Instance.TypekitId.Length > 0 ) { Header.Controls.Add( new Literal { Text = "<script type=\"text/javascript\" src=\"http" + ( EwfApp.Instance.RequestIsSecure( Request ) ? "s" : "" ) + "://use.typekit.com/" + EwfApp.Instance.TypekitId + ".js\"></script>" } ); Header.Controls.Add( new Literal { Text = "<script type=\"text/javascript\">try{Typekit.load();}catch(e){}</script>" } ); } } private Control getStyleSheetLink( string url ) { var l = new HtmlLink { Href = url }; l.Attributes.Add( "rel", "stylesheet" ); l.Attributes.Add( "type", "text/css" ); return l; } private void addModernizrLogic() { Header.Controls.Add( new Literal { Text = "<script type=\"text/javascript\" src=\"" + this.GetClientUrl( EwfApp.MetaLogicFactory.CreateModernizrJavaScriptInfo().GetUrl( false, false, false ) ) + "\"></script>" } ); } private void addGoogleAnalyticsLogicIfNecessary() { if( EwfApp.Instance.GoogleAnalyticsWebPropertyId.Length == 0 ) return; using( var sw = new StringWriter() ) { sw.WriteLine( "<script>" ); sw.WriteLine( "(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){" ); sw.WriteLine( "(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o)," ); sw.WriteLine( "m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)" ); sw.WriteLine( "})(window,document,'script','//www.google-analytics.com/analytics.js','ga');" ); var userId = EwfApp.Instance.GetGoogleAnalyticsUserId(); sw.WriteLine( "ga('create', '" + EwfApp.Instance.GoogleAnalyticsWebPropertyId + "', 'auto'{0});", userId.Any() ? ", {{'userId': '{0}'}}".FormatWith( userId ) : "" ); sw.WriteLine( "ga('send', 'pageview');" ); sw.WriteLine( "</script>" ); Header.Controls.Add( new Literal { Text = sw.ToString() } ); } } private void addJavaScriptIncludes() { foreach( var url in from i in EwfApp.MetaLogicFactory.CreateJavaScriptInfos() select i.GetUrl( false, false, false ) ) ClientScript.RegisterClientScriptInclude( GetType(), "ewf" + url, this.GetClientUrl( url ) ); ClientScript.RegisterClientScriptBlock( GetType(), "stackExchangeMiniProfiler", MiniProfiler.RenderIncludes().ToHtmlString(), false ); foreach( var url in from i in EwfApp.Instance.GetJavaScriptFiles() select i.GetUrl( false, false, false ) ) ClientScript.RegisterClientScriptInclude( GetType(), "systemSpecificFile" + url, this.GetClientUrl( url ) ); } /// <summary> /// Loads and displays data on the page. This is a replacement for the Init event that provides access to EWF page state. /// </summary> protected virtual void loadData() {} private void loadDataForControlAndChildren( Control control ) { var controlTreeDataLoader = control as ControlTreeDataLoader; if( controlTreeDataLoader != null ) { FormState.Current.SetForNextElement(); // This master-page hack will go away when EnduraCode goal 790 is complete. At that point master pages will be nothing more than components. if( control is MasterPage ) FormState.ExecuteWithDataModificationsAndDefaultAction( DataUpdate.ToCollection(), controlTreeDataLoader.LoadData ); else controlTreeDataLoader.LoadData(); } foreach( var child in control.Controls.Cast<Control>().Where( i => i != etherealPlace ) ) loadDataForControlAndChildren( child ); if( !etherealControlsByControl.TryGetValue( control, out var etherealControls ) ) etherealControls = new List<EtherealControl>(); if( etherealControls.Any() ) { var np = new NamingPlaceholder( etherealControls.Select( i => { // This is kind of a hack, but it's an easy way to make sure ethereal controls are initially hidden. i.Control.Style.Add( HtmlTextWriterStyle.Display, "none" ); return i.Control; } ) ) { ID = "ethereal{0}".FormatWith( control.UniqueID.Replace( "$", "" ) ) }; etherealPlace.AddControlsReturnThis( np ); ( (ControlTreeDataLoader)np ).LoadData(); } foreach( var child in etherealControls ) loadDataForControlAndChildren( child.Control ); } /// <summary> /// Gets the page's data-update modification, which executes on every full post-back prior to the post-back object. WARNING: Do *not* use this for /// modifications that should happen because of a specific post-back action, e.g. adding a new item to the database when a button is clicked. There are two /// reasons for this. First, there may be other post-back controls such as buttons or lookup boxes on the page, any of which could also cause the update to /// execute. Second, by default the update only runs if form values were modified, which would not be the case if a user clicks the button on an add-item /// page before entering any data. /// </summary> public DataModification DataUpdate => dataUpdate; /// <summary> /// Gets a post-back that updates the page's data without performing any other actions. /// </summary> public PostBack DataUpdatePostBack => dataUpdatePostBack; /// <summary> /// Gets whether the page forces a post-back when a link is clicked. /// </summary> public virtual bool IsAutoDataUpdater => false; internal void AddEtherealControl( Control parent, EtherealControl etherealControl ) { if( !etherealControlsByControl.TryGetValue( parent, out var etherealControls ) ) { etherealControls = new List<EtherealControl>(); etherealControlsByControl.Add( parent, etherealControls ); } etherealControls.Add( etherealControl ); } internal PostBack GetPostBack( string id ) { PostBack value; return postBacksById.TryGetValue( id, out value ) ? value : null; } internal void AddFormValue( FormValue formValue ) { formValues.Add( formValue ); } /// <summary> /// Adds a display mapping to this page. /// </summary> internal void AddDisplayLink( DisplayLink displayLink ) { displayLinks.Add( displayLink ); } internal void AddUpdateRegionLinker( LegacyUpdateRegionLinker linker ) { updateRegionLinkers.Add( linker ); } /// <summary> /// If you are using the results of this method to create controls, put them in a naming container so that when the controls differ before and after a /// transfer, other parts of the page such as form control IDs do not get affected. /// </summary> internal IEnumerable<string> AddModificationErrorDisplayAndGetErrors( Control control, string keySuffix, EwfValidation validation ) { var key = control.UniqueID + keySuffix; if( modErrorDisplaysByValidation.ContainsKey( validation ) ) modErrorDisplaysByValidation[ validation ].Add( key ); else modErrorDisplaysByValidation.Add( validation, key.ToCollection().ToList() ); // We want to ignore all of the problems that could happen, such as the key not existing in the dictionary. This problem will be shown in a more helpful // way when we compare form control hashes after a transfer. // // Avoid using exceptions here if possible. This method is sometimes called many times during a request, and we've seen exceptions take as long as 50 ms // each when debugging. IEnumerable<string> value; return AppRequestState.Instance.EwfPageRequestState.InLineModificationErrorsByDisplay.TryGetValue( key, out value ) ? value : new string[ 0 ]; } internal void AddControlTreeValidation( Action validation ) { controlTreeValidations.Add( validation ); } /// <summary> /// Notifies this page that only the form controls within the specified control should be checked for modifications and used to set default focus. /// </summary> public void SetContentContainer( Control control ) { contentContainer = control; } /// <summary> /// EWL use only. Gets the status messages. /// </summary> public IEnumerable<Tuple<StatusMessageType, string>> StatusMessages => StandardLibrarySessionState.Instance.StatusMessages.Concat( statusMessages ); private void executeWithDataModificationExceptionHandling( Action method ) { try { method(); } catch( Exception e ) { var dmException = e.GetChain().OfType<DataModificationException>().FirstOrDefault(); if( dmException == null ) throw; AppRequestState.Instance.EwfPageRequestState.FocusKey = ""; AppRequestState.Instance.EwfPageRequestState.GeneralModificationErrors = dmException.HtmlMessages; AppRequestState.Instance.EwfPageRequestState.SetStaticAndUpdateRegionState( getStaticRegionContents( new Control[ 0 ] ).Item1, new Tuple<string, string>[ 0 ] ); } } private void validateFormSubmission( string formValueHash ) { var requestState = AppRequestState.Instance.EwfPageRequestState; var webFormsHiddenFields = new[] { "__EVENTTARGET", "__EVENTARGUMENT", "__LASTFOCUS", "__VIEWSTATE", "__SCROLLPOSITIONX", "__SCROLLPOSITIONY", "__VIEWSTATEGENERATOR" }; var activeFormValues = formValues.Where( i => i.GetPostBackValueKey().Any() ).ToArray(); var postBackValueKeys = new HashSet<string>( activeFormValues.Select( i => i.GetPostBackValueKey() ) ); requestState.PostBackValues = new PostBackValueDictionary(); var extraPostBackValuesExist = requestState.PostBackValues.AddFromRequest( Request.Form.Cast<string>().Except( new[] { postBackHiddenFieldName, ButtonElementName }.Concat( webFormsHiddenFields ) ), postBackValueKeys.Contains, key => Request.Form[ key ] ) | requestState.PostBackValues.AddFromRequest( Request.Files.Cast<string>(), postBackValueKeys.Contains, key => Request.Files[ key ] ); // Make sure data didn't change under this page's feet since the last request. var invalidPostBackValuesExist = activeFormValues.Any( i => i.PostBackValueIsInvalid( requestState.PostBackValues ) ); var formValueHashesDisagree = generateFormValueHash() != formValueHash; if( extraPostBackValuesExist || invalidPostBackValuesExist || formValueHashesDisagree ) { // Remove invalid post-back values so they don't cause a false developer-mistake exception after the transfer. var validPostBackValueKeys = from i in activeFormValues where !i.PostBackValueIsInvalid( requestState.PostBackValues ) select i.GetPostBackValueKey(); requestState.PostBackValues.RemoveExcept( validPostBackValueKeys ); throw new DataModificationException( Translation.AnotherUserHasModifiedPageHtml.ToCollection() ); } } private void handleValidationErrors( EwfValidation validation, IEnumerable<string> errorMessages ) { if( !errorMessages.Any() ) return; if( !modErrorDisplaysByValidation.ContainsKey( validation ) ) throw new ApplicationException( "An undisplayed validation produced errors." ); foreach( var displayKey in modErrorDisplaysByValidation[ validation ] ) { var errorsByDisplay = AppRequestState.Instance.EwfPageRequestState.InLineModificationErrorsByDisplay; errorsByDisplay[ displayKey ] = errorsByDisplay.ContainsKey( displayKey ) ? errorsByDisplay[ displayKey ].Concat( errorMessages ) : errorMessages; } } private Tuple<string, IEnumerable<FormValue>> getStaticRegionContents( IEnumerable<Control> updateRegionControls ) { var contents = new StringBuilder(); updateRegionControls = new HashSet<Control>( updateRegionControls ); var staticFormValues = GetDescendants( this, predicate: i => !updateRegionControls.Contains( i ) ) .OfType<FormValueControl>() .Select( i => i.FormValue ) .Where( i => i != null ) .Distinct() .OrderBy( i => i.GetPostBackValueKey() ) .ToArray(); foreach( var formValue in staticFormValues ) { contents.Append( formValue.GetPostBackValueKey() ); contents.Append( formValue.GetDurableValueAsString() ); } var requestState = AppRequestState.Instance.EwfPageRequestState; if( requestState.ModificationErrorsExist ) // Include mod error display keys. They shouldn't change across a transfer when there are modification errors because that could prevent some of the // errors from being displayed. foreach( var modErrorDisplayKey in modErrorDisplaysByValidation.Values.SelectMany( i => i ) ) contents.Append( modErrorDisplayKey + " " ); if( requestState.ModificationErrorsExist || ( requestState.DmIdAndSecondaryOp != null && requestState.DmIdAndSecondaryOp.Item2 == SecondaryPostBackOperation.NoOperation ) ) // It's probably bad if a developer puts a post-back object in the page because of a modification error. It will be gone on the post-back and cannot be // processed. foreach( var postBack in postBacksById.Values.OrderBy( i => i.Id ) ) contents.Append( postBack.Id ); return Tuple.Create<string, IEnumerable<FormValue>>( contents.ToString(), staticFormValues ); } private void navigate( ResourceInfo destination, FullResponse secondaryResponse ) { var requestState = AppRequestState.Instance.EwfPageRequestState; string destinationUrl; try { // Determine the final redirect destination. If a destination is already specified and it is the current page or a page with the same entity setup, // replace any default optional parameter values it may have with new values from this post back. If a destination isn't specified, make it the current // page with new parameter values from this post back. At the end of this block, redirectInfo is always newly created with fresh data that reflects any // changes that may have occurred in EH methods. It's important that every case below *actually creates* a new page info object to guard against this // scenario: // 1. A page modifies data such that a previously created redirect destination page info object that is then used here is no longer valid because it // would throw an exception from init if it were re-created. // 2. The page redirects, or transfers, to this destination, leading the user to an error page without developers being notified. This is bad behavior. if( requestState.ModificationErrorsExist || ( requestState.DmIdAndSecondaryOp != null && requestState.DmIdAndSecondaryOp.Item2 == SecondaryPostBackOperation.NoOperation ) ) destination = InfoAsBaseType.CloneAndReplaceDefaultsIfPossible( true ); else if( destination != null ) destination = destination.CloneAndReplaceDefaultsIfPossible( false ); else destination = createInfoFromNewParameterValues(); // This GetUrl call is important even for the transfer case below for the same reason that we *actually create* a new page info object in every case // above. We want to force developers to get an error email if a page modifies data to make itself unauthorized/disabled without specifying a different // page as the redirect destination. The resulting transfer would lead the user to an error page. destinationUrl = destination.GetUrl(); } catch( Exception e ) { throw getPossibleDeveloperMistakeException( "The post-modification destination page became invalid.", innerException: e ); } // Put the secondary response into session state right before navigation so that it doesn't get sent if there is an error before this point. if( secondaryResponse != null ) { // It's important that we put the response in session state first since it's used by the Info.init method of the pre-built-response page. StandardLibrarySessionState.Instance.ResponseToSend = secondaryResponse; StandardLibrarySessionState.Instance.SetClientSideNavigation( EwfApp.MetaLogicFactory.CreatePreBuiltResponsePageInfo().GetUrl(), !secondaryResponse.FileName.Any(), null ); } // If the redirect destination is identical to the current page, do a transfer instead of a redirect. if( destination.IsIdenticalToCurrent() ) { AppRequestState.Instance.ClearUserAndImpersonator(); resetPage(); } // If the redirect destination is the current page, but with different query parameters, save request state in session state until the next request. if( destination.GetType() == InfoAsBaseType.GetType() ) StandardLibrarySessionState.Instance.EwfPageRequestState = requestState; NetTools.Redirect( destinationUrl ); } /// <summary> /// Creates a page info object using the new parameter value fields in this page. /// </summary> protected abstract PageInfo createInfoFromNewParameterValues(); private ApplicationException getPossibleDeveloperMistakeException( string messageSentence, Exception innerException = null ) { var sentences = new[] { "Possible developer mistake.", messageSentence, "There is a chance that this was caused by something outside the request, but it's more likely that a developer incorrectly modified something." }; throw new ApplicationException( StringTools.ConcatenateWithDelimiter( " ", sentences ), innerException ); } private void resetPage() { Server.Transfer( Request.AppRelativeCurrentExecutionFilePath ); } protected sealed override void OnPreRender( EventArgs eventArgs ) { base.OnPreRender( eventArgs ); foreach( var i in GetDescendants( this ) ) ( i as ElementNode )?.InitLocalData(); var requestState = AppRequestState.Instance.EwfPageRequestState; var modificationErrorsOccurred = requestState.ModificationErrorsExist && ( requestState.DmIdAndSecondaryOp == null || !new[] { SecondaryPostBackOperation.Validate, SecondaryPostBackOperation.ValidateChangesOnly }.Contains( requestState.DmIdAndSecondaryOp.Item2 ) ); Func<FocusabilityCondition, bool> isFocusablePredicate; if( modificationErrorsOccurred ) isFocusablePredicate = condition => condition.ErrorFocusabilitySources.Validations.Any( i => ValidationsWithErrors.Contains( i ) ) || ( condition.ErrorFocusabilitySources.IncludeGeneralErrors && AppRequestState.Instance.EwfPageRequestState.GeneralModificationErrors.Any() ); else isFocusablePredicate = condition => condition.IsNormallyFocusable; var autofocusInfo = getAutofocusInfo( this, modificationErrorsOccurred, isFocusablePredicate ); if( !modificationErrorsOccurred && autofocusInfo.activeRegionsExist && autofocusInfo.focusedElement == null ) throw new ApplicationException( "The active autofocus regions do not contain any focusable elements." ); addJavaScriptStartUpLogic( autofocusInfo.focusedElement ); // Direct response object modifications. These should happen once per page view; they are not needed in redirect responses. FormsAuthStatics.UpdateFormsAuthCookieIfNecessary(); if( !ConfigurationStatics.IsLiveInstallation ) Response.AppendHeader( "X-Robots-Tag", "noindex, nofollow" ); else if( !InfoAsBaseType.AllowsSearchEngineIndexing ) Response.AppendHeader( "X-Robots-Tag", "noindex" ); // Without this header, certain sites could be forced into compatibility mode due to the Compatibility View Blacklist maintained by Microsoft. Response.AppendHeader( "X-UA-Compatible", "IE=edge" ); StandardLibrarySessionState.Instance.StatusMessages.Clear(); StandardLibrarySessionState.Instance.ClearClientSideNavigation(); } private ( bool activeRegionsExist, ElementNode focusedElement ) getAutofocusInfo( Control control, bool inActiveRegion, Func<FocusabilityCondition, bool> isFocusablePredicate ) { if( !inActiveRegion && AutofocusConditionsByControl.TryGetValue( control, out var conditions ) ) inActiveRegion = conditions.Any( i => i.IsTrue( AppRequestState.Instance.EwfPageRequestState.FocusKey ) ); if( inActiveRegion && control is ElementNode element && isFocusablePredicate( element.FocusabilityCondition ) ) return ( true, element ); if( !etherealControlsByControl.TryGetValue( control, out var etherealControls ) ) etherealControls = new List<EtherealControl>(); var autofocusInfo = ( activeRegionsExist: inActiveRegion, focusedElement: (ElementNode)null ); foreach( var child in control.Controls.Cast<Control>().Where( i => i != etherealPlace ).Concat( from i in etherealControls select i.Control ) ) { var childInfo = getAutofocusInfo( child, inActiveRegion, isFocusablePredicate ); autofocusInfo.activeRegionsExist = autofocusInfo.activeRegionsExist || childInfo.activeRegionsExist; autofocusInfo.focusedElement = childInfo.focusedElement; if( autofocusInfo.focusedElement != null ) break; } return autofocusInfo; } private void addJavaScriptStartUpLogic( ElementNode focusedElement ) { focusedElement?.SetIsFocused(); var controlInitStatements = getDescendants( this, i => true ) .Where( i => i.Item2 != null ) .Select( i => i.Item2() ) .Aggregate( new StringBuilder(), ( builder, statements ) => builder.Append( statements ), i => i.ToString() ); MaintainScrollPositionOnPostBack = true; var requestState = AppRequestState.Instance.EwfPageRequestState; var scroll = scrollPositionForThisResponse == ScrollPosition.LastPositionOrStatusBar && ( !requestState.ModificationErrorsExist || ( requestState.DmIdAndSecondaryOp != null && new[] { SecondaryPostBackOperation.Validate, SecondaryPostBackOperation.ValidateChangesOnly } .Contains( requestState.DmIdAndSecondaryOp.Item2 ) ) ); // If a transfer happened on this request and we're on the same page and we want to scroll, get coordinates from the per-request data in EwfApp. var scrollStatement = ""; if( scroll && requestState.ScrollPositionX != null && requestState.ScrollPositionY != null ) scrollStatement = "window.scrollTo(" + requestState.ScrollPositionX + "," + requestState.ScrollPositionY + ");"; // If the page has requested a client-side redirect, configure it now. The JavaScript solution is preferred over a meta tag since apparently it doesn't // cause reload behavior by the browser. See http://josephsmarr.com/2007/06/06/the-hidden-cost-of-meta-refresh-tags. string clientSideNavigationUrl; bool clientSideNavigationInNewWindow; int? clientSideNavigationDelay; StandardLibrarySessionState.Instance.GetClientSideNavigationSetup( out clientSideNavigationUrl, out clientSideNavigationInNewWindow, out clientSideNavigationDelay ); var clientSideNavigationStatements = ""; if( clientSideNavigationUrl.Any() ) { var url = this.GetClientUrl( clientSideNavigationUrl ); if( clientSideNavigationInNewWindow ) clientSideNavigationStatements = "var newWindow = window.open( '{0}', '{1}' ); newWindow.focus();".FormatWith( url, "_blank" ); else clientSideNavigationStatements = "location.replace( '" + url + "' );"; if( clientSideNavigationDelay.HasValue ) clientSideNavigationStatements = "setTimeout( \"" + clientSideNavigationStatements + "\", " + clientSideNavigationDelay.Value * 1000 + " );"; } ClientScript.RegisterClientScriptBlock( GetType(), "jQueryDocumentReadyBlock", "$( document ).ready( function() { " + StringTools.ConcatenateWithDelimiter( " ", "OnDocumentReady();", "$( '#aspnetForm' ).submit( function( e, postBackId ) {{ postBackRequestStarting( e, postBackId !== undefined ? postBackId : '{0}' ); }} );" .FormatWith( SubmitButtonPostBack != null ? SubmitButtonPostBack.Id : "" /* This empty string we're using when no submit button exists is arbitrary and meaningless; it should never actually be submitted. */ ), controlInitStatements, EwfApp.Instance.JavaScriptDocumentReadyFunctionCall.AppendDelimiter( ";" ), javaScriptDocumentReadyFunctionCall.AppendDelimiter( ";" ), StringTools.ConcatenateWithDelimiter( " ", scrollStatement, clientSideNavigationStatements ) .PrependDelimiter( "window.onload = function() { " ) .AppendDelimiter( " };" ) ) + " } );", true ); } /// <summary> /// Standard Library use only. /// </summary> public IEnumerable<Control> GetDescendants( Control control, Func<Control, bool> predicate = null ) { return from i in getDescendants( control, predicate ?? ( i => true ) ) select i.Item1; } private IEnumerable<Tuple<Control, Func<string>>> getDescendants( Control control, Func<Control, bool> predicate ) { var normalChildren = from i in control.Controls.Cast<Control>() where i != etherealPlace let jsControl = i as ControlWithJsInitLogic select Tuple.Create( i, jsControl != null ? new Func<string>( jsControl.GetJsInitStatements ) : null ); if( !etherealControlsByControl.TryGetValue( control, out var etherealControls ) ) etherealControls = new List<EtherealControl>(); var etherealChildren = etherealControls.Select( i => Tuple.Create( (Control)i.Control, new Func<string>( i.GetJsInitStatements ) ) ); var descendants = new List<Tuple<Control, Func<string>>>(); foreach( var child in normalChildren.Concat( etherealChildren ).Where( i => predicate( i.Item1 ) ) ) { descendants.Add( child ); descendants.AddRange( getDescendants( child.Item1, predicate ) ); } return descendants; } /// <summary> /// The desired scroll position of the browser when this response is received. /// </summary> protected virtual ScrollPosition scrollPositionForThisResponse => ScrollPosition.LastPositionOrStatusBar; /// <summary> /// Gets the function call that should be executed when the jQuery document ready event is fired. /// </summary> protected virtual string javaScriptDocumentReadyFunctionCall => ""; /// <summary> /// Saves view state. /// </summary> protected sealed override object SaveViewState() { // This is the last possible place in the life cycle this could go; view state is saved right after this. foreach( Control child in Controls ) child.EnableViewState = false; return base.SaveViewState(); } /// <summary> /// Saves hidden field state. /// </summary> protected sealed override void SavePageStateToPersistenceMedium( object state ) { var rs = AppRequestState.Instance.EwfPageRequestState; var failingDmId = rs.ModificationErrorsExist && rs.DmIdAndSecondaryOp != null && rs.DmIdAndSecondaryOp.Item2 != SecondaryPostBackOperation.ValidateChangesOnly ? rs.DmIdAndSecondaryOp.Item1 : null; base.SavePageStateToPersistenceMedium( PageState.GetViewStateArray( new object[] { generateFormValueHash(), failingDmId } ) ); } private string generateFormValueHash() { var formValueString = new StringBuilder(); foreach( var formValue in formValues.Where( i => i.GetPostBackValueKey().Any() ) ) { formValueString.Append( formValue.GetPostBackValueKey() ); formValueString.Append( formValue.GetDurableValueAsString() ); } var hash = MD5.Create().ComputeHash( Encoding.ASCII.GetBytes( formValueString.ToString() ) ); var hashString = ""; foreach( var b in hash ) hashString += b.ToString( "x2" ); return hashString; } } }