// The Page_load method fetches the context token and the access token. The access token is used by all of the data retrieval methods. protected void Page_Load(object sender, EventArgs e) { TokenHelper.TrustAllCertificates(); string contextTokenString = TokenHelper.GetContextTokenFromRequest(Request); if (contextTokenString != null) { contextToken = TokenHelper.ReadAndValidateContextToken(contextTokenString, Request.Url.Authority); sharepointUrl = new Uri(Request.QueryString["SPHostUrl"]); accessToken = TokenHelper.GetAccessToken(contextToken, sharepointUrl.Authority).AccessToken; SelectEndpoint.CommandArgument = accessToken; SelectResourceButton.CommandArgument = accessToken; Reset.CommandArgument = accessToken; SelectActionButton.CommandArgument = accessToken; RetrieveEndpoints(accessToken, "Web"); } else if (!IsPostBack) { Response.Write("Could not find a context token."); } }
// The Page_load method fetches the context token and the access token. The access token is used by all of the data retrieval methods. protected void Page_Load(object sender, EventArgs e) { TokenHelper.TrustAllCertificates(); string contextTokenString = TokenHelper.GetContextTokenFromRequest(Request); if (contextTokenString != null) { contextToken = TokenHelper.ReadAndValidateContextToken(contextTokenString, Request.Url.Authority); sharepointUrl = new Uri(Request.QueryString["SPHostUrl"]); accessToken = TokenHelper.GetAccessToken(contextToken, sharepointUrl.Authority).AccessToken; AddListButton.CommandArgument = accessToken; RefreshListButton.CommandArgument = accessToken; RetrieveListButton.CommandArgument = accessToken; AddItemButton.CommandArgument = accessToken; DeleteListButton.CommandArgument = accessToken; ChangeListTitleButton.CommandArgument = accessToken; RetrieveLists(accessToken); } else if (!IsPostBack) { Response.Write("Could not find a context token."); } }
/// <summary> /// Retrieves an access token from ACS to call the source of the specified context token at the specified /// targetHost. The targetHost must be registered for principal the that sent the context token. /// </summary> /// <param name="contextToken">Context token issued by the intended access token audience</param> /// <param name="targetHost">Url authority of the target principal</param> /// <returns>An access token with an audience matching the context token's source</returns> public static OAuth2AccessTokenResponse GetAccessToken(SharePointContextToken contextToken, string targetHost) { string targetPrincipalName = contextToken.TargetPrincipalName; // Extract the refreshToken from the context token string refreshToken = contextToken.RefreshToken; if (String.IsNullOrEmpty(refreshToken)) { return null; } string realm = Realm ?? contextToken.Realm; string resource = GetFormattedPrincipal(targetPrincipalName, targetHost, realm); string clientId = GetFormattedPrincipal(ClientId, null, realm); OAuth2AccessTokenRequest oauth2Request = OAuth2MessageFactory.CreateAccessTokenRequestWithRefreshToken( clientId, ClientSecret, refreshToken, resource); // Get token OAuth2S2SClient client = new OAuth2S2SClient(); OAuth2AccessTokenResponse oauth2Response; try { oauth2Response = client.Issue(AcsMetadataParser.GetStsUrl(realm), oauth2Request) as OAuth2AccessTokenResponse; } catch (WebException wex) { using (StreamReader sr = new StreamReader(wex.Response.GetResponseStream())) { string responseText = sr.ReadToEnd(); throw new WebException(wex.Message + " - " + responseText, wex); } } return oauth2Response; }