/// <summary>
        /// Utility method for getting the authenticated who from the header.
        /// </summary>
        public static IAuthenticatedWho GetWho(String authorizationHeader)
        {
            IAuthenticatedWho authenticatedWho = null;

            // Check to see if it's null - it can be in some situations
            if (authorizationHeader != null &&
                authorizationHeader.Trim().Length > 0)
            {
                // Deserialize into an object
                authenticatedWho = AuthenticationUtils.Deserialize(Uri.EscapeDataString(authorizationHeader));
            }

            return(authenticatedWho);
        }
        private IAuthenticatedWho GetWho()
        {
            IAuthenticatedWho authenticatedWho    = null;
            String            authorizationHeader = null;

            // Get the authorization header from this invoke request
            authorizationHeader = System.Web.HttpContext.Current.Request.Headers[HttpUtils.HEADER_AUTHORIZATION];

            if (authorizationHeader == null)
            {
                throw new ArgumentNullException("BadRequest", "Not authorized to invoke this service.");
            }

            // Check to make sure the authorization header parses OK
            authenticatedWho = AuthenticationUtils.Deserialize(HttpUtility.UrlDecode(authorizationHeader));

            return(authenticatedWho);
        }
Exemplo n.º 3
0
        /// <summary>
        /// This method allows you to login as an author of flows.
        /// </summary>
        public IAuthenticatedWho Login(INotifier notifier, String tenantId, String manywhoBaseUrl, AuthenticationCredentialsAPI authenticationCredentials)
        {
            String              authorizationToken  = null;
            IAuthenticatedWho   authenticatedWho    = null;
            HttpClient          httpClient          = null;
            HttpContent         httpContent         = null;
            HttpResponseMessage httpResponseMessage = null;
            String              endpointUrl         = null;

            Policy.Handle <ServiceProblemException>().Retry(HttpUtils.MAXIMUM_RETRIES).Execute(() =>
            {
                using (httpClient = HttpUtils.CreateHttpClient(null, tenantId, null))
                {
                    // Use the JSON formatter to create the content of the request body.
                    httpContent = new StringContent(JsonConvert.SerializeObject(authenticationCredentials));
                    httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                    // Construct the URL for the describe request
                    endpointUrl = manywhoBaseUrl + DrawSingleton.MANYWHO_DRAW_URI_PART_LOGIN;

                    // Send the describe request over to the remote service
                    httpResponseMessage = httpClient.PostAsync(endpointUrl, httpContent).Result;

                    // Check the status of the response and respond appropriately
                    if (httpResponseMessage.IsSuccessStatusCode)
                    {
                        // Get the describe response object from the response message
                        authorizationToken = httpResponseMessage.Content.ReadAsStringAsync().Result;

                        // Trim the quotes from the JSON response token
                        authorizationToken = authorizationToken.Substring(1, authorizationToken.Length - 2);

                        // Deserialize the token back to an authenticated who object
                        authenticatedWho = AuthenticationUtils.Deserialize(Uri.UnescapeDataString(authorizationToken));
                    }
                    else
                    {
                        throw new ServiceProblemException(new ServiceProblem(endpointUrl, httpResponseMessage, string.Empty));
                    }
                }
            });

            return(authenticatedWho);
        }