Skip to content

Commit 0e73a83

Browse files
authored
Merge pull request #4858 from KoenZomers/PersistedLoginText
Proposal for changing the persisted login text
2 parents c3530a0 + edea640 commit 0e73a83

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

pages/articles/persistedlogin.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Persisted Login
2+
3+
Starting with PnP PowerShell 3.0, the `Connect-PnPOnline` cmdlet has been updated to allow `-PersistLogin` to be provided. Documentation for it can be [found here](../cmdlets/Connect-PnPOnline.md#-persistlogin). This parameter allows you to persist the delegated authentication token retrieved through an interactive login in a local file on your machine, which can be used for subsequent connections without requiring re-authentication.
4+
5+
This feature is particularly useful for scenarios where you need to run scripts or tasks that require authentication but do not want to enter your credentials every time. The risk obviously will be that anyone with access to your machine can use the token to authenticate against your tenant.
6+
7+
## Where is the token stored
8+
The token is stored in a file in the `%LOCALAPPDATA%\.m365pnppowershell` folder on Windows or `$HOME/.m365pnppowershell` on Linux and MacOS. The file is encrypted using the Data Protection API (DPAPI) on Windows or the Keychain on MacOS and Linux.
9+
10+
This means that the token is securely stored and cannot be easily accessed by unauthorized users nor can it be copied to another machine as the encryption is tied to the machine on which it has been generated. However, it is important to note that if you share your machine with others, they may be able to access the token if they have access to your user profile.
11+
12+
## How does it work
13+
When you use the `-PersistLogin` parameter with the `Connect-PnPOnline` cmdlet, PnP PowerShell will authenticate you as normal but will also store the refresh token in a local file. The next time you run `Connect-PnPOnline`, PnP PowerShell will check if a valid token already exists in the local file for the tenant or site you are trying to connect to. If a valid token is found, it will be used to authenticate without prompting for credentials. If no valid token is found, PnP PowerShell will prompt for credentials as normal.
14+
15+
You do not need to specify the `-PersistLogin` parameter again for subsequent connections unless you want to change the behavior.
16+
17+
## Clearing the persisted login
18+
If you want to clear the persisted login and remove the stored token, you can connect to the tenant for which you would like to remove the stored token first and then use the `Disconnect-PnPOnline` cmdlet with the `-ClearPersistedLogin` option. Documentation for it can be [found here](../cmdlets/Disconnect-PnPOnline.md#-clearpersistedlogin). This will delete the token from the local file and require you to authenticate again the next time you run `Connect-PnPOnline`.
19+
20+
## FAQ
21+
22+
### Can I use `-PersistLogin` in Azure?
23+
24+
No you cannot, as there are no profiles folders in Azure.
25+
26+
### Can I use `-PersistLogin` with an app only context?
27+
28+
No, it is meant to be used for an interactive delegated authentication context only. If you want to use an app only context, you can just use the parameters with the `Connect-PnPOnline` cmdlet that support app only authentication as normal. Documentation for it can be [found here](../cmdlets/Connect-PnPOnline.md#app-only-with-azure-active-directory).
29+
30+
### Do I still need my own application registration in Entra ID when using `-PersistLogin`?
31+
32+
Yes, this is still required.
33+
34+
### Can I use a different application registration for `-PersistLogin` for different tenants or even site collections on the same tenant?
35+
36+
Yes, that is supported. Just use it as described above and it will store the token for the tenant or site collection you are connecting to.

pages/articles/toc.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
- name: Advanced topics
4040
items:
4141
- name: How to handle authenticating to multiple tenants
42-
href: handlingmultitenantauth
42+
href: handlingmultitenantauth.md
4343
- name: Using Microsoft Search with PnP PowerShell
4444
href: microsoftsearch.md
4545
- name: Batching in PnP PowerShell
@@ -48,6 +48,8 @@
4848
href: upgrading.md
4949
- name: Credential Management
5050
href: credentialmanagement.md
51+
- name: Working with persisted logins
52+
href: persistedlogin.md
5153
- name: How to contribute
5254
items:
5355
- name: Getting started

src/Commands/Base/PnPConnection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1053,7 +1053,7 @@ private static void EnableCaching(string url, string clientid)
10531053

10541054
private static void WriteCacheEnabledMessage(PSHost host)
10551055
{
1056-
host.UI.WriteWarningLine("Secure token cache enabled. Access tokens may be retrieved from the cache if present. Clear the cache entry for this tenant with Disconnect-PnPOnline -ClearPersistedLogin.");
1056+
host.UI.WriteWarningLine("Connecting using token cache. See https://pnp.github.io/powershell/articles/persistedlogin.html for more information.");
10571057
}
10581058

10591059
internal static void ClearCache(PnPConnection connection)

0 commit comments

Comments
 (0)