Exemple #1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            string spAppToken = TokenHelper.GetContextTokenFromRequest(HttpContext.Current.Request);

            requestTokenValue.InnerText = spAppToken;

            SharePointContextToken spToken = ReadToken(spAppToken);

            requestTokenContents.InnerHtml = spToken.ToString().Replace(",", "<br/>");

            string hostWeb     = Page.Request["SPHostUrl"];
            Uri    hostUri     = new Uri(hostWeb);
            string accessToken = TokenHelper.GetAccessToken(spToken, hostUri.Authority).AccessToken;

            accessTokenValue.InnerText = accessToken;

            JwtSecurityTokenHandler tokenHandler = new System.IdentityModel.Tokens.JwtSecurityTokenHandler();
            SecurityToken           st           = tokenHandler.ReadToken(accessToken);

            accessTokenContents.InnerHtml = st.ToString().Replace(",", "<br/>");
        }
Exemple #2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            TokenHelper.TrustAllCertificates();

            string contextTokenString = TokenHelper.GetContextTokenFromRequest(Request);

            if (contextTokenString != null)
            {
                SharePointContextToken contextToken =
                    TokenHelper.ReadAndValidateContextToken(contextTokenString, Request.Url.Authority);

                Response.Write("<h2>Valid context token found</h2>");
                Response.Write("<p>" + contextToken.ToString() + "</p>");
                Response.Flush();
                Uri    sharepointUrl = new Uri(Request.QueryString["SPHostUrl"]);
                string accessToken   =
                    TokenHelper.GetAccessToken(contextToken, sharepointUrl.Authority).AccessToken;

                Response.Write("<h2>Valid access token retrieved</h2>");
                Response.Write("<p>" + accessToken + "</p>");
                Response.Flush();


                ClientContext clientContext =
                    TokenHelper.GetClientContextWithAccessToken(
                        sharepointUrl.ToString(), accessToken);
                clientContext.Load(clientContext.Web);
                clientContext.ExecuteQuery();

                Response.Write("<h2>Web title retrieved</h2>");
                Response.Write("<p>" + clientContext.Web.Title + "</p>");
                Response.Flush();


                HttpWebRequest request =
                    (HttpWebRequest)HttpWebRequest.Create(sharepointUrl.ToString() + "_api/Web/title");
                request.Headers.Add("Authorization", "Bearer " + accessToken);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader    reader   = new StreamReader(response.GetResponseStream());

                Response.Write("<h2>Web title retrieved using REST</h2>");
                Response.Write("<p>" + reader.ReadToEnd() + "</p>");
                Response.Flush();


                //Load the properties for the web object
                Web web = clientContext.Web;
                clientContext.Load(web);
                clientContext.ExecuteQuery();

                //Load the lists from the web object
                ListCollection lists = web.Lists;
                clientContext.Load <ListCollection>(lists);
                clientContext.ExecuteQuery();

                //print out the information
                clientContext.Load(web.CurrentUser);
                clientContext.ExecuteQuery();
                string str = clientContext.Web.CurrentUser.LoginName + "<br>";

                UserCollection users = web.SiteUsers;
                clientContext.Load <UserCollection>(users);
                clientContext.ExecuteQuery();

                foreach (User siteUser in users)
                {
                    str += "SiteUser: "******"<br>";
                }

                foreach (string key in Request.QueryString.AllKeys)
                {
                    str += key + " = " + Request.QueryString[key] + "<br>";
                }

                foreach (List list in lists)
                {
                    str += "List: " + list.Title + "<br>";
                }
                Response.Write(str);

                Response.Write("<p>Web title retrieved: " + clientContext.Web.Title + "</p>");
                Response.Flush();
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            TokenHelper.TrustAllCertificates();

            string contextTokenString = TokenHelper.GetContextTokenFromRequest(Request);

            if (contextTokenString != null)
            {
                SharePointContextToken contextToken =
                    TokenHelper.ReadAndValidateContextToken(contextTokenString, Request.Url.Authority);

                Response.Write("<h2>Valid context token found</h2>");
                Response.Write("<p>" + contextToken.ToString() + "</p>");
                Response.Flush();

                Uri    sharepointUrl = new Uri(Request.QueryString["SPHostUrl"]);
                string accessToken   =
                    TokenHelper.GetAccessToken(contextToken, sharepointUrl.Authority).AccessToken;

                Response.Write("<h2>Valid access token retrieved</h2>");
                Response.Write("<p>" + accessToken + "</p>");
                Response.Flush();

                //string appOnlyAccessToken = TokenHelper.GetAppOnlyAccessToken(contextToken.TargetPrincipalName, sharepointUrl.Authority, contextToken.Realm).AccessToken;

                ClientContext clientContext =
                    TokenHelper.GetClientContextWithAccessToken(
                        sharepointUrl.ToString(), accessToken);

                //Load the properties for the web object.
                Web web = clientContext.Web;
                clientContext.Load(web);
                clientContext.ExecuteQuery();

                //clientContext.Load(clientContext.Web);

                //clientContext.ExecuteQuery();

                //Response.Write("<h2>Web title retrieved</h2>");
                //Response.Write("<p>" + clientContext.Web.Title + "</p>");
                //Response.Flush();


                string appOnlyAccessToken = TokenHelper.GetAppOnlyAccessToken(contextToken.TargetPrincipalName, sharepointUrl.Authority, contextToken.Realm).AccessToken;


                Response.Write("<h2>Valid app-only access token retrieved</h2>");
                Response.Write("<p>" + appOnlyAccessToken + "</p>");
                Response.Flush();

                clientContext.Dispose();

                clientContext = TokenHelper.GetClientContextWithAccessToken(sharepointUrl.ToString(), appOnlyAccessToken);

                clientContext.Load(clientContext.Web);
                clientContext.ExecuteQuery();

                Response.Write("<h2>Web title retrieved with an app only token</h2>");
                Response.Write("<p>" + clientContext.Web.Title + "</p>");

                clientContext.Dispose();
            }
        }
Exemple #4
0
        protected void Page_Load(object sender, EventArgs e)
        {
            uriHostWeb = new Uri(Request.QueryString["SPHostUrl"]);

            contextTokenString = TokenHelper.GetContextTokenFromRequest(Request);

            if (contextTokenString != null)
            {
                contextToken = TokenHelper.ReadAndValidateContextToken(contextTokenString, Request.Url.Authority);

                targetPrincipalName = contextToken.TargetPrincipalName;
                realm                    = contextToken.Realm;
                accessToken              = TokenHelper.GetAccessToken(contextToken, uriHostWeb.Authority);
                accessTokenString        = TokenHelper.GetAccessToken(contextToken, uriHostWeb.Authority).AccessToken;
                appOnlyAccessToken       = TokenHelper.GetAppOnlyAccessToken(contextToken.TargetPrincipalName, uriHostWeb.Authority, contextToken.Realm);
                appOnlyAccessTokenString = appOnlyAccessToken.AccessToken;

                // cache state that can be shared across user
                Cache["uriHostWeb"] = uriHostWeb;
                Cache["appOnlyAccessTokenString"] = appOnlyAccessTokenString;

                // cache state that must be tracked on per-user basis
                Session["contextTokenString"] = contextTokenString;
                Session["accessTokenString"]  = accessTokenString;
            }

            #region "Incoming Data"

            HtmlTableWriter table1 = new HtmlTableWriter();

            table1.AddRow("Request URL", this.Request.Path);

            foreach (var param in Request.Form.AllKeys)
            {
                table1.AddRow("Request.Form['" + param + "']", Request.Form[param].ToString());
            }

            foreach (var param in Request.QueryString.AllKeys)
            {
                table1.AddRow("Request.QueryString['" + param + "']", Request.QueryString[param].ToString());
            }

            placeholderIncomingData.Controls.Add(new LiteralControl(table1.ToString()));

            #endregion

            #region "Context Token"

            HtmlTableWriter table2 = new HtmlTableWriter();
            table2.AddRow("Context Token (RAW)", contextTokenString);

            if (contextToken != null)
            {
                table2.AddRow("Content Token (JSON)", contextToken.ToString());
                table2.AddRow("Cache Key", contextToken.CacheKey);
                table2.AddRow("Realm", contextToken.Realm);
                table2.AddRow("Security Token Service Uri", contextToken.SecurityTokenServiceUri);
                table2.AddRow("Target Principal Name", contextToken.TargetPrincipalName);

                table2.AddRow("Valid From", contextToken.ValidFrom.ToString());
                table2.AddRow("Valid To", contextToken.ValidTo.ToString());
                table2.AddRow("Refresh Token", contextToken.RefreshToken);

                placeholderContextToken.Controls.Add(new LiteralControl(table2.ToString()));
            }

            #endregion

            #region "Access Token"
            if (contextToken != null)
            {
                HtmlTableWriter table3 = new HtmlTableWriter();
                // create OAuth access token
                table3.AddRow("Access Token", accessTokenString);
                table3.AddRow("Access Token (JSON)", accessToken.ToString());
                table3.AddRow("Resource", accessToken.Message["resource"]);
                table3.AddRow("NotBefore", accessToken.NotBefore.ToString());
                table3.AddRow("ExpiresOn", accessToken.ExpiresOn.ToString());
                table3.AddRow("ExpiresIn", TimeSpan.FromSeconds(Convert.ToInt32(accessToken.ExpiresIn)).TotalHours.ToString("0.0") + " hours");

                foreach (var msg in accessToken.Message)
                {
                    //table3.AddRow("Message - " + msg.Key, msg.Value);
                }

                placeholderAccessToken.Controls.Add(new LiteralControl(table3.ToString()));
            }
            #endregion

            #region "App-only Access Token"
            if (contextToken != null)
            {
                appOnlyAccessToken       = TokenHelper.GetAppOnlyAccessToken(contextToken.TargetPrincipalName, uriHostWeb.Authority, contextToken.Realm);
                appOnlyAccessTokenString = appOnlyAccessToken.AccessToken;

                HtmlTableWriter table4 = new HtmlTableWriter();
                // create OAuth access token
                table4.AddRow("App-only Access Token", appOnlyAccessTokenString);
                table4.AddRow("App-only Access Token (JSON)", appOnlyAccessToken.ToString());
                table4.AddRow("Resource", appOnlyAccessToken.Message["resource"]);
                table4.AddRow("NotBefore", appOnlyAccessToken.NotBefore.ToString());
                table4.AddRow("ExpiresOn", appOnlyAccessToken.ExpiresOn.ToString());
                table4.AddRow("ExpiresIn", TimeSpan.FromSeconds(Convert.ToInt32(appOnlyAccessToken.ExpiresIn)).TotalHours.ToString("0.0") + " hours");

                foreach (var msg in appOnlyAccessToken.Message)
                {
                    table4.AddRow("Message - " + msg.Key, msg.Value);
                }

                placeholderAppOnlyAccessToken.Controls.Add(new LiteralControl(table4.ToString()));
            }

            #endregion
        }