@@ -50,72 +50,95 @@ private static IAuthorizationState Authorization {
5050 set { HttpContext . Current . Session [ "Authorization" ] = value ; }
5151 }
5252
53- protected async void Page_Load ( object sender , EventArgs e ) {
54- if ( ! IsPostBack ) {
55- // Check to see if we're receiving a end user authorization response.
56- var authorization = await Client . ProcessUserAuthorizationAsync ( new HttpRequestWrapper ( Request ) , Response . ClientDisconnectedToken ) ;
57- if ( authorization != null ) {
58- // We are receiving an authorization response. Store it and associate it with this user.
59- Authorization = authorization ;
60- Response . Redirect ( Request . Path ) ; // get rid of the /?code= parameter
61- }
62- }
63-
64- if ( Authorization != null ) {
65- // Indicate to the user that we have already obtained authorization on some of these.
66- foreach ( var li in this . scopeList . Items . OfType < ListItem > ( ) . Where ( li => Authorization . Scope . Contains ( li . Value ) ) ) {
67- li . Selected = true ;
68- }
69- this . authorizationLabel . Text = "Authorization received!" ;
70- if ( Authorization . AccessTokenExpirationUtc . HasValue ) {
71- TimeSpan timeLeft = Authorization . AccessTokenExpirationUtc . Value - DateTime . UtcNow ;
72- this . authorizationLabel . Text += string . Format ( CultureInfo . CurrentCulture , " (access token expires in {0} minutes)" , Math . Round ( timeLeft . TotalMinutes , 1 ) ) ;
73- }
74- }
75-
76- this . getNameButton . Enabled = this . getAgeButton . Enabled = this . getFavoriteSites . Enabled = Authorization != null ;
53+ protected void Page_Load ( object sender , EventArgs e ) {
54+ this . RegisterAsyncTask (
55+ new PageAsyncTask (
56+ async ct => {
57+ if ( ! IsPostBack ) {
58+ // Check to see if we're receiving a end user authorization response.
59+ var authorization =
60+ await Client . ProcessUserAuthorizationAsync ( new HttpRequestWrapper ( Request ) , Response . ClientDisconnectedToken ) ;
61+ if ( authorization != null ) {
62+ // We are receiving an authorization response. Store it and associate it with this user.
63+ Authorization = authorization ;
64+ Response . Redirect ( Request . Path ) ; // get rid of the /?code= parameter
65+ }
66+ }
67+
68+ if ( Authorization != null ) {
69+ // Indicate to the user that we have already obtained authorization on some of these.
70+ foreach ( var li in this . scopeList . Items . OfType < ListItem > ( ) . Where ( li => Authorization . Scope . Contains ( li . Value ) ) ) {
71+ li . Selected = true ;
72+ }
73+ this . authorizationLabel . Text = "Authorization received!" ;
74+ if ( Authorization . AccessTokenExpirationUtc . HasValue ) {
75+ TimeSpan timeLeft = Authorization . AccessTokenExpirationUtc . Value - DateTime . UtcNow ;
76+ this . authorizationLabel . Text += string . Format (
77+ CultureInfo . CurrentCulture , " (access token expires in {0} minutes)" , Math . Round ( timeLeft . TotalMinutes , 1 ) ) ;
78+ }
79+ }
80+
81+ this . getNameButton . Enabled = this . getAgeButton . Enabled = this . getFavoriteSites . Enabled = Authorization != null ;
82+ } ) ) ;
7783 }
7884
79- protected async void getAuthorizationButton_Click ( object sender , EventArgs e ) {
80- string [ ] scopes = ( from item in this . scopeList . Items . OfType < ListItem > ( )
81- where item . Selected
82- select item . Value ) . ToArray ( ) ;
83-
84- var request = await Client . PrepareRequestUserAuthorizationAsync ( scopes , cancellationToken : Response . ClientDisconnectedToken ) ;
85- await request . SendAsync ( ) ;
86- this . Context . Response . End ( ) ;
85+ protected void getAuthorizationButton_Click ( object sender , EventArgs e ) {
86+ this . RegisterAsyncTask (
87+ new PageAsyncTask (
88+ async ct => {
89+ string [ ] scopes =
90+ ( from item in this . scopeList . Items . OfType < ListItem > ( ) where item . Selected select item . Value ) . ToArray ( ) ;
91+
92+ var request =
93+ await Client . PrepareRequestUserAuthorizationAsync ( scopes , cancellationToken : Response . ClientDisconnectedToken ) ;
94+ await request . SendAsync ( ) ;
95+ this . Context . Response . End ( ) ;
96+ } ) ) ;
8797 }
8898
89- protected async void getNameButton_Click ( object sender , EventArgs e ) {
90- try {
91- this . nameLabel . Text = await this . CallServiceAsync ( client => client . GetName ( ) , Response . ClientDisconnectedToken ) ;
92- } catch ( SecurityAccessDeniedException ) {
93- this . nameLabel . Text = "Access denied!" ;
94- } catch ( MessageSecurityException ) {
95- this . nameLabel . Text = "Access denied!" ;
96- }
99+ protected void getNameButton_Click ( object sender , EventArgs e ) {
100+ this . RegisterAsyncTask (
101+ new PageAsyncTask (
102+ async ct => {
103+ try {
104+ this . nameLabel . Text = await this . CallServiceAsync ( client => client . GetName ( ) , Response . ClientDisconnectedToken ) ;
105+ } catch ( SecurityAccessDeniedException ) {
106+ this . nameLabel . Text = "Access denied!" ;
107+ } catch ( MessageSecurityException ) {
108+ this . nameLabel . Text = "Access denied!" ;
109+ }
110+ } ) ) ;
97111 }
98112
99- protected async void getAgeButton_Click ( object sender , EventArgs e ) {
100- try {
101- int ? age = await this . CallServiceAsync ( client => client . GetAge ( ) , Response . ClientDisconnectedToken ) ;
102- this . ageLabel . Text = age . HasValue ? age . Value . ToString ( CultureInfo . CurrentCulture ) : "not available" ;
103- } catch ( SecurityAccessDeniedException ) {
104- this . ageLabel . Text = "Access denied!" ;
105- } catch ( MessageSecurityException ) {
106- this . ageLabel . Text = "Access denied!" ;
107- }
113+ protected void getAgeButton_Click ( object sender , EventArgs e ) {
114+ this . RegisterAsyncTask (
115+ new PageAsyncTask (
116+ async ct => {
117+ try {
118+ int ? age = await this . CallServiceAsync ( client => client . GetAge ( ) , Response . ClientDisconnectedToken ) ;
119+ this . ageLabel . Text = age . HasValue ? age . Value . ToString ( CultureInfo . CurrentCulture ) : "not available" ;
120+ } catch ( SecurityAccessDeniedException ) {
121+ this . ageLabel . Text = "Access denied!" ;
122+ } catch ( MessageSecurityException ) {
123+ this . ageLabel . Text = "Access denied!" ;
124+ }
125+ } ) ) ;
108126 }
109127
110- protected async void getFavoriteSites_Click ( object sender , EventArgs e ) {
111- try {
112- string [ ] favoriteSites = await this . CallServiceAsync ( client => client . GetFavoriteSites ( ) , Response . ClientDisconnectedToken ) ;
113- this . favoriteSitesLabel . Text = string . Join ( ", " , favoriteSites ) ;
114- } catch ( SecurityAccessDeniedException ) {
115- this . favoriteSitesLabel . Text = "Access denied!" ;
116- } catch ( MessageSecurityException ) {
117- this . favoriteSitesLabel . Text = "Access denied!" ;
118- }
128+ protected void getFavoriteSites_Click ( object sender , EventArgs e ) {
129+ this . RegisterAsyncTask (
130+ new PageAsyncTask (
131+ async ct => {
132+ try {
133+ string [ ] favoriteSites =
134+ await this . CallServiceAsync ( client => client . GetFavoriteSites ( ) , Response . ClientDisconnectedToken ) ;
135+ this . favoriteSitesLabel . Text = string . Join ( ", " , favoriteSites ) ;
136+ } catch ( SecurityAccessDeniedException ) {
137+ this . favoriteSitesLabel . Text = "Access denied!" ;
138+ } catch ( MessageSecurityException ) {
139+ this . favoriteSitesLabel . Text = "Access denied!" ;
140+ }
141+ } ) ) ;
119142 }
120143
121144 private async Task < T > CallServiceAsync < T > ( Func < DataApiClient , T > predicate , CancellationToken cancellationToken ) {
0 commit comments