예제 #1
0
        private IdcrlHeader ParseIdcrlHeader(string headerValue, Uri url, HttpStatusCode statusCode, string allResponseHeaders, bool alwaysThrowOnFailure)
        {
            if (string.IsNullOrWhiteSpace(headerValue))
            {
                this._Logger?.LogWarning("IDCRL header value is empty");
                if (alwaysThrowOnFailure)
                {
                    throw new NotSupportedException($"SharePoint ClientCredentials are NOT supported {url.OriginalString} {statusCode} {allResponseHeaders}.");
                }
                return(null);
            }
            IdcrlHeader idcrlHeader = new IdcrlHeader();

            string[] array = headerValue.Split(',');
            foreach (string text in array)
            {
                string   text2  = text.Trim();
                string[] array2 = text2.Split('=');
                if (array2.Length == 2)
                {
                    array2[0] = array2[0].Trim().ToUpperInvariant();
                    array2[1] = array2[1].Trim(' ', '"');
                    switch (array2[0])
                    {
                    case IdcrlConstants.IDCRL_PARAM_IDCRL_TYPE /*"IDCRL TYPE"*/:
                        idcrlHeader.IdcrlType = array2[1];
                        break;

                    case IdcrlConstants.IDCRL_PARAM_ENDPOINT /*"ENDPOINT"*/:
                        idcrlHeader.Endpoint = array2[1];
                        break;

                    case IdcrlConstants.IDCRL_PARAM_ROOTDOMAIN /* "ROOTDOMAIN" */:
                        idcrlHeader.ServiceTarget = array2[1];
                        break;

                    case IdcrlConstants.IDCRL_PARAM_POLICY /* "POLICY" */:
                        idcrlHeader.ServicePolicy = array2[1];
                        break;
                    }
                }
            }
            if (idcrlHeader.IdcrlType != IdcrlConstants.IDCRLTYPE_BPOSIDRL ||
                string.IsNullOrEmpty(idcrlHeader.ServicePolicy) ||
                string.IsNullOrEmpty(idcrlHeader.ServiceTarget) ||
                string.IsNullOrEmpty(idcrlHeader.Endpoint))
            {
                this._Logger?.LogWarning("Cannot extract required information from IDCRL header. Header={0}, IdcrlType={1}, ServicePolicy={2}, ServiceTarget={3}, Endpoint={4}", headerValue, idcrlHeader.IdcrlType, idcrlHeader.ServicePolicy, idcrlHeader.ServiceTarget, idcrlHeader.Endpoint);
                if (alwaysThrowOnFailure)
                {
                    throw new ClientRequestException($"Invalid IDCRL Header {url.OriginalString}, {headerValue}, {statusCode}, {allResponseHeaders}");
                }
                idcrlHeader = null;
            }
            return(idcrlHeader);
        }
예제 #2
0
        public async Task <string> GetAuthenticationCookieAsync(Uri url, string username, string password, bool alwaysThrowOnFailure, EventHandler <WebRequestEventArgs> executingWebRequest)
        {
            if (url == (Uri)null)
            {
                throw new ArgumentNullException("url");
            }
            if (string.IsNullOrEmpty(username))
            {
                throw new ArgumentNullException("username");
            }
            if (password == null)
            {
                throw new ArgumentNullException("password");
            }
            IdcrlHeader idcrlHeader = this.GetIdcrlHeader(url, alwaysThrowOnFailure, executingWebRequest);

            if (idcrlHeader == null)
            {
                this._Logger?.LogWarning("Cannot get IDCRL header for {0}", url);
                if (alwaysThrowOnFailure)
                {
                    throw new ClientRequestException($"CannotContactSite {url}");
                }
                return(null);
            }
#if UseRegistry
            IdcrlEnvironment env       = (IdcrlEnvironment)((string.Compare(IdcrlServiceEnvironment, "INT-MSO", StringComparison.OrdinalIgnoreCase) == 0) ? 1 : (string.Equals(IdcrlServiceEnvironment, "PPE-MSO", StringComparison.OrdinalIgnoreCase) ? 2 : 0));
            IdcrlAuth        idcrlAuth = new IdcrlAuth(env, executingWebRequest, this._Logger);
#else
            IdcrlAuth idcrlAuth = new IdcrlAuth(executingWebRequest, this._Logger);
#endif
            string serviceToken = await idcrlAuth.GetServiceTokenAsync(username, password, idcrlHeader.ServiceTarget, idcrlHeader.ServicePolicy);

            if (string.IsNullOrEmpty(serviceToken))
            {
                this._Logger?.LogWarning("Cannot get IDCRL ticket for username {0}", username);
                if (alwaysThrowOnFailure)
                {
                    throw new IdcrlException("PPCRL_REQUEST_E_UNKNOWN  -2147186615");
                }
                return(null);
            }
            return(this.GetCookie(url, idcrlHeader.Endpoint, serviceToken, alwaysThrowOnFailure, executingWebRequest));
        }