コード例 #1
0
        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);
        }
コード例 #2
0
        // DELETE api/<controller>/5
        public void Delete(int id)
        {
            var accessTokenInfo = WebAPIHelper.GetAccessTokenInfo(ControllerContext);


            using (var appClientContext = accessTokenInfo.CreateAppOnlyClientContextForSPHost())
            {
                Utilities.DeleteJsLink(appClientContext);
            }
        }
コード例 #3
0
        public void Post([FromBody] string value)
        {
            var accessTokenInfo = WebAPIHelper.GetAccessTokenInfo(ControllerContext);


            using (var appClientContext = accessTokenInfo.CreateAppOnlyClientContextForSPHost())
            {
                Utilities.AddCssAndScripts(appClientContext, this.Request);
                Utilities.AddWebParts(appClientContext);
            }
        }
コード例 #4
0
        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";
        }
コード例 #5
0
        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");
            }
        }