protected void processLicense(VerifyEntitlementTokenResponse verifiedLicense) { SPAppLicenseType licenseType = SPAppLicenseHelper.GetLicenseTypeFromLicense(verifiedLicense); switch (licenseType) { case SPAppLicenseType.Trial: //Do something for a valid trial besides presenting a message on the UI if (SPAppLicenseHelper.GetRemainingDays(verifiedLicense) > 0) { //Valid trial //The UI string retrieved above will already contain the appropiate info //In real app code you could take additional steps (we encourage trials to be fully featured) //Helper code will return int.MaxValue for an unlimited trial } else { //Expired trial //The UI string retrieved above will already contain the appropiate info //In real app code you could take additional steps (e.g. provide reduced functionality) } break; case SPAppLicenseType.Paid: //Do something for a paid app break; case SPAppLicenseType.Free: //Do something for a free app break; default: throw new Exception("Unknown License Type"); } }
/// <summary> /// Generates a TEST token. Note that the token returned will always contain a hardcoded AssetId that points to the Cheezburguers App /// </summary> /// <param name="licenseType">The type of license to generate</param> /// <param name="productId">The productId of the app</param> /// <param name="userLimit">Number of users for the license; 0 for unlimited</param> /// <param name="expirationDays">Number of days the license will be valid for. -1 for unlimited</param> /// <param name="purchaserId">Id of the purchaser; random GUID</param> public static string GenerateTestToken(SPAppLicenseType licenseType, String productId, String userLimit, int expirationDays, String purchaserId) { //Note that the AssetId matches that of the Cheezburgers app on the marketplace. //This is just for TEST purposes so that the storefront URL takes you to a valid app page string hardCodedBaseToken = "<r v=\"0\"><t aid=\"WA103524926\" did=\"{3F47392A-2308-4FC6-BF24-740626612B26}\" ad=\"2012-06-19T21:48:56Z\" te=\"2112-07-15T23:47:42Z\" sd=\"2012-02-01\" test=\"true\"/><d>449JFz+my0wNoCm0/h+Ci9DsF/W0Q8rqEBqjpe44KkY=</d></r>"; string tokenXml = hardCodedBaseToken; tokenXml = AddAttributesToToken(tokenXml, "pid", productId); tokenXml = AddAttributesToToken(tokenXml, "et", UppercaseFirst(licenseType.ToString())); tokenXml = AddAttributesToToken(tokenXml, "cid", purchaserId); //Set user limit if (licenseType == SPAppLicenseType.Free) { tokenXml = AddAttributesToToken(tokenXml, "ts", "0"); } else { tokenXml = AddAttributesToToken(tokenXml, "ts", userLimit); } //Set site license == unlimited users if (userLimit.Equals("0")) { tokenXml = AddAttributesToToken(tokenXml, "sl", "true"); } else { tokenXml = AddAttributesToToken(tokenXml, "sl", "false"); } //Set expiration (only supported for Trials) if (licenseType == SPAppLicenseType.Trial) { DateTime expirationDate; if (expirationDays == -1) { //expired token expirationDate = DateTime.UtcNow.Subtract(TimeSpan.FromDays(10)); } else if (expirationDays == 9999) { //Unlimited trial expirationDate = DateTime.MaxValue; } else { //today + the selected number of days expirationDate = DateTime.UtcNow.AddDays(expirationDays); } tokenXml = AddAttributesToToken(tokenXml, "ed", expirationDate.ToString("o")); } return(tokenXml); }
/// <summary> /// Returns a ready to display UI string for a given license. Includes a link to the storefront if necessary /// </summary> /// <param name="verifiedLicense">A license object that has already been verified by the Store web service</param> /// <param name="hostWebUrl">The URL of the web where the app is installed. Get this from SP tokens</param> /// <param name="currentPageUrl">The URL of the current page (so users can return to it from the storefront); must be same domain as hostWeb</param> /// <param name="appDisplayName">The title of the breadcrum that storefront will display on the back link</param> /// <returns></returns> public static string GetUIStringText(VerifyEntitlementTokenResponse verifiedLicense, string hostWebUrl, string currentPageUrl, string appDisplayName, string additionalFeaturesFullVersion) { string uiWarning = String.Empty; if (verifiedLicense == null) { uiWarning = String.Format(GetLicenseStringFromResource(_noLicenseStringKey), appDisplayName, "<a href='" + GetStoreSearchUrl(appDisplayName, hostWebUrl, currentPageUrl) + "'>", "</a>", additionalFeaturesFullVersion); } else { string storeFrontUrl = SPAppLicenseHelper.GetStorefrontUrl(verifiedLicense, hostWebUrl, currentPageUrl, appDisplayName); SPAppLicenseType licenseType = GetLicenseTypeFromLicense(verifiedLicense); switch (licenseType) { case SPAppLicenseType.Free: uiWarning = String.Format(GetLicenseStringFromResource(_freeStringKey), appDisplayName, "<a href='" + storeFrontUrl + "'>", "</a>"); break; case SPAppLicenseType.Paid: uiWarning = String.Format(GetLicenseStringFromResource(_paidStringKey), appDisplayName, "<a href='" + storeFrontUrl + "'>", "</a>"); break; case SPAppLicenseType.Trial: int remainingDays = GetRemainingDays(verifiedLicense); if (remainingDays == int.MaxValue) { //Unlimited time trial uiWarning = String.Format(GetLicenseStringFromResource(_trialUnlimitedStringKey), appDisplayName, "<a href='" + storeFrontUrl + "'>", "</a>", additionalFeaturesFullVersion); } else { //Time limited trial if (remainingDays > 0) { uiWarning = String.Format(GetLicenseStringFromResource(_trialStringKey), appDisplayName, remainingDays, "<a href='" + storeFrontUrl + "'>", "</a>", additionalFeaturesFullVersion); } else { //Expired trial uiWarning = String.Format(GetLicenseStringFromResource(_trialExpiredStringKey), "<a href='" + storeFrontUrl + "'>", "</a>"); } } break; } } return(uiWarning); }
protected void ImportLicense_Click(object sender, EventArgs e) { statusMessage.Text = ""; TokenHelper.TrustAllCertificates(); ClientContext ctx = TokenHelper.GetClientContextWithContextToken(Session["SPHostUrl"].ToString(), Session["contexToken"].ToString(), Request.Url.Authority); string appIconUrl = Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.LastIndexOf("/")) + "/AppIcon.png"; SPAppLicenseType licenseType = SPAppLicenseHelper.GetLicenseTypeFromString(importLicenseType.SelectedValue); string token = SPAppLicenseHelper.GenerateTestToken(licenseType, importProductId.Text, importUserLimit.SelectedValue, int.Parse(importExpiration.SelectedValue), importPurchaserId.Text); SPAppLicenseHelper.ImportLicense(ctx, token, appIconUrl, importAppTitle.Text, importProviderName.Text); WebColorConverter converter = new WebColorConverter(); statusMessage.Text = "License Imported Succesfully! Time=" + DateTime.Now; //importTokenGenerated.Text = token; }