App permission in SharePoint online

by | Nov 14, 2016 | Development, Office 365, SharePoint | 0 comments

AppForSharePointWebToolkit

Today I would like to write a few words how we can use App permission in SharePoint online. This topic is very interesting as also very useful. It allows us to make a lot of things with our tenant without direct login in Office 365 login page. Do you remember my post about geolocation? When you take a look on the code you will see that I used user name and password with clear text. Of course it could be fine in some situation, but in general we should avoid this. So the question is what we can do? How to remove user name and password from our application?

The best solution for this problem is to use App permission. With this approach our application will use tokens, which provide us much better security level. To do start we have to generate Client Id and Client Secret with following URL in our tenant:

URL: https://[tenant].sharepoint.com/_layouts/15/appregnew.aspx

app registration
app registration

Press Generate button. We will get two strings which will allow our application to authenticate with tokens. In Title field we enter name of our application.We should use something more than “my app” as in the future it could be difficult to remember purpose of the application. App domain should use localhost, and Redirect URI should have URL of our tenant. That’s the first step. After token generation we have to setup permission for our app. We can do this at following URL: /layouts/15/appinv.aspx. We enter following XML structure:

 <AppPermissionRequests AllowAppOnlyPolicy="true">
 <AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="FullControl" />
 </AppPermissionRequests>

How to use it App permission in SharePoint online

So it’s time to use our knowledge in real world. Let’s create simple console application. Next in the NuGet Package Manager we should look for something called AppForSharePointWebToolkit. Install it.

AppForSharePointWebToolkit
AppForSharePointWebToolkit

After package installation we will get some references to Microsoft.SharePoint.Client.* and plik App.config, which is our next target. Let’s open it and add new section appSettings with our keys  ClientId and ClientSecret. We put there our keys which we generate before. In our case it looks like this:

<add key="ClientId" value="28921286-09f3-4bce-819a-4e53a12dabb6"/>
 <add key="ClientSecret" value="HOaS7LQbBi8QYU/1x67Bdae38qJh79qt+LY4IoLcQHM="/>
 <add key="ClientSettingsProvider.ServiceUri" value="" />
 

The last piece is new using statement in our code using Microsoft.SharePoint.Client; . And that’s all . In my cases I prefer to use additional class which has only one purpose – to return only clientContext for single URL, just like below:

public static ClientContext GetClientContext(string siteUrl)
{
         Uri siteUri = new Uri(siteUrl);
         string realm = TokenHelper.GetRealmFromTargetUrl(siteUri);
         string accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, siteUri.Authority, realm).AccessToken;
         var clientContext = TokenHelper.GetClientContextWithAccessToken(siteUri.ToString(), accessToken);
         return clientContext;
}
 

As a result we will get clientContext, so then do whatever we want in our tenant.

Written by Tomasz Szulczewski

Hi, my name is Tomasz Szulczewski, and I have been in love with information technology for over 25 years, but I still have an IT passion and feel like a geek. I am a person who is problem solver who thinks that not all people must be experts in IT.

Related Posts

Power virtual agent for office 365

Power virtual agent for office 365

Why Should I even care about power virtual agent for office 365 or power virtual agent for teams? Simple answer? Do you need an AI assistant who will help you run your business in many different ways? If yes, that's your answer :). But let's start from the beginning....

read more
What is SharePoint in Office 365?

What is SharePoint in Office 365?

What is SharePoint in Office 365, and why is SharePoint for small business the critical product? So you bought a Microsoft 365 license, and your tenant is online. You open a browser, and you have no idea what to do next? Don't worry; I will try to give you a few...

read more

0 Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.