Skip to content

Commit c34b36f

Browse files
authored
Added code samples
1 parent ff8f7af commit c34b36f

File tree

1 file changed

+69
-1
lines changed

1 file changed

+69
-1
lines changed

pages/articles/acsdeprecation.md

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ It produces an output similar to:
3838

3939
![Sample Get-PnPAzureACSPrincipal output](./../images/acsdeprecation/sample-get-pnpazureacsprincipal.png)
4040

41-
The SiteId, WebId and ListId columns in this output, give away what kind of permissions have been set on the ACS Application registration. If a column contains just zeroes (00000000-0000-0000-0000-000000000000), it means the permissions have not been set down to that level. If it contains something else (i.e. 5c7836e9-a6fb-450f-a117-43ccea341193), it means that permissions have been set on that level. So to make it concrete, for the above sample, the following permissions have been set on this ACS Application Registration:
41+
The SiteId, WebId and ListId columns in this output, give away what kind of permissions have been set on the ACS Application registration. If a column contains just zeroes (00000000-0000-0000-0000-000000000000), it means the permissions have not been set down to that level. If it contains something else (i.e. 5c7836e9-a6fb-450f-a117-43ccea341193), it means that permissions have been set on that level. So to make it concrete, for the above sample, the following permissions have been set on this ACS Application Registration:
4242

4343
- FullControl on the Web scope
4444
- Read on the list with id e94218ca-30d1-4118-a9b0-33e00f00d139
@@ -52,3 +52,71 @@ The above example again would map to:
5252
- Sites.Selected
5353

5454
Use [Grant-PnPAzureADAppSitePermission](Grant-PnPAzureADAppSitePermission.md) to set FullControl permissions on it.
55+
56+
## What do I need to change in my code?
57+
58+
### PnP PowerShell
59+
If you were connecting using PnP PowerShell, you will have to switch to using a certificate instead of a clientsecret and update your Connect-PnPOnline to something such as:
60+
61+
```powershell
62+
Connect-PnPOnline https://contoso.sharepoint.com -CertificatePath c:\temp\pnp.pfx -Clientid xxx-xxx-xxx-xxx-xxx -Tenant xxx-xxx-xxx-xxx-xxx
63+
```
64+
65+
For all the possible connection options, check the documentation of [Connect-PnPOnline](../cmdlets/Connect-PnPOnline.md).
66+
67+
### PnP Core
68+
69+
There are many ways to connect through PnP Core. Also in this scenario, you will have to authenticate using a Client ID and Certificate. There's no one off sample that works for every scenario. Have a look at the [PnP Core Authentication documentation](https://pnp.github.io/pnpcore/using-the-sdk/configuring%20authentication.html) for inspiration towards the possible options.
70+
71+
### CSOM
72+
73+
Here as well you need to use a certificate to connect. A sample piece of code demonstrating how to do this using the native Client Side Object Model (CSOM) is:
74+
75+
```c#
76+
using Microsoft.Identity.Client;
77+
using Microsoft.SharePoint.Client;
78+
using System;
79+
using System.Security.Cryptography.X509Certificates;
80+
81+
class Program
82+
{
83+
static void Main()
84+
{
85+
string siteUrl = "https://yourtenant.sharepoint.com/sites/yoursite";
86+
string tenantId = "your-tenant-id";
87+
string clientId = "your-client-id";
88+
string certThumbprint = "your-cert-thumbprint";
89+
string authority = $"https://login.microsoftonline.com/{tenantId}";
90+
91+
// Load certificate from store
92+
var store = new X509Store(StoreLocation.CurrentUser);
93+
store.Open(OpenFlags.ReadOnly);
94+
var cert = store.Certificates.Find(X509FindType.FindByThumbprint, certThumbprint, false)[0];
95+
store.Close();
96+
97+
// Acquire token
98+
var app = ConfidentialClientApplicationBuilder.Create(clientId)
99+
.WithCertificate(cert)
100+
.WithAuthority(new Uri(authority))
101+
.Build();
102+
103+
string[] scopes = { "https://yourtenant.sharepoint.com/.default" };
104+
var result = app.AcquireTokenForClient(scopes).ExecuteAsync().Result;
105+
106+
// Connect to SharePoint
107+
using (var context = new ClientContext(siteUrl))
108+
{
109+
context.ExecutingWebRequest += (sender, e) =>
110+
{
111+
e.WebRequestExecutor.RequestHeaders["Authorization"] = "Bearer " + result.AccessToken;
112+
};
113+
114+
Web web = context.Web;
115+
context.Load(web);
116+
context.ExecuteQuery();
117+
118+
Console.WriteLine("Connected to site: " + web.Title);
119+
}
120+
}
121+
}
122+
```

0 commit comments

Comments
 (0)