Quantcast
Channel: Developing Apps for SharePoint 2013 forum
Viewing all articles
Browse latest Browse all 8089

Provider Hosted App for anonymous users.

$
0
0

Hi,

I want to create an app part that works for anonymous users in Office365 public website. I found that it can be possible by using App Only Policy. I have created one provider hosted app with App Only policy and hosted it in my local IIS server and deploy app in my Office365 sharepoint site.

The problem is that it is working fine with logged in user but Giving me Null context token for anonymous users.

Here is my code snippet.

AppManifest.xml

<AppPermissionRequests AllowAppOnlyPolicy="true" />

C# code

 string contextTokenString = TokenHelper.GetContextTokenFromRequest(Request);
                if (contextTokenString != null)
                {
                    //Get context token.
                    SharePointContextToken contextToken = TokenHelper.ReadAndValidateContextToken(contextTokenString, Request.Url.Authority);
                    Uri sharepointUrl = new Uri(Request.QueryString["SPHostUrl"]);
                    //Get app only token.
                    string appOnlyAccessToken = TokenHelper.GetAppOnlyAccessToken(contextToken.TargetPrincipalName, sharepointUrl.Authority, contextToken.Realm).AccessToken;
                    using (var clientContext = TokenHelper.GetClientContextWithAccessToken(sharepointUrl.ToString(), appOnlyAccessToken))
                    {
                        clientContext.Load(clientContext.Web, web => web.Title);
                        clientContext.Load(clientContext.Web, web => web.CurrentUser.LoginName);
                        clientContext.ExecuteQuery();
                        Response.Write("title=" + clientContext.Web.Title + ", ");
                        Response.Write("user=" + clientContext.Web.CurrentUser.LoginName);
                    }
                }

This works fine with logged in users but gives error for anonymous users.

ERROR

The parameter 'token' cannot be a null or empty string


Viewing all articles
Browse latest Browse all 8089

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>