internal CookieContainer GetLogonCookie(string baseUrl)
        {
            // Create an instance of the loginWindows object.
            SvcLoginWindows.LoginWindows loginWindows = new SvcLoginWindows.LoginWindows();
            //http://epmprod03/escritoriodeprojetos/_vti_bin/PSI/LoginWindows.asmx?wsdl
            loginWindows.Url         = baseUrl + "/_vti_bin/PSI/LoginWindows.asmx";
            loginWindows.Credentials = CredentialCache.DefaultCredentials;

            loginWindows.CookieContainer = new CookieContainer();

            if (!loginWindows.Login())
            {
                // Login failed; throw an exception.
                throw new UnauthorizedAccessException("Login failed.");
            }
            return(loginWindows.CookieContainer);
        }
Esempio n. 2
0
        public static bool P14Login(string projectserverURL)
        {
            bool endPointError = false;
            bool result = false;

            try
            {
                projectServerUrl = projectserverURL.Trim();

                if (!projectServerUrl.EndsWith("/"))
                {
                    projectServerUrl = projectServerUrl + "/";
                }
                String baseUrl = projectServerUrl;

                // Configure the WCF endpoints of PSI services used in ProjTool, before logging on.
                if (mySettings.UseAppConfig)
                {
                    endPointError = !ConfigClientEndpoints();
                }
                else
                {
                    endPointError = !SetClientEndpointsProg(baseUrl);
                }

                if (endPointError) return false;

                // NOTE: Windows logon with the default Windows credentials, Forms logon, and impersonation work in ProjTool.
                // Windows logon without the default Windows credentials does not currently work.
                if (!isImpersonated)
                {
                    if (isWindowsAuth)
                    {
                        if (useDefaultWindowsCredentials)
                        {
                            result = true;
                        }
                        else
                        {
                            String[] splits = userName.Split('\\');

                            if (splits.Length != 2)
                            {
                                String errorMessage = "User name must be in the format domainname\\accountname";
                                result = false;
                            }
                            else
                            {
                                // Order of strings returned by String.Split is not deterministic
                                // Hence we cannot use splits[0] and splits[1] to obtain domain name and user name

                                int positionOfBackslash = userName.IndexOf('\\');
                                String windowsDomainName = userName.Substring(0, positionOfBackslash);
                                String windowsUserName = userName.Substring(positionOfBackslash + 1);

                                loginWindows = new SvcLoginWindows.LoginWindows();
                                loginWindows.Url = baseUrl + "_vti_bin/PSI/LoginWindows.asmx";
                                loginWindows.Credentials = new NetworkCredential(windowsUserName, password, windowsDomainName);
                                Utility.WriteLog(string.Format("Logging in with username={0} password={1} Domain={1} ", windowsUserName, password, windowsDomainName), System.Diagnostics.EventLogEntryType.Information);
                                result = loginWindows.Login();
                                Utility.WriteLog(string.Format("Logging in result ={0} ", result.ToString()), System.Diagnostics.EventLogEntryType.Information);
                            }
                        }
                    }
                    else
                    {
                        // Forms authentication requires the Authentication web service in Microsoft SharePoint Foundation.
                        Utility.WriteLog(string.Format("Logging in with username={0} password={1}  ", userName, password), System.Diagnostics.EventLogEntryType.Information);
                        result = WcfHelpers.LogonWithMsf(userName, password, new Uri(baseUrl));
                        Utility.WriteLog(string.Format("Logging in result ={0} ", result.ToString()), System.Diagnostics.EventLogEntryType.Information);
                    }
                }
                return result;
            }
            catch (Exception ex)
            {
                return false;
            }
        }