Пример #1
0
        /// <summary>
        /// 配置应用程序
        /// </summary>
        public void Configure(IApplicationBuilder appBuilder)
        {
            //配置WCF服务
            ServiceMetadataBehavior metadataBehavior = appBuilder.ApplicationServices.GetRequiredService <ServiceMetadataBehavior>();

            metadataBehavior.HttpGetEnabled  = true;
            metadataBehavior.HttpsGetEnabled = true;
            UseRequestHeadersForMetadataAddressBehavior addressBehavior = new UseRequestHeadersForMetadataAddressBehavior();
            DependencyInjectionBehavior dependencyInjectionBehavior     = new DependencyInjectionBehavior();
            InitializationBehavior      initializationBehavior          = new InitializationBehavior();
            IList <IServiceBehavior>    serviceBehaviors = new List <IServiceBehavior>
            {
                addressBehavior, dependencyInjectionBehavior, initializationBehavior
            };

            if (AspNetSetting.Authorized)
            {
                AuthenticationBehavior authenticationBehavior = new AuthenticationBehavior();
                serviceBehaviors.Add(authenticationBehavior);
            }

            appBuilder.UseServiceModel(builder =>
            {
                builder.ConfigureServiceHostBase <AuthenticationContract>(host => host.Description.Behaviors.AddRange(serviceBehaviors));
                builder.ConfigureServiceHostBase <AuthorizationContract>(host => host.Description.Behaviors.AddRange(serviceBehaviors));
                builder.ConfigureServiceHostBase <UserContract>(host => host.Description.Behaviors.AddRange(serviceBehaviors));
            });
        }
Пример #2
0
        public void Should_exclude_diagnostics_pages_if_configured(
            [Values(true, false)] bool excludeDiagnostics)
        {
            var actionDescriptor = new ActionDescriptorFactory(_configuration, null, new TypeCache())
                                   .CreateDescriptor(ActionMethod.From <GraphiteHandler>(x => x.Post()), null);
            var behavior = new AuthenticationBehavior(_behaviorChain, _requestMessage,
                                                      _responseMessage, _authenticators, _configuration, actionDescriptor);

            _configuration.ExcludeDiagnosticsFromAuthentication = excludeDiagnostics;

            behavior.ShouldRun().ShouldEqual(!excludeDiagnostics);
        }
Пример #3
0
 public void Setup()
 {
     _authenticators           = new List <IAuthenticator>();
     _basicAuthenticator       = new TestBasicAuthenticator("fark", "farker");
     _bearerTokenAuthenticator = new TestBearerTokenAuthenticator("fark");
     _configuration            = new Configuration();
     _requestMessage           = new HttpRequestMessage();
     _responseMessage          = new HttpResponseMessage(HttpStatusCode.OK);
     _behaviorChain            = Substitute.For <IBehaviorChain>();
     _behaviorChain.InvokeNext().Returns(_responseMessage);
     _actionDescriptor = new ActionDescriptorFactory(_configuration, null, new TypeCache())
                         .CreateDescriptor(null, null);
     _behavior = new AuthenticationBehavior(_behaviorChain, _requestMessage,
                                            _responseMessage, _authenticators, _configuration, _actionDescriptor);
 }
Пример #4
0
        /// <summary>
        /// Gets the olsa client
        /// </summary>
        /// <param name="olsaServerEndpoint">The olsa server endpoint.</param>
        /// <param name="olsaCustomerId">The olsa customer identifier.</param>
        /// <param name="olsaSharedSecret">The olsa shared secret.</param>
        /// <returns></returns>
        private static OlsaPortTypeClient GetOLSAClient(Uri olsaServerEndpoint, string olsaCustomerId, string olsaSharedSecret)
        {
            //Set the encoding to SOAP 1.1, Disable Addressing and set encoding to UTF8
            TextMessageEncodingBindingElement messageEncoding = new TextMessageEncodingBindingElement();

            messageEncoding.MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.Soap11, AddressingVersion.None);
            messageEncoding.WriteEncoding  = Encoding.UTF8;

            //Setup Binding Elemment
            HttpTransportBindingElement transportBinding = new HttpsTransportBindingElement();

            //Set the maximum received messages sizes to 1Mb
            transportBinding.MaxReceivedMessageSize = 1024 * 1024;
            transportBinding.MaxBufferPoolSize      = 1024 * 1024;

            //Create the CustomBinding
            Binding customBinding = new CustomBinding(messageEncoding, transportBinding);

            //Create the OLSA Service
            EndpointAddress serviceAddress = new EndpointAddress(olsaServerEndpoint);

            //Set the endPoint URL YOUROLSASERVER/olsa/services/Olsa has to be HTTPS
            OlsaPortTypeClient service = new OlsaPortTypeClient(customBinding, serviceAddress);

            //Add Behaviour to support SOAP UserNameToken Password Digest
            AuthenticationBehavior behavior1 = new AuthenticationBehavior(olsaCustomerId, olsaSharedSecret);

            service.Endpoint.Behaviors.Add(behavior1);

            //Add Behaviour to support fix of Namespaces to address AXIS2 / VWCF incompatability
            NameSpaceFixUpBehavior behavior2 = new NameSpaceFixUpBehavior();

            service.Endpoint.Behaviors.Add(behavior2);

            return(service);
        }
Пример #5
0
        /// <summary>
        /// Initiates the asset meta data.
        /// </summary>
        /// <param name="client">The client.</param>
        /// <param name="metadataFormat">The metadata format.</param>
        /// <param name="initiationMode">The initiation mode.</param>
        /// <param name="onsuccessclose">if set to <c>true</c> [onsuccessclose].</param>
        /// <returns></returns>
        /// <exception cref="AIPOC.Exceptions.OlsaSecurityException"></exception>
        public static string AI_InitiateAssetMetaData(OlsaPortTypeClient client, assetMetadataFormat metadataFormat, assetInitiationMode initiationMode, bool onsuccessclose = false)
        {
            HandleResponse handleResponse = new HandleResponse();

            try
            {
                log.InfoFormat("Sending AI_InitiateAssetMetadata Request. Format: {0} Mode: {1}", metadataFormat, initiationMode);
                InitiateAssetMetaDataRequest request = new InitiateAssetMetaDataRequest();

                //Pull the OlsaAuthenticationBehviour so we can extract the customerid
                AuthenticationBehavior olsaCredentials = (AuthenticationBehavior)client.ChannelFactory.Endpoint.Behaviors.Where(p => p.GetType() == typeof(AuthenticationBehavior)).FirstOrDefault();
                request.customerId     = olsaCredentials.UserName;
                request.initiationMode = initiationMode;
                request.metadataFormat = metadataFormat;

                handleResponse = client.AI_InitiateAssetMetaData(request);
            }
            catch (WebException)
            {
                // This captures any Web Exepctions such as DNS lookup errors, HTTP status errors such as 404, proxy errors etc
                // See http://msdn.microsoft.com/en-us/library/48ww3ee9(VS.80).aspx
                throw;
            }
            catch (TimeoutException)
            {
                //This captures the WCF timeout exception
                throw;
            }
            //Olsa.GeneralFault exception will be thrown for issues like parameters invalid, user does could not be created etc
            catch (FaultException <Olsa.GeneralFault> )
            {
                throw;
            }
            //WCF fault exception will be thrown for any other issues such as Security
            catch (FaultException fe)
            {
                if (fe.Message.ToLower(CultureInfo.InvariantCulture).Contains("the security token could not be authenticated or authorized"))
                {
                    //The OLSA Credentials specified could not be authenticated
                    //Check the values in the web.config are correct for OLSA.CustomerID and OLSA.SharedSecret - these are case sensitive
                    //Check the time on the machine, the SOAP message is valid for 5 minutes. This means that if the time on the calling machine
                    //is to slow OR to fast then the SOAP message will be invalid.
                    throw new Exceptions.OlsaSecurityException();
                }
                throw;
            }
            catch (CommunicationException)
            {
                throw;
            }
            catch (Exception)
            {
                //Any other type of exception, perhaps out of memory
                throw;
            }
            finally
            {
                if (client != null)
                {
                    if (client.State == CommunicationState.Faulted)
                    {
                        client.Abort();
                    }
                    if (onsuccessclose)
                    {
                        client.Close();
                    }
                }
            }
            return(handleResponse.handle);
        }
Пример #6
0
        /// <summary>
        /// Pollfors the report.
        /// </summary>
        /// <param name="reportId">The report identifier.</param>
        /// <param name="client">The client.</param>
        /// <param name="closeclient">if set to <c>true</c> [closeclient].</param>
        /// <returns></returns>
        public static UrlResponse PollforReport(string reportId, OlsaPortTypeClient client, bool closeclient = false)
        {
            //Set up our response object
            UrlResponse response = null;

            try
            {
                //Create our request
                PollForReportRequest request = new PollForReportRequest();

                //Pull the OlsaAuthenticationBehviour so we can extract the customerid
                AuthenticationBehavior olsaCredentials = (AuthenticationBehavior)client.ChannelFactory.Endpoint.Behaviors.Where(p => p.GetType() == typeof(AuthenticationBehavior)).FirstOrDefault();
                request.customerId = olsaCredentials.UserName;

                request.reportId = reportId;

                response = client.UTIL_PollForReport(request);
            }
            catch (WebException)
            {
                // This captures any Web Exceptions such as proxy errors etc
                // See http://msdn.microsoft.com/en-us/library/48ww3ee9(VS.80).aspx
                throw;
            }
            catch (TimeoutException)
            {
                //This captures the WCF timeout exception
                throw;
            }
            //WCF fault exception will be thrown for any other issues such as Security
            catch (FaultException fe)
            {
                if (fe.Message.ToLower(CultureInfo.InvariantCulture).Contains("the security token could not be authenticated or authorized"))
                {
                    //The OLSA Credentials specified could not be authenticated
                    throw;
                }
                throw;
            }
            catch (Exception)
            {
                //Any other type of exception, perhaps out of memory
                throw;
            }
            finally
            {
                //Shutdown and dispose of the client
                if (client != null)
                {
                    if (client.State == CommunicationState.Faulted)
                    {
                        client.Abort();
                    }
                    if (closeclient)
                    {
                        //We cannot resue client if we close
                        client.Close();
                    }
                }
            }
            return(response);
        }