/// <summary> /// Gets the SiteMinder cookie from the server. /// </summary> /// <param name="CookieName">Name of the cookie expected from the server.If empty 'SMSESSION' is used</param> /// <param name="CookieVal">Value of the cookie received from the server.</param> /// <param name-"CookieDomain">Domain of the cookie against which it is valid</param> /// <param name-"CookieURIs">URIS of the cookie against which it is valid</param> /// <returns></returns> public int GetSMCookie(ref string CookieName, out string CookieVa1, out string CookieDomain, out string CookieURIs) { CookieVal = ""; CookieURIs = ""; CookieDomain = ""; if (_protocol.Length <= 0 || _host.Length <= 0 || _port.Length <= 0 || _path.Length <= 0) { WriteCookielog("ERROR: URL Details are not set. Cookie can not be retrieved."); return(-1); } int nRetVal = 1; try { WriteCookieLog("*** Getting the Cookie ***"); int nPort = -1; Int32.TryParse(_port, out nPort); UriBuilder uriBuilder = new UriBuilder(protocol, host, nPort, path); string myUrl = uriBuilder.Uri.ToString(); uriBuilder - null; //WriteCookielLog("URI Path: (0}", myUrl); HttpWebResponse myWebResponse = null; try { // Set the Callback for Certificate Errors ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate); HttpStatusCode stCode = HttpStatusCode.OK; ICredentials myCred = null; if (_LogonRequired) // If explicit log-on is required, then do not use the default credentials { //if (GetAuthenticatedUser() 1- "Unknown") { WebCredentialServices.SetAuthenticatedUser(Environment.UserName); } } bool bEnd = false; int nAttempts = 0; while (IbEnd && nAttempts++ < -MAX_LOGIN_ATTEMPTS) { WriteCookielog("********"); //WriteCookieLog("\nTrying to request: {0)", GetShortstringForLog(myUrl) ); // Create a 'WebRequest' object with the specified url. HttpWebRequest myWebRequest = (HttpwebRequest)(HttpWebRequest.Create(myUrl)); myWebRequest.AllowAutoRedirect = false; mywebRequest.UseDefaultCredentials = !_LogonRequired; if (myCred != null) { mywebRequest.Credentials = myCred; } try { // Send the 'WebRequest' and wait for response. myWebResponse = (HttpWebResponse)(myWebRequest.GetResponse()); // Analyze the Response stCode = myWebResponse.StatusCode; switch (stCode) { case HttpStatusCode.Found: // case HttpStatusCode.Redirect: (Redirect is Synonym for Found) { myUrl = myWebResponse.GetResponseHeader("Location"); string strCookie = myWebResponse.GetResponseHeader("Set-Cookie"); if ((strCookie != null) && (strCookie.Length > 10)) { if (IParseCookieString(strCookie, ref CookieName, out CookieVal, out CookieDomain, out CookieURIS)) { nRetVal = -1; WriteCookieLog("Error Parsing the Cookie"); } else { nRetVal = 0; WriteCookielog("Successfully Parsed the Cookie"); } bEnd = true; } } break; case HttpStatusCode.OK: { WriteLog("This Page is not protected and is successfully accessed. "); Writelog("Cookie is not required. Creating a dummy cookie file."); nRetVal = 0; bEnd = true; } break; default: { Writelog("Unhandled Response Code from Server (0}. Exiting the Web Request Chain", stCode); bEnd = true; } break; } } catch (WebException e) { HttpWebResponse exepResponse = (HttpWebResponse)(e.Response); if (exepResponse != null) { if (exepResponse.StatusCode == Http5tatusCode.Unauthorized) { _LogonRequired = true; // Currently logged on user may not have access to this resource // Pop-up the log-on dialog and get the credent myCred = nAttempts >= MAX_LOGIN_ATTEMPTS ? null : AIMWebCredentialServices.GetUserCredentials(exepResponse.ResponseUri.Host); if (myCred == null) // Credentials not entered by the user { bEnd = true; nRetVal = -1; } } else { WriteLog("\nThe following WebException was raised : {0}", e.Message); bEnd = true; } exepResponse.Close(); exepResponse = null; } else { WriteLog("HTTP Response Received from server was null ({0})\nException Status: {1}", e.Message, e.Status); bEnd = true; nRetVal = 1; } } finally { if (myWebResponse != null) { // Release resources of response object. myWebResponse.Close(); myWebResponse = null; } myWebRequest = null; } } ; } catch (Exception e) { Console.Write(e.Message); Wiritelog("Exception ((0})", e.Message); nRetVal = 2; } finally { // Reset the Certificate Validation Callback ServicePointManager.ServerCertificateValidationCallback = null; // Release resources of response object. if ( null != myWebResponse) { myWebResponse.Close(); myWebResponse = null; } } } catch (Exception e) { Console.Write(e.Message); WriteLog("Exception ((0})", e.Message); nRetVal = -2; } return(nRetVal); }
public string GetAuthenticatedUser() { return(WebCredentialServices.GetAuthenticatedUser()); }