C#.NET Transfer Script.
I'd like to note that MUCH of this code (mostly the hard parts) is based on the examples found in this thread: http://discussions.apple.com/thread.jspa?messageID=2710342�. I've tried to credit the orignal poster in my comments.
protected void Page_Load(object sender, EventArgs e)
{
// Define your site's information. Replace these
// values with ones appropriate for your site.
string debugSuffix = "/your debug suffix";
string sharedSecret = "YOUR SHARED SECRET";
string credential1 = "your credential@urn:mace:itunesu.com:sites:school.edu";
string siteUrl = "https://deimos.apple.com/WebObjects/Core.woa/Browse/school.edu";
// Define the user information. Replace the credentials with the
// credentials you want to grant to that user, and the optional
// identity information with the identity of the current user.
// For initial testing and site setup, use the singe administrator
// credential defined when your iTunes U site was created. Once
// you have access to your iTunes U site, you will be able to define
// additional credentials and the iTunes U access they provide.
string[] credentialArray = {};
string displayName = "Demo";
string emailAddress = "demo@school.edu";
string userName = "demo";
string userIdentifier = "0";
// Append your site's debug suffix to the destination if you
// want to receive an HTML page providing information about
// the transmission of credentials and identity between this
// program and iTunes U. Remove this code after initial
// testing to instead receive the destination page requested.
// uncomment the line below to access debug information from the
// iTunes U server.
//siteUrl = siteUrl + debugSuffix;
//We create the token pieces...
string identity = getIdentityString(displayName, emailAddress, userName, userIdentifier);
string credentials = getCredentialsString(credentialArray);
string currentTime = getCurrentTime();
//...then encode and concatenate them...
string token = "credentials=" + Uri.EscapeDataString(credentials) + "&identity=" + Uri.EscapeDataString(identity) + "&time=" + currentTime;
//...and then perform three replaces because Uri.EscapeDataString doesn't properly encode spaces, "(", or ")"
token = token.Replace("%20", "+");
token = token.Replace("(", "%28");
token = token.Replace(")", "%29");
//Generate the signature string
string signature = "&signature=" + GenerateSignature(token, sharedSecret);
//Pass the token, signature, and url to the actual page request
requestSite(token + signature, siteUrl);
}
//This method creates the time needed for the signature it is based on code originally posted by "Ed at NAIT"
private string getCurrentTime()
{
DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0);
TimeSpan span = DateTime.UtcNow - epoch;
int elapsed = (int)span.TotalSeconds;
return elapsed.ToString();
}
//Concatenate the identity string from the defined user information
private string getIdentityString(string displayName, string emailAddress, string userName, string userIdentifier)
{
string identity = "\"" + displayName + "\" " + "<"(" + userName + ") " + "[" + userIdentifier + "]";
return identity;
}
// concatenates all the passed in credentials into a string
// with a semicolon delimiting the credentials in the string.
private string getCredentialsString(Array credentialArray)
{
string credential = "";
int credentialCount = credentialArray.Length;
for (int i = 0; i < credentialCount; i++)
{
if (i == 0)
{
credential = credentialArray.GetValue(i).ToString();
}
else
{
credential += ";" + credentialArray.GetValue(i).ToString();
}
}
return credential;
}
//This method creates hex encoded signature. It is code originally posted by "Ed at NAIT"
public string GenerateSignature(string token, string sharedSecret)
{
//get the bytes of shared key
byte[] key = System.Text.Encoding.ASCII.GetBytes(sharedSecret);
//Get the HMACSHA256 Object for the shared key
System.Security.Cryptography.HMACSHA256 hmac = new System.Security.Cryptography.HMACSHA256(key);
//calculate the new key
byte[] newKey = hmac.ComputeHash(System.Text.Encoding.ASCII.GetBytes(token));
//return the hex of the new key
return ToHexString(newKey);
}
//This method converts the from ASCII to Hex. It is based on code originally posted by "Ed at NAIT"
public static string ToHexString(byte[] bytes)
{
string hexString = "";
for (int i = 0; i < bytes.Length; i++)
{
hexString += String.Format("{0:x2}", bytes.GetValue(i));
}
return hexString;
}
//This method makes the HTTP request. It is based on code originally posted by "Ed at NAIT"
public void requestSite(string signature, string siteUrl)
{
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(siteUrl);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
using (StreamWriter sw = new StreamWriter(req.GetRequestStream()))
{
//The token is the query string (ie. credentials=foo&identity=bar&time=111&signature=key)
sw.Write(signature);
}
using (WebResponse objResponse = req.GetResponse())
{
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
{
//This will write the html to launch iTunes if succsessful or the error if unsuccessful
Response.Write(sr.ReadToEnd());
}
}
}
}
20" iMac, Mac OS X (10.5.1), Dual Boot w/XP