Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
/// <summary>
/// Calculate the data for a basic LTI 1.x request.
/// </summary>
/// <param name="assignment">The Assignment to be launched.</param>
/// <returns>An OAuthParameters object which includes the required paremters
/// for an LTI 1.x request.</returns>
private OAuthParameters BuildBaseLtiRequestData(Assignment assignment)
const string lti_version = "LTI-1p0";
const string lti_message_type = "basic-lti-launch-request";
const string oauth_callback = "about:blank";
const string oauth_signature_method = "HMAC-SHA1";
const string oauth_version = "1.0";
// First I calculate some values that I will need to sign the request
// with OAuth.Net.
var ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
var timestamp = Convert.ToInt64(ts.TotalSeconds).ToString();
var nonce = Guid.NewGuid().ToString("N");
var parameters = new OAuthParameters();
parameters.Callback = oauth_callback;
parameters.ConsumerKey = assignment.ConsumerKey;
parameters.Nonce = nonce;
parameters.SignatureMethod = oauth_signature_method;
parameters.Timestamp = timestamp;
parameters.Version = oauth_version;
// Now add LTI specific parameters, starting with any
// parameters that were included in the URL (the LTI spec says
// to include these when the signature is calculated).
var uri = new Uri(assignment.Url);
// LTI Header: These identify the request as being an LTI request
parameters.AdditionalParameters.Add("lti_message_type", lti_message_type);
parameters.AdditionalParameters.Add("lti_version", lti_version);
// Resource: These parameters identify the resource. In K-12, a resource is
// equivalent to assignment and the resource_link_id must be unique to each
// context_id (remember that context is equivalent to course or class). In
// this sample, every user has their own course/class/context, so I simply
// concatenate the class id with the assignment id to form the resource_link_id.
var user = db.Users.Find(WebSecurity.CurrentUserId);
var course = new Course(user);
parameters.AdditionalParameters.Add("resource_link_id", string.Format("{0}-{1}",
course.Id, assignment.AssignmentId));
// Note that the title is recommend, but not required.
parameters.AdditionalParameters.Add("resource_link_title", assignment.Name);
return parameters;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment