77 using System . ServiceModel ;
88 using System . ServiceModel . Channels ;
99 using System . ServiceModel . Security ;
10+ using System . Threading . Tasks ;
1011 using DotNetOpenAuth ;
1112 using DotNetOpenAuth . OAuth ;
1213
@@ -25,41 +26,40 @@ protected override bool CheckAccessCore(OperationContext operationContext) {
2526 HttpRequestMessageProperty httpDetails = operationContext . RequestContext . RequestMessage . Properties [ HttpRequestMessageProperty . Name ] as HttpRequestMessageProperty ;
2627 Uri requestUri = operationContext . RequestContext . RequestMessage . Properties . Via ;
2728 ServiceProvider sp = Constants . CreateServiceProvider ( ) ;
28- try {
29- var auth = sp . ReadProtectedResourceAuthorization ( httpDetails , requestUri ) ;
30- if ( auth != null ) {
31- var accessToken = Global . DataContext . OAuthTokens . Single ( token => token . Token == auth . AccessToken ) ;
29+ return Task . Run (
30+ async delegate {
31+ try {
32+ var auth = await sp . ReadProtectedResourceAuthorizationAsync ( httpDetails , requestUri ) ;
33+ if ( auth != null ) {
34+ var accessToken = Global . DataContext . OAuthTokens . Single ( token => token . Token == auth . AccessToken ) ;
3235
33- var principal = sp . CreatePrincipal ( auth ) ;
34- var policy = new OAuthPrincipalAuthorizationPolicy ( principal ) ;
35- var policies = new List < IAuthorizationPolicy > {
36- policy ,
37- } ;
36+ var principal = sp . CreatePrincipal ( auth ) ;
37+ var policy = new OAuthPrincipalAuthorizationPolicy ( principal ) ;
38+ var policies = new List < IAuthorizationPolicy > { policy , } ;
3839
39- var securityContext = new ServiceSecurityContext ( policies . AsReadOnly ( ) ) ;
40- if ( operationContext . IncomingMessageProperties . Security != null ) {
41- operationContext . IncomingMessageProperties . Security . ServiceSecurityContext = securityContext ;
42- } else {
43- operationContext . IncomingMessageProperties . Security = new SecurityMessageProperty {
44- ServiceSecurityContext = securityContext ,
45- } ;
46- }
40+ var securityContext = new ServiceSecurityContext ( policies . AsReadOnly ( ) ) ;
41+ if ( operationContext . IncomingMessageProperties . Security != null ) {
42+ operationContext . IncomingMessageProperties . Security . ServiceSecurityContext = securityContext ;
43+ } else {
44+ operationContext . IncomingMessageProperties . Security = new SecurityMessageProperty {
45+ ServiceSecurityContext = securityContext ,
46+ } ;
47+ }
4748
48- securityContext . AuthorizationContext . Properties [ "Identities" ] = new List < IIdentity > {
49- principal . Identity ,
50- } ;
49+ securityContext . AuthorizationContext . Properties [ "Identities" ] = new List < IIdentity > { principal . Identity , } ;
5150
52- // Only allow this method call if the access token scope permits it.
53- string [ ] scopes = accessToken . Scope . Split ( '|' ) ;
54- if ( scopes . Contains ( operationContext . IncomingMessageHeaders . Action ) ) {
55- return true ;
51+ // Only allow this method call if the access token scope permits it.
52+ string [ ] scopes = accessToken . Scope . Split ( '|' ) ;
53+ if ( scopes . Contains ( operationContext . IncomingMessageHeaders . Action ) ) {
54+ return true ;
55+ }
56+ }
57+ } catch ( ProtocolException ex ) {
58+ Global . Logger . Error ( "Error processing OAuth messages." , ex ) ;
5659 }
57- }
58- } catch ( ProtocolException ex ) {
59- Global . Logger . Error ( "Error processing OAuth messages." , ex ) ;
60- }
6160
62- return false ;
61+ return false ;
62+ } ) . GetAwaiter ( ) . GetResult ( ) ;
6363 }
6464 }
6565}
0 commit comments