4
4
*/
5
5
6
6
using Microsoft . Graph ;
7
+ using Microsoft . Graph . Auth ;
7
8
using Microsoft_Graph_ASPNET_Snippets . Helpers ;
8
9
using Microsoft_Graph_ASPNET_Snippets . Models ;
9
10
using Resources ;
10
- using System ;
11
- using System . Collections . Generic ;
12
11
using System . Threading . Tasks ;
12
+ using System . Web ;
13
13
using System . Web . Mvc ;
14
14
15
15
namespace Microsoft_Graph_ASPNET_Snippets . Controllers
16
16
{
17
17
[ Authorize ]
18
18
public class EventsController : Controller
19
19
{
20
- EventsService eventsService = new EventsService ( ) ;
21
-
20
+ EventsService eventsService ;
21
+
22
+ public EventsController ( )
23
+ {
24
+ // Initialize the GraphServiceClient.
25
+ GraphServiceClient graphClient = SDKHelper . GetAuthenticatedClient ( ) ;
26
+ eventsService = new EventsService ( graphClient ) ;
27
+ }
22
28
public ActionResult Index ( )
23
29
{
24
30
return View ( "Events" ) ;
@@ -30,20 +36,17 @@ public async Task<ActionResult> GetMyEvents()
30
36
ResultsViewModel results = new ResultsViewModel ( ) ;
31
37
try
32
38
{
33
-
34
- // Initialize the GraphServiceClient.
35
- GraphServiceClient graphClient = SDKHelper . GetAuthenticatedClient ( ) ;
36
-
37
39
// Get events.
38
- results . Items = await eventsService . GetMyEvents ( graphClient ) ;
40
+ results . Items = await eventsService . GetMyEvents ( ) ;
39
41
}
40
42
catch ( ServiceException se )
41
43
{
42
- if ( se . Error . Message == Resource . Error_AuthChallengeNeeded ) return new EmptyResult ( ) ;
43
-
44
- // Personal accounts that aren't enabled for the Outlook REST API get a "MailboxNotEnabledForRESTAPI" or "MailboxNotSupportedForRESTAPI" error.
44
+ if ( ( se . InnerException as AuthenticationException ) ? . Error . Code == Resource . Error_AuthChallengeNeeded )
45
+ {
46
+ HttpContext . Request . GetOwinContext ( ) . Authentication . Challenge ( ) ;
47
+ return new EmptyResult ( ) ;
48
+ }
45
49
return RedirectToAction ( "Index" , "Error" , new { message = string . Format ( Resource . Error_Message , Request . RawUrl , se . Error . Code , se . Error . Message ) } ) ;
46
-
47
50
}
48
51
return View ( "Events" , results ) ;
49
52
}
@@ -54,17 +57,16 @@ public async Task<ActionResult> GetMyCalendarView()
54
57
ResultsViewModel results = new ResultsViewModel ( ) ;
55
58
try
56
59
{
57
- // Initialize the GraphServiceClient.
58
- GraphServiceClient graphClient = SDKHelper . GetAuthenticatedClient ( ) ;
59
-
60
60
// Get a calendar view.
61
- results . Items = await eventsService . GetMyCalendarView ( graphClient ) ;
61
+ results . Items = await eventsService . GetMyCalendarView ( ) ;
62
62
}
63
63
catch ( ServiceException se )
64
64
{
65
- if ( se . Error . Message == Resource . Error_AuthChallengeNeeded ) return new EmptyResult ( ) ;
66
-
67
- // Personal accounts that aren't enabled for the Outlook REST API get a "MailboxNotEnabledForRESTAPI" or "MailboxNotSupportedForRESTAPI" error.
65
+ if ( ( se . InnerException as AuthenticationException ) ? . Error . Code == Resource . Error_AuthChallengeNeeded )
66
+ {
67
+ HttpContext . Request . GetOwinContext ( ) . Authentication . Challenge ( ) ;
68
+ return new EmptyResult ( ) ;
69
+ }
68
70
return RedirectToAction ( "Index" , "Error" , new { message = string . Format ( Resource . Error_Message , Request . RawUrl , se . Error . Code , se . Error . Message ) } ) ;
69
71
}
70
72
return View ( "Events" , results ) ;
@@ -77,18 +79,16 @@ public async Task<ActionResult> CreateEvent()
77
79
ResultsViewModel results = new ResultsViewModel ( ) ;
78
80
try
79
81
{
80
-
81
- // Initialize the GraphServiceClient.
82
- GraphServiceClient graphClient = SDKHelper . GetAuthenticatedClient ( ) ;
83
-
84
82
// Create the event.
85
- results . Items = await eventsService . CreateEvent ( graphClient ) ;
83
+ results . Items = await eventsService . CreateEvent ( ) ;
86
84
}
87
85
catch ( ServiceException se )
88
86
{
89
- if ( se . Error . Message == Resource . Error_AuthChallengeNeeded ) return new EmptyResult ( ) ;
90
-
91
- // Personal accounts that aren't enabled for the Outlook REST API get a "MailboxNotEnabledForRESTAPI" or "MailboxNotSupportedForRESTAPI" error.
87
+ if ( ( se . InnerException as AuthenticationException ) ? . Error . Code == Resource . Error_AuthChallengeNeeded )
88
+ {
89
+ HttpContext . Request . GetOwinContext ( ) . Authentication . Challenge ( ) ;
90
+ return new EmptyResult ( ) ;
91
+ }
92
92
return RedirectToAction ( "Index" , "Error" , new { message = string . Format ( Resource . Error_Message , Request . RawUrl , se . Error . Code , se . Error . Message ) } ) ;
93
93
}
94
94
return View ( "Events" , results ) ;
@@ -100,18 +100,16 @@ public async Task<ActionResult> GetEvent(string id)
100
100
ResultsViewModel results = new ResultsViewModel ( ) ;
101
101
try
102
102
{
103
-
104
- // Initialize the GraphServiceClient.
105
- GraphServiceClient graphClient = SDKHelper . GetAuthenticatedClient ( ) ;
106
-
107
103
// Get the event.
108
- results . Items = await eventsService . GetEvent ( graphClient , id ) ;
104
+ results . Items = await eventsService . GetEvent ( id ) ;
109
105
}
110
106
catch ( ServiceException se )
111
107
{
112
- if ( se . Error . Message == Resource . Error_AuthChallengeNeeded ) return new EmptyResult ( ) ;
113
-
114
- // Personal accounts that aren't enabled for the Outlook REST API get a "MailboxNotEnabledForRESTAPI" or "MailboxNotSupportedForRESTAPI" error.
108
+ if ( ( se . InnerException as AuthenticationException ) ? . Error . Code == Resource . Error_AuthChallengeNeeded )
109
+ {
110
+ HttpContext . Request . GetOwinContext ( ) . Authentication . Challenge ( ) ;
111
+ return new EmptyResult ( ) ;
112
+ }
115
113
return RedirectToAction ( "Index" , "Error" , new { message = string . Format ( Resource . Error_Message , Request . RawUrl , se . Error . Code , se . Error . Message ) } ) ;
116
114
}
117
115
return View ( "Events" , results ) ;
@@ -124,18 +122,16 @@ public async Task<ActionResult> UpdateEvent(string id, string name)
124
122
ResultsViewModel results = new ResultsViewModel ( ) ;
125
123
try
126
124
{
127
-
128
- // Initialize the GraphServiceClient.
129
- GraphServiceClient graphClient = SDKHelper . GetAuthenticatedClient ( ) ;
130
-
131
125
// Update the event.
132
- results . Items = await eventsService . UpdateEvent ( graphClient , id , name ) ;
126
+ results . Items = await eventsService . UpdateEvent ( id , name ) ;
133
127
}
134
128
catch ( ServiceException se )
135
129
{
136
- if ( se . Error . Message == Resource . Error_AuthChallengeNeeded ) return new EmptyResult ( ) ;
137
-
138
- // Personal accounts that aren't enabled for the Outlook REST API get a "MailboxNotEnabledForRESTAPI" or "MailboxNotSupportedForRESTAPI" error.
130
+ if ( ( se . InnerException as AuthenticationException ) ? . Error . Code == Resource . Error_AuthChallengeNeeded )
131
+ {
132
+ HttpContext . Request . GetOwinContext ( ) . Authentication . Challenge ( ) ;
133
+ return new EmptyResult ( ) ;
134
+ }
139
135
return RedirectToAction ( "Index" , "Error" , new { message = string . Format ( Resource . Error_Message , Request . RawUrl , se . Error . Code , se . Error . Message ) } ) ;
140
136
}
141
137
return View ( "Events" , results ) ;
@@ -147,18 +143,16 @@ public async Task<ActionResult> DeleteEvent(string id)
147
143
ResultsViewModel results = new ResultsViewModel ( false ) ;
148
144
try
149
145
{
150
-
151
- // Initialize the GraphServiceClient.
152
- GraphServiceClient graphClient = SDKHelper . GetAuthenticatedClient ( ) ;
153
-
154
146
// Delete the event.
155
- results . Items = await eventsService . DeleteEvent ( graphClient , id ) ;
147
+ results . Items = await eventsService . DeleteEvent ( id ) ;
156
148
}
157
149
catch ( ServiceException se )
158
150
{
159
- if ( se . Error . Message == Resource . Error_AuthChallengeNeeded ) return new EmptyResult ( ) ;
160
-
161
- // Personal accounts that aren't enabled for the Outlook REST API get a "MailboxNotEnabledForRESTAPI" or "MailboxNotSupportedForRESTAPI" error.
151
+ if ( ( se . InnerException as AuthenticationException ) ? . Error . Code == Resource . Error_AuthChallengeNeeded )
152
+ {
153
+ HttpContext . Request . GetOwinContext ( ) . Authentication . Challenge ( ) ;
154
+ return new EmptyResult ( ) ;
155
+ }
162
156
return RedirectToAction ( "Index" , "Error" , new { message = string . Format ( Resource . Error_Message , Request . RawUrl , se . Error . Code , se . Error . Message ) } ) ;
163
157
}
164
158
return View ( "Events" , results ) ;
@@ -172,17 +166,16 @@ public async Task<ActionResult> AcceptMeetingRequest(string id)
172
166
ResultsViewModel results = new ResultsViewModel ( false ) ;
173
167
try
174
168
{
175
- // Initialize the GraphServiceClient.
176
- GraphServiceClient graphClient = SDKHelper . GetAuthenticatedClient ( ) ;
177
-
178
169
// Accept the meeting.
179
- results . Items = await eventsService . AcceptMeetingRequest ( graphClient , id ) ;
170
+ results . Items = await eventsService . AcceptMeetingRequest ( id ) ;
180
171
}
181
172
catch ( ServiceException se )
182
173
{
183
- if ( se . Error . Message == Resource . Error_AuthChallengeNeeded ) return new EmptyResult ( ) ;
184
-
185
- // Personal accounts that aren't enabled for the Outlook REST API get a "MailboxNotEnabledForRESTAPI" or "MailboxNotSupportedForRESTAPI" error.
174
+ if ( ( se . InnerException as AuthenticationException ) ? . Error . Code == Resource . Error_AuthChallengeNeeded )
175
+ {
176
+ HttpContext . Request . GetOwinContext ( ) . Authentication . Challenge ( ) ;
177
+ return new EmptyResult ( ) ;
178
+ }
186
179
return RedirectToAction ( "Index" , "Error" , new { message = string . Format ( Resource . Error_Message , Request . RawUrl , se . Error . Code , se . Error . Message ) } ) ;
187
180
}
188
181
return View ( "Events" , results ) ;
0 commit comments