public async Task<ActionResult> Save(string newName) { //grab all the stuff you need to get a token var signInUserId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value; var userObjectId = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value; var tenantId = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value; String token = null; AuthenticationContext authContext = new AuthenticationContext(string.Format(AADAppSettings.AuthorizationUri, tenantId), new ADALTokenCache(signInUserId)); AuthenticationResult authResult = null; Stream fileStream = null; //change the name in the file gpxUtils = Session[DocumentKey] as GPXHelper; gpxUtils.setTitle(newName); try { //grab activation parameters (this was set in Open controller) ActivationParameters parameters = Session[AADAppSettings.SavedFormDataName] as ActivationParameters; //grab token authResult = await authContext.AcquireTokenSilentAsync(parameters.ResourceId, new ClientCredential(AADAppSettings.ClientId, AADAppSettings.AppKey), new UserIdentifier(userObjectId, UserIdentifierType.UniqueId)); token = authResult.AccessToken; //create request to write file back to server HttpWebRequest request = (HttpWebRequest)WebRequest.Create(parameters.FilePut); request.Headers.Add("Authorization: bearer " + token); request.Method = "PUT"; //write bytes to stream byte[] xmlByteArray = Encoding.Default.GetBytes(gpxUtils.doc.OuterXml); fileStream = request.GetRequestStream(); fileStream.Write(xmlByteArray, 0, xmlByteArray.Length); //send file over & respond as the workload does HttpWebResponse response = (HttpWebResponse)request.GetResponse(); return new HttpStatusCodeResult(response.StatusCode); } catch (AdalException exception) { //handle token acquisition failure if (exception.ErrorCode == AdalError.FailedToAcquireTokenSilently) { authContext.TokenCache.Clear(); } return new HttpStatusCodeResult(HttpStatusCode.ExpectationFailed); } catch (WebException webException) { //something funky happened in the web response - return as needed HttpWebResponse response = (HttpWebResponse)webException.Response; return new HttpStatusCodeResult(response.StatusCode); } finally { //close the file stream if (fileStream != null) { fileStream.Close(); } } }
public async Task <ActionResult> Save(string newName) { //grab all the stuff you need to get a token var signInUserId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value; var userObjectId = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value; var tenantId = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value; String token = null; AuthenticationContext authContext = new AuthenticationContext(string.Format(AADAppSettings.AuthorizationUri, tenantId), new ADALTokenCache(signInUserId)); AuthenticationResult authResult = null; Stream fileStream = null; //change the name in the file gpxUtils = Session[DocumentKey] as GPXHelper; gpxUtils.setTitle(newName); try { //grab activation parameters (this was set in Open controller) ActivationParameters parameters = Session[AADAppSettings.SavedFormDataName] as ActivationParameters; //grab token authResult = await authContext.AcquireTokenSilentAsync(parameters.ResourceId, new ClientCredential(AADAppSettings.ClientId, AADAppSettings.AppKey), new UserIdentifier(userObjectId, UserIdentifierType.UniqueId)); token = authResult.AccessToken; //create request to write file back to server HttpWebRequest request = (HttpWebRequest)WebRequest.Create(parameters.FilePut); request.Headers.Add("Authorization: bearer " + token); request.Method = "PUT"; //write bytes to stream byte[] xmlByteArray = Encoding.Default.GetBytes(gpxUtils.doc.OuterXml); fileStream = request.GetRequestStream(); fileStream.Write(xmlByteArray, 0, xmlByteArray.Length); //send file over & respond as the workload does HttpWebResponse response = (HttpWebResponse)request.GetResponse(); return(new HttpStatusCodeResult(response.StatusCode)); } catch (AdalException exception) { //handle token acquisition failure if (exception.ErrorCode == AdalError.FailedToAcquireTokenSilently) { authContext.TokenCache.Clear(); } return(new HttpStatusCodeResult(HttpStatusCode.ExpectationFailed)); } catch (WebException webException) { //something funky happened in the web response - return as needed HttpWebResponse response = (HttpWebResponse)webException.Response; return(new HttpStatusCodeResult(response.StatusCode)); } finally { //close the file stream if (fileStream != null) { fileStream.Close(); } } }