protected override bool CheckAccessCore(OperationContext operationContext)
        {
            if (!base.CheckAccessCore(operationContext))
            {
                return(false);
            }

            var httpDetails = operationContext.RequestContext.RequestMessage.Properties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
            var requestUri  = operationContext.RequestContext.RequestMessage.Properties.Via;

            try {
                var principal = VerifyOAuth2(httpDetails, requestUri, operationContext.IncomingMessageHeaders.Action ?? operationContext.IncomingMessageHeaders.To.AbsolutePath);
                if (principal != null)
                {
                    var policy   = new OAuthPrincipalAuthorizationPolicy(principal);
                    var policies = new List <IAuthorizationPolicy> {
                        policy,
                    };

                    var securityContext = new ServiceSecurityContext(policies.AsReadOnly());
                    if (operationContext.IncomingMessageProperties.Security != null)
                    {
                        operationContext.IncomingMessageProperties.Security.ServiceSecurityContext = securityContext;
                    }
                    else
                    {
                        operationContext.IncomingMessageProperties.Security = new SecurityMessageProperty {
                            ServiceSecurityContext = securityContext,
                        };
                    }

                    securityContext.AuthorizationContext.Properties["Identities"] = new List <IIdentity> {
                        principal.Identity,
                    };

                    return(true);
                }
                else
                {
                    return(false);
                }
            } catch (ProtocolFaultResponseException ex) {
                Global.Logger.Error("Error processing OAuth messages.", ex);

                // Return the appropriate unauthorized response to the client.
                var outgoingResponse = ex.CreateErrorResponse();
                outgoingResponse.Respond(WebOperationContext.Current.OutgoingResponse);
            } catch (ProtocolException ex) {
                Global.Logger.Error("Error processing OAuth messages.", ex);
            }

            return(false);
        }
		protected override bool CheckAccessCore(OperationContext operationContext) {
			if (!base.CheckAccessCore(operationContext)) {
				return false;
			}

			var httpDetails = operationContext.RequestContext.RequestMessage.Properties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
			var requestUri = operationContext.RequestContext.RequestMessage.Properties.Via;

			return Task.Run(async delegate {
				ProtocolFaultResponseException exception = null;
				try {
					var principal = await VerifyOAuth2Async(
						httpDetails,
						requestUri,
						operationContext.IncomingMessageHeaders.Action ?? operationContext.IncomingMessageHeaders.To.AbsolutePath);
					if (principal != null) {
						var policy = new OAuthPrincipalAuthorizationPolicy(principal);
						var policies = new List<IAuthorizationPolicy> { policy, };

						var securityContext = new ServiceSecurityContext(policies.AsReadOnly());
						if (operationContext.IncomingMessageProperties.Security != null) {
							operationContext.IncomingMessageProperties.Security.ServiceSecurityContext = securityContext;
						} else {
							operationContext.IncomingMessageProperties.Security = new SecurityMessageProperty {
								ServiceSecurityContext = securityContext,
							};
						}

						securityContext.AuthorizationContext.Properties["Identities"] = new List<IIdentity> { principal.Identity, };

						return true;
					} else {
						return false;
					}
				} catch (ProtocolFaultResponseException ex) {
					Global.Logger.Error("Error processing OAuth messages.", ex);
					exception = ex;
				} catch (ProtocolException ex) {
					Global.Logger.Error("Error processing OAuth messages.", ex);
				}

				if (exception != null) {
					// Return the appropriate unauthorized response to the client.
					var outgoingResponse = await exception.CreateErrorResponseAsync(CancellationToken.None);
					this.Respond(WebOperationContext.Current.OutgoingResponse, outgoingResponse);
				}

				return false;
			}).GetAwaiter().GetResult();
		}
		protected override bool CheckAccessCore(OperationContext operationContext) {
			if (!base.CheckAccessCore(operationContext)) {
				return false;
			}

			var httpDetails = operationContext.RequestContext.RequestMessage.Properties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
			var requestUri = operationContext.RequestContext.RequestMessage.Properties.Via;

			try {
				var principal = VerifyOAuth2(httpDetails, requestUri);
				if (principal != null) {
					var policy = new OAuthPrincipalAuthorizationPolicy(principal);
					var policies = new List<IAuthorizationPolicy> {
						policy,
					};

					var securityContext = new ServiceSecurityContext(policies.AsReadOnly());
					if (operationContext.IncomingMessageProperties.Security != null) {
						operationContext.IncomingMessageProperties.Security.ServiceSecurityContext = securityContext;
					} else {
						operationContext.IncomingMessageProperties.Security = new SecurityMessageProperty {
							ServiceSecurityContext = securityContext,
						};
					}

					securityContext.AuthorizationContext.Properties["Identities"] = new List<IIdentity> {
						principal.Identity,
					};

					// Only allow this method call if the access token scope permits it.
					return principal.IsInRole(operationContext.IncomingMessageHeaders.Action);
				} else {
					return false;
				}
			} catch (ProtocolException ex) {
				Global.Logger.Error("Error processing OAuth messages.", ex);
			}

			return false;
		}
示例#4
0
        protected override bool CheckAccessCore(OperationContext operationContext)
        {
            if (!base.CheckAccessCore(operationContext))
            {
                return(false);
            }

            var httpDetails = operationContext.RequestContext.RequestMessage.Properties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
            var requestUri  = operationContext.RequestContext.RequestMessage.Properties.Via;

            return(Task.Run(async delegate {
                ProtocolFaultResponseException exception = null;
                try {
                    var principal = await VerifyOAuth2Async(
                        httpDetails,
                        requestUri,
                        operationContext.IncomingMessageHeaders.Action ?? operationContext.IncomingMessageHeaders.To.AbsolutePath);
                    if (principal != null)
                    {
                        var policy = new OAuthPrincipalAuthorizationPolicy(principal);
                        var policies = new List <IAuthorizationPolicy> {
                            policy,
                        };

                        var securityContext = new ServiceSecurityContext(policies.AsReadOnly());
                        if (operationContext.IncomingMessageProperties.Security != null)
                        {
                            operationContext.IncomingMessageProperties.Security.ServiceSecurityContext = securityContext;
                        }
                        else
                        {
                            operationContext.IncomingMessageProperties.Security = new SecurityMessageProperty {
                                ServiceSecurityContext = securityContext,
                            };
                        }

                        securityContext.AuthorizationContext.Properties["Identities"] = new List <IIdentity> {
                            principal.Identity,
                        };

                        return true;
                    }
                    else
                    {
                        return false;
                    }
                } catch (ProtocolFaultResponseException ex) {
                    Global.Logger.ErrorException("Error processing OAuth messages.", ex);
                    exception = ex;
                } catch (ProtocolException ex) {
                    Global.Logger.ErrorException("Error processing OAuth messages.", ex);
                }

                if (exception != null)
                {
                    // Return the appropriate unauthorized response to the client.
                    var outgoingResponse = await exception.CreateErrorResponseAsync(CancellationToken.None);
                    this.Respond(WebOperationContext.Current.OutgoingResponse, outgoingResponse);
                }

                return false;
            }).GetAwaiter().GetResult());
        }