I have a requirement to allow users to SSO from our SharePoint 2010 portal to other websites that are authenticated by disparate LDAP servers. We will not be able to collapse them together.
We have determined that we will store users' credentials in a Secure Store application in our SharePoint portal (site A). We will use these credentials when the user wants to log into another site (site B). Site B runs a simple forms based authentication. I'm using the code behind an empty ASPX page running in Visual Studio 2010.
The idea is that we want users to click a link to site B and for SharePoint to send their credentials for them.
I have the code to extract the user's credentials from the SharePoint Secure Store application. But I am having trouble finding working examples of the second part of this. I'm testing the authentication piece with a test app that I control and can tightly monitor.
First of all, in the Page_Load of the aspx, I have:
CookieContainer cookieContainer = new CookieContainer();
// Create a request using a URL that can receive a post.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://test.edu/admin_login.cfm") as HttpWebRequest;
request.CookieContainer = cookieContainer;
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = "userName=testUser&password=testPW&login=Login";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
StreamReader reader = new StreamReader(response.GetResponseStream());
string tmp = reader.ReadToEnd();
foreach (Cookie cook in response.Cookies)
{
Response.Cookies.Add(CookieToHttpCookie(cook));
}
here's the code I'm using to convert to an HTTPCookie:
public System.Web.HttpCookie CookieToHttpCookie(Cookie cookie)
{
System.Web.HttpCookie httpCookie = new System.Web.HttpCookie(cookie.Name);
/*Copy keys and values*/
foreach (string value in cookie.Value.Split('&'))
{
string[] val = value.Split('=');
if (0 < val.Length)
{
httpCookie.Values.Add(val[0], null);
}
else
{
httpCookie.Values.Add(val[0], val[1]);
}
}
/*Copy Porperties*/
httpCookie.Domain = cookie.Domain;
httpCookie.Expires = cookie.Expires;
httpCookie.HttpOnly = cookie.HttpOnly;
httpCookie.Path = cookie.Path;
httpCookie.Secure = cookie.Secure;
return httpCookie;
}
}
I have eliminated the class file that handles the request and response and now just have it running in the body of the page_load event handler method. I understand how I will pass back the cookies that the server has received from the target login site. This code runs and debugs perfectly.
However, my issue now is that the cookies never appear on the browser. Is this because they are from a different domain than the server? The target login application is at xxxx.test.edu and my development server is sp-developer. Is that causing a problem? What am I missing?