Пример #1
0
        /// <summary>
        /// Validates the current request
        /// </summary>
        /// <param name="args">An <see cref="ValidateCredentialTokenFunctionArgs"/> instance specifying the location of the client configuration in Azure storage.</param>
        /// <returns>OK with json '{"valid": true || false}'</returns>
        public HttpResponseMessage Execute(ValidateCredentialTokenFunctionArgs args)
        {
            var responseObject = new { valid = false };

            //If the input is bad in any way there will be an error and the response shall be OK with json {"valid":false}
            try
            {
                if (!_queryParams.ContainsKey("token") || !_queryParams.ContainsKey("cId"))
                {
                    throw new Exception();
                }
                var clientConfig = GetConfiguration(_queryParams["cId"]);

                //Will throw if the token can't be decrypted
                var ctx = CredentialedSharePointConnectionInfo.GetSharePointClientContext(_queryParams["token"],
                                                                                          clientConfig.CredentialedClientConfig.Password, clientConfig.CredentialedClientConfig.Salt);

                ctx.Load(ctx.Web);

                //Will throw if the credentials are no good
                ctx.ExecuteQuery();
                responseObject = new { valid = true };
            }
            catch
            {
                //ignored - if there is an exception the request is simply not valid
            }
            _response.StatusCode = HttpStatusCode.OK;
            _response.Content    = new StringContent((new JavaScriptSerializer()).Serialize(responseObject));
            _response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            return(_response);
        }
        /// <summary>
        /// Initializes the handler for a given HttpRequestMessage received from the function trigger
        /// </summary>
        /// <param name="request">The current request</param>
        public CreateCredentialTokenHandler(HttpRequestMessage request)
        {
            try
            {
                //Inexplicably this test fails when receiving from JavaScript (angular) instead
                //I can clearly see if I debug that the header is application/json,
                //but the Equals test fails
                //So, just try the operation, the end result is the same either way
                //TODO: Figure this out!

                //if (request.Content.Headers.ContentType.Equals(new MediaTypeHeaderValue("application/json")))
                //{
                var body = request.Content.ReadAsStringAsync().Result;
                _credentialedSharePointConnectionInfo =
                    (new JavaScriptSerializer()).Deserialize <CredentialedSharePointConnectionInfo>(body);
                //}
            }
            catch
            {
                //ignored
            }
            _response = request.CreateResponse();
        }