public OnPremAccessTokenInfo(HttpRequestBase request) { WindowsIdentity logonUserIdentity = request.LogonUserIdentity; this.SPHostUrl = WebAPIHelper.GetSPHostUrl(request).ToString(); string onPremAppOnlyAccessToken = TokenHelper.GetS2SAccessTokenWithWindowsIdentity(new Uri(this.SPHostUrl), null); string onPremUserAccessToken = TokenHelper.GetS2SAccessTokenWithWindowsIdentity(new Uri(this.SPHostUrl), logonUserIdentity); this.RefreshToken = null; this.CacheKey = logonUserIdentity.User.Value; // SPAppWebUrl string spAppWebUrlString = TokenHelper.EnsureTrailingSlash(request.QueryString[WebAPIHelper.SPAppWebUrlKey]); Uri spAppWebUrl; if (!Uri.TryCreate(spAppWebUrlString, UriKind.Absolute, out spAppWebUrl) || !(spAppWebUrl.Scheme == Uri.UriSchemeHttp || spAppWebUrl.Scheme == Uri.UriSchemeHttps)) { spAppWebUrl = null; } else { this.SPAppWebUrl = spAppWebUrl.ToString(); } this.AppAccessToken = onPremAppOnlyAccessToken; this.UserAccessToken = onPremUserAccessToken; this.Expires = DateTime.Now.AddDays(1); }
// DELETE api/<controller>/5 public void Delete(int id) { var accessTokenInfo = WebAPIHelper.GetAccessTokenInfo(ControllerContext); using (var appClientContext = accessTokenInfo.CreateAppOnlyClientContextForSPHost()) { Utilities.DeleteJsLink(appClientContext); } }
public void Post([FromBody] string value) { var accessTokenInfo = WebAPIHelper.GetAccessTokenInfo(ControllerContext); using (var appClientContext = accessTokenInfo.CreateAppOnlyClientContextForSPHost()) { Utilities.AddCssAndScripts(appClientContext, this.Request); Utilities.AddWebParts(appClientContext); } }
public string Get(int id) { var accessTokenInfo = WebAPIHelper.GetAccessTokenInfo(ControllerContext); var ctx = accessTokenInfo.CreateUserClientContextForSPHost(); ctx.Load(ctx.Web, w => w.Title); ctx.ExecuteQuery(); return(ctx.Web.Title); //return "value"; }
public CloudAccessTokenInfo(HttpRequestBase request) { try { var contextToken = TokenHelper.GetContextTokenFromRequest(request); SharePointContextToken sharePointContextToken = TokenHelper.ReadAndValidateContextToken(contextToken, request.Url.Authority); this.RefreshToken = sharePointContextToken.RefreshToken; this.CacheKey = sharePointContextToken.CacheKey; this.SPHostUrl = WebAPIHelper.GetSPHostUrl(request).ToString(); // SPAppWebUrl string spAppWebUrlString = TokenHelper.EnsureTrailingSlash(request.QueryString[SharePointContext.SPAppWebUrlKey]); Uri spAppWebUrl; if (!Uri.TryCreate(spAppWebUrlString, UriKind.Absolute, out spAppWebUrl) || !(spAppWebUrl.Scheme == Uri.UriSchemeHttp || spAppWebUrl.Scheme == Uri.UriSchemeHttps)) { spAppWebUrl = null; } else { this.SPAppWebUrl = spAppWebUrl.ToString(); } var accessToken = TokenHelper.GetAccessToken(this.RefreshToken, TokenHelper.SharePointPrincipal, new Uri(this.SPHostUrl).Authority, TokenHelper.GetRealmFromTargetUrl( new Uri(this.SPHostUrl))); var userToken = accessToken.AccessToken; string appToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, new Uri(this.SPHostUrl).Authority, TokenHelper.GetRealmFromTargetUrl( new Uri(this.SPHostUrl))).AccessToken; this.AppAccessToken = appToken; this.UserAccessToken = userToken; this.Expires = accessToken.ExpiresOn.AddHours(12); } catch (Exception error) { //throw new Exception("Redirect user to app"); } }