public void ProcessRequest(HttpContext context)
{
ICasPrincipal user = context.User as ICasPrincipal;
if (user == null)
throw new HttpException(500, "HttpContext.Current.User is null. Check that the DotNetCasClient is mapped and configured correctly in <web.conf>");
// Retrieve a Proxy Ticket for ClearPass
string proxyTicket = CasAuthentication.GetProxyTicketIdFor(ClearPassUrl);
if (log.IsDebugEnabled)
log.Debug("Proxy ticket received for clearpass: " + proxyTicket);
// Get the Password from ClearPass
string clearPassRequest = ClearPassUrl + "?" + ArtifactParameterName + "=" + proxyTicket + "&" + ServiceParameterName + "=" + ClearPassUrl;
string clearPassResponse;
try
{
using (StreamReader reader = new StreamReader(new WebClient().OpenRead(clearPassRequest)))
clearPassResponse = reader.ReadToEnd();
}
catch (Exception ex)
{
throw new HttpException(500, "Error getting response from clearPass at URL: " + clearPassRequest + ". " + ex.Message, ex);
}
string clearPass = XmlUtils.GetTextForElement(clearPassResponse, "cas:credentials");
if (String.IsNullOrEmpty(clearPass))
throw new HttpException(500, "Received response from " + clearPassRequest + ", but cas:credientials IsNullOrEmpty. Check CAS server logs for errors. Make sure SSL certs are trusted.");
// POST username/password to owaauth.dll to get sessionid and cadata cookies
var owaAuthFormFields = "destination=" + OwaUrl
+ "&username=" + user.Identity.Name
+ "&password=" + HttpUtility.UrlEncode(clearPass, Encoding.ASCII)
+ OwaOptionalFormFields;
byte[] postData = Encoding.UTF8.GetBytes(owaAuthFormFields);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(OwaUrl + OwaAuthPath);
request.AllowAutoRedirect = false;
request.CookieContainer = new CookieContainer();
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postData.Length;
request.UserAgent = "Mozilla/5.0+(compatible;+MSIE+9.0;+Windows+NT+6.1;+WOW64;+Trident/5.0)";
try
{
using (Stream requestStream = request.GetRequestStream())
requestStream.Write(postData, 0, postData.Length);
}
catch (Exception ex)
{
if (log.IsErrorEnabled)
log.Error(ex.Message, ex);
throw new HttpException(500, "Error POSTing Auth Form to " + OwaUrl + OwaAuthPath + ". " + ex.Message, ex);
}
try
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
if (log.IsDebugEnabled)
log.Debug("# of OWA cookies received: " + response.Cookies.Count);
// Send sessionid and cadata cookies back to the browser and redirect to Owa
foreach (Cookie cookie in response.Cookies)
context.Response.Cookies.Add(new HttpCookie(cookie.Name, cookie.Value));
string redirectUrl;
if (String.IsNullOrEmpty(OwaInboxUrl))
redirectUrl = response.GetResponseHeader("Location");
else
redirectUrl = OwaInboxUrl;
if (log.IsDebugEnabled)
log.Debug("Added all auth cookies. Redirecting to " + redirectUrl);
context.Response.Redirect(redirectUrl);
}
}
catch (Exception ex)
{
if (log.IsErrorEnabled)
log.Error(ex.Message, ex);
throw new HttpException(500, "Error getting Response from " + OwaUrl + OwaAuthPath + ". " + ex.Message, ex);
}
}