@@ -65,15 +65,7 @@ public async Task<IActionResult> Index()
6565 //
6666 if ( response . StatusCode == System . Net . HttpStatusCode . Unauthorized )
6767 {
68- var todoTokens = authContext . TokenCache . ReadItems ( ) . Where ( a => a . Resource == AzureAdOptions . Settings . TodoListResourceId ) ;
69- foreach ( TokenCacheItem tci in todoTokens )
70- authContext . TokenCache . DeleteItem ( tci ) ;
71-
72- ViewBag . ErrorMessage = "UnexpectedError" ;
73- TodoItem newItem = new TodoItem ( ) ;
74- newItem . Title = "(No items in list)" ;
75- itemList . Add ( newItem ) ;
76- return View ( itemList ) ;
68+ return ProcessUnauthorized ( itemList , authContext ) ;
7769 }
7870 }
7971 catch ( Exception )
@@ -102,8 +94,6 @@ public async Task<IActionResult> Index()
10294 return View ( "Error" ) ;
10395 }
10496
105-
106-
10797 [ HttpPost ]
10898 public async Task < ActionResult > Index ( string item )
10999 {
@@ -148,15 +138,7 @@ public async Task<ActionResult> Index(string item)
148138 //
149139 if ( response . StatusCode == System . Net . HttpStatusCode . Unauthorized )
150140 {
151- var todoTokens = authContext . TokenCache . ReadItems ( ) . Where ( a => a . Resource == AzureAdOptions . Settings . TodoListResourceId ) ;
152- foreach ( TokenCacheItem tci in todoTokens )
153- authContext . TokenCache . DeleteItem ( tci ) ;
154-
155- ViewBag . ErrorMessage = "UnexpectedError" ;
156- TodoItem newItem = new TodoItem ( ) ;
157- newItem . Title = "(No items in list)" ;
158- itemList . Add ( newItem ) ;
159- return View ( newItem ) ;
141+ return ProcessUnauthorized ( itemList , authContext ) ;
160142 }
161143 }
162144 catch ( Exception )
@@ -177,5 +159,18 @@ public async Task<ActionResult> Index(string item)
177159 }
178160 return View ( "Error" ) ;
179161 }
162+
163+ private ActionResult ProcessUnauthorized ( List < TodoItem > itemList , AuthenticationContext authContext )
164+ {
165+ var todoTokens = authContext . TokenCache . ReadItems ( ) . Where ( a => a . Resource == AzureAdOptions . Settings . TodoListResourceId ) ;
166+ foreach ( TokenCacheItem tci in todoTokens )
167+ authContext . TokenCache . DeleteItem ( tci ) ;
168+
169+ ViewBag . ErrorMessage = "UnexpectedError" ;
170+ TodoItem newItem = new TodoItem ( ) ;
171+ newItem . Title = "(No items in list)" ;
172+ itemList . Add ( newItem ) ;
173+ return View ( itemList ) ;
174+ }
180175 }
181176}
0 commit comments