@@ -33,6 +33,10 @@ public bool ContainsPrefix(string prefix)
3333
3434 public ValueProviderResult GetValue ( string key )
3535 {
36+ int pos = key . LastIndexOf ( "." , StringComparison . Ordinal ) ;
37+ if ( pos != - 1 )
38+ key = key . Substring ( pos + 1 ) ;
39+
3640 string [ ] value = null ;
3741
3842 if ( _context . CookieData . Values ? . TryGetValue ( key , out value ) != true )
@@ -54,44 +58,42 @@ public ValueProviderResult GetValue(string key)
5458#if NET45
5559 public class GriddlyCookieFilterValueProviderFactory : ValueProviderFactory
5660 {
57- Func < ControllerContext , bool > _canProvide = null ;
61+ Func < ControllerContext , bool > _canProvide = ( controllerContext ) => controllerContext . HttpContext . Request . QueryString . Count == 0 ;
5862
5963 public GriddlyCookieFilterValueProviderFactory ( Func < ControllerContext , bool > canProvide = null )
6064 {
65+ if ( canProvide != null )
6166 _canProvide = canProvide ;
6267 }
6368
6469 public override IValueProvider GetValueProvider ( ControllerContext controllerContext )
6570 {
66- if ( controllerContext . IsChildAction && controllerContext . HttpContext . Request . QueryString . Count == 0 )
71+ if ( controllerContext . IsChildAction && _canProvide . Invoke ( controllerContext ) )
6772 {
68- if ( _canProvide ? . Invoke ( controllerContext ) != false )
69- {
70- var context = controllerContext . Controller . GetOrCreateGriddlyContext ( ) ;
71- var cookie = controllerContext . HttpContext . Request . Cookies [ context . CookieName ] ;
73+ var context = controllerContext . Controller . GetOrCreateGriddlyContext ( ) ;
74+ var cookie = controllerContext . HttpContext . Request . Cookies [ context . CookieName ] ;
7275
73- if ( cookie != null && ! string . IsNullOrWhiteSpace ( cookie . Value ) )
76+ if ( cookie != null && ! string . IsNullOrWhiteSpace ( cookie . Value ) )
77+ {
78+ try
7479 {
75- try
76- {
77- var data = JsonConvert . DeserializeObject < GriddlyFilterCookieData > ( cookie . Value ) ;
78-
79- // chrome/ff don't delete session cookies if they're set to "continue where you left off"
80- // https://stackoverflow.com/questions/10617954/chrome-doesnt-delete-session-cookies
81- // only use a cookie if it's new within 100 minutes
82- if ( data . CreatedUtc != null && ( DateTime . UtcNow - data . CreatedUtc . Value ) . TotalMinutes < 100 )
83- {
84- context . CookieData = data ;
85- context . IsDefaultSkipped = true ;
80+ var data = JsonConvert . DeserializeObject < GriddlyFilterCookieData > ( cookie . Value ) ;
8681
87- return new GriddlyCookieFilterValueProvider ( context ) ;
88- }
89- }
90- catch
82+ // chrome/ff don't delete session cookies if they're set to "continue where you left off"
83+ // https://stackoverflow.com/questions/10617954/chrome-doesnt-delete-session-cookies
84+ // only use a cookie if it's new within 100 minutes
85+ if ( data . CreatedUtc != null && ( DateTime . UtcNow - data . CreatedUtc . Value ) . TotalMinutes < 100 )
9186 {
92- // TODO: log it?
87+ context . CookieData = data ;
88+ context . IsDefaultSkipped = true ;
89+
90+ return new GriddlyCookieFilterValueProvider ( context ) ;
9391 }
9492 }
93+ catch
94+ {
95+ // TODO: log it?
96+ }
9597 }
9698 }
9799
@@ -101,48 +103,44 @@ public override IValueProvider GetValueProvider(ControllerContext controllerCont
101103#else
102104 public class GriddlyCookieFilterValueProviderFactory : IValueProviderFactory
103105 {
104- Func < ActionContext , bool > _canProvide = null ;
106+ Func < ActionContext , bool > _canProvide = ( actionContext ) => actionContext . HttpContext . Request . Query . Count == 0 ;
105107
106108 public GriddlyCookieFilterValueProviderFactory ( Func < ActionContext , bool > canProvide = null )
107109 {
108- _canProvide = canProvide ;
110+ if ( canProvide != null )
111+ _canProvide = canProvide ;
109112 }
110113
111114 public Task CreateValueProviderAsync ( ValueProviderFactoryContext vpfc )
112115 {
113116 return Task . Factory . StartNew ( ( ) =>
114117 {
115- var isChildAction = vpfc . ActionContext . HttpContext . IsChildAction ( ) ;
116-
117- if ( isChildAction && vpfc . ActionContext . HttpContext . Request . Query . Count == 0 )
118+ if ( vpfc . ActionContext . HttpContext . IsChildAction ( ) && _canProvide . Invoke ( vpfc . ActionContext ) )
118119 {
119- if ( _canProvide ? . Invoke ( vpfc . ActionContext ) != false )
120- {
121- var context = vpfc . ActionContext . GetOrCreateGriddlyContext ( ) ;
122- var cookie = vpfc . ActionContext . HttpContext . Request . Cookies [ context . CookieName ] ;
120+ var context = vpfc . ActionContext . GetOrCreateGriddlyContext ( ) ;
121+ var cookie = vpfc . ActionContext . HttpContext . Request . Cookies [ context . CookieName ] ;
123122
124- if ( cookie != null && ! string . IsNullOrWhiteSpace ( cookie ) )
123+ if ( cookie != null && ! string . IsNullOrWhiteSpace ( cookie ) )
124+ {
125+ try
125126 {
126- try
127- {
128- var data = JsonConvert . DeserializeObject < GriddlyFilterCookieData > ( cookie ) ;
129-
130- // chrome/ff don't delete session cookies if they're set to "continue where you left off"
131- // https://stackoverflow.com/questions/10617954/chrome-doesnt-delete-session-cookies
132- // only use a cookie if it's new within 100 minutes
133- if ( data . CreatedUtc != null && ( DateTime . UtcNow - data . CreatedUtc . Value ) . TotalMinutes < 100 )
134- {
135- context . CookieData = data ;
136- context . IsDefaultSkipped = true ;
137-
138- vpfc . ValueProviders . Add ( new GriddlyCookieFilterValueProvider ( context ) ) ;
139- }
140- }
141- catch
127+ var data = JsonConvert . DeserializeObject < GriddlyFilterCookieData > ( cookie ) ;
128+
129+ // chrome/ff don't delete session cookies if they're set to "continue where you left off"
130+ // https://stackoverflow.com/questions/10617954/chrome-doesnt-delete-session-cookies
131+ // only use a cookie if it's new within 100 minutes
132+ if ( data . CreatedUtc != null && ( DateTime . UtcNow - data . CreatedUtc . Value ) . TotalMinutes < 100 )
142133 {
143- // TODO: log it?
134+ context . CookieData = data ;
135+ context . IsDefaultSkipped = true ;
136+
137+ vpfc . ValueProviders . Add ( new GriddlyCookieFilterValueProvider ( context ) ) ;
144138 }
145139 }
140+ catch
141+ {
142+ // TODO: log it?
143+ }
146144 }
147145 }
148146 } ) ;
0 commit comments