Beispiel #1
0
        /// <summary>
        /// Removes the account associated with context.HttpContext.User from the MSAL.NET cache.
        /// </summary>
        /// <param name="context">RedirectContext passed-in to a <see cref="OpenIdConnectEvents.OnRedirectToIdentityProviderForSignOut"/>
        /// OpenID Connect event.</param>
        /// <returns>A <see cref="Task"/> that represents a completed account removal operation.</returns>
        public async Task RemoveAccountAsync(RedirectContext context)
        {
            ClaimsPrincipal user   = context.HttpContext.User;
            string?         userId = user.GetMsalAccountId();

            if (!string.IsNullOrEmpty(userId))
            {
                IConfidentialClientApplication app = await GetOrBuildConfidentialClientApplicationAsync().ConfigureAwait(false);

                // For B2C, we should remove all accounts of the user regardless the user flow
                if (_microsoftIdentityOptions.IsB2C)
                {
                    var b2cAccounts = await app.GetAccountsAsync().ConfigureAwait(false);

                    foreach (var b2cAccount in b2cAccounts)
                    {
                        await app.RemoveAsync(b2cAccount).ConfigureAwait(false);
                    }

                    await _tokenCacheProvider.ClearAsync(userId).ConfigureAwait(false);
                }
                else
                {
                    string?  identifier = context.HttpContext.User.GetMsalAccountId();
                    IAccount account    = await app.GetAccountAsync(identifier).ConfigureAwait(false);

                    if (account != null)
                    {
                        await app.RemoveAsync(account).ConfigureAwait(false);

                        await _tokenCacheProvider.ClearAsync(userId).ConfigureAwait(false);
                    }
                }
            }
        }
        /// <summary>
        /// Removes the account associated with context.HttpContext.User from the MSAL.NET cache.
        /// </summary>
        /// <param name="context">RedirectContext passed-in to a <see cref="OpenIdConnectEvents.OnRedirectToIdentityProviderForSignOut"/>
        /// OpenID Connect event.</param>
        /// <returns>A <see cref="Task"/> that represents a completed account removal operation.</returns>
        public async Task RemoveAccountAsync(RedirectContext context)
        {
            ClaimsPrincipal user   = context.HttpContext.User;
            string?         userId = user.GetMsalAccountId();

            if (!string.IsNullOrEmpty(userId))
            {
                IConfidentialClientApplication app = await GetOrBuildConfidentialClientApplicationAsync().ConfigureAwait(false);

                if (_microsoftIdentityOptions.IsB2C)
                {
                    await _tokenCacheProvider.ClearAsync(userId).ConfigureAwait(false);
                }
                else
                {
                    string?  identifier = context.HttpContext.User.GetMsalAccountId();
                    IAccount account    = await app.GetAccountAsync(identifier).ConfigureAwait(false);

                    if (account != null)
                    {
                        await app.RemoveAsync(account).ConfigureAwait(false);

                        await _tokenCacheProvider.ClearAsync(userId).ConfigureAwait(false);
                    }
                }
            }
        }
        public static void ClearCaches()
        {
            if (publicClientApplication != null)
            {
                var accounts = publicClientApplication.GetAccountsAsync().GetAwaiter().GetResult().ToList();

                // clear the cache
                while (accounts.Any())
                {
                    publicClientApplication.RemoveAsync(accounts.First());
                    accounts = publicClientApplication.GetAccountsAsync().GetAwaiter().GetResult().ToList();
                }
            }
            if (confidentialClientApplication != null)
            {
                var accounts = confidentialClientApplication.GetAccountsAsync().GetAwaiter().GetResult().ToList();

                // clear the cache
                while (accounts.Any())
                {
                    confidentialClientApplication.RemoveAsync(accounts.First());
                    accounts = confidentialClientApplication.GetAccountsAsync().GetAwaiter().GetResult().ToList();
                }
            }
        }
Beispiel #4
0
        public async Task SignOut()
        {
            if (null != _userAccount)
            {
                await _msalClient.RemoveAsync(_userAccount);

                _userAccount = null;
            }
        }
Beispiel #5
0
        public static async Task RemoveAccount()
        {
            BuildConfidentialClientApplication();

            var userAccount = await clientapp.GetAccountAsync(ClaimsPrincipal.Current.GetAccountId());

            if (userAccount != null)
            {
                await clientapp.RemoveAsync(userAccount);
            }
        }
Beispiel #6
0
        public static async Task RemoveAccount(string clientId, string clientSecret, string redirectUri, string authority, string accountId)
        {
            BuildConfidentialClientApplication(clientId, clientSecret, redirectUri, authority);

            var userAccount = await _cca.GetAccountAsync(accountId);

            if (userAccount != null)
            {
                await _cca.RemoveAsync(userAccount);
            }
        }
        /// <summary>
        /// Removes the account associated with context.HttpContext.User from the MSAL.NET cache.
        /// </summary>
        /// <param name="context">RedirectContext passed-in to a <see cref="OpenIdConnectEvents.OnRedirectToIdentityProviderForSignOut"/>
        /// Openidconnect event.</param>
        /// <returns></returns>
        public async Task RemoveAccountAsync(RedirectContext context)
        {
            ClaimsPrincipal user = context.HttpContext.User;
            IConfidentialClientApplication app = await GetOrBuildConfidentialClientApplicationAsync().ConfigureAwait(false);

            IAccount account = null;

            // For B2C, we should remove all accounts of the user regardless the user flow
            if (_microsoftIdentityOptions.IsB2C)
            {
                var b2cAccounts = await app.GetAccountsAsync().ConfigureAwait(false);

                foreach (var b2cAccount in b2cAccounts)
                {
                    await app.RemoveAsync(b2cAccount).ConfigureAwait(false);
                }

                _tokenCacheProvider?.ClearAsync().ConfigureAwait(false);
            }

            else
            {
                account = await app.GetAccountAsync(context.HttpContext.User.GetMsalAccountId()).ConfigureAwait(false);

                // Workaround for the guest account
                if (account == null)
                {
                    var accounts = await app.GetAccountsAsync().ConfigureAwait(false);

                    account = accounts.FirstOrDefault(a => a.Username == user.GetLoginHint());
                }

                if (account != null)
                {
                    await app.RemoveAsync(account).ConfigureAwait(false);

                    _tokenCacheProvider?.ClearAsync().ConfigureAwait(false);
                }
            }
        }
        /// <summary>
        /// Removes the account associated with context.HttpContext.User from the MSAL.NET cache
        /// </summary>
        /// <param name="context">RedirectContext passed-in to a <see cref="OnRedirectToIdentityProviderForSignOut"/>
        /// Openidconnect event</param>
        /// <returns></returns>
        public async Task RemoveAccount(RedirectContext context)
        {
            ClaimsPrincipal user = context.HttpContext.User;
            IConfidentialClientApplication app = CreateApplication(context.HttpContext, user);
            IAccount account = await app.GetAccountAsync(context.HttpContext.User.GetMsalAccountId());

            // Workaround for the guest account
            if (account == null)
            {
                var accounts = await app.GetAccountsAsync();

                account = accounts.FirstOrDefault(a => a.Username == user.GetLoginHint());
            }
            await app.RemoveAsync(account);
        }
        public static async Task ClearUserTokenCache()
        {
            IConfidentialClientApplication clientapp = ConfidentialClientApplicationBuilder.Create(AuthenticationConfig.ClientId)
                                                       .WithClientSecret(AuthenticationConfig.ClientSecret)
                                                       .WithRedirectUri(AuthenticationConfig.RedirectUri)
                                                       .WithAuthority(new Uri(AuthenticationConfig.Authority))
                                                       .Build();

            // We only clear the user's tokens.
            MSALPerUserMemoryTokenCache userTokenCache = new MSALPerUserMemoryTokenCache(clientapp.UserTokenCache, ClaimsPrincipal.Current);
            var userAccounts = await clientapp.GetAccountsAsync();

            await clientapp.RemoveAsync(userAccounts.FirstOrDefault());

            userTokenCache.Clear();
        }
Beispiel #10
0
		/// <summary>
		/// Common method to remove the cached tokens for the currently signed in user
		/// </summary>
		/// <returns></returns>
		public static async Task ClearUserTokenCache()
		{
			IConfidentialClientApplication clientapp = ConfidentialClientApplicationBuilder.Create(Globals.ClientId)
				  .WithClientSecret(Globals.ClientSecret)
				  .WithRedirectUri(Globals.RedirectUri)
				  .WithAuthority(new Uri(Globals.Authority))
				  .Build();

			// We only clear the user's tokens.
			MSALPerUserMemoryTokenCache userTokenCache = new MSALPerUserMemoryTokenCache(clientapp.UserTokenCache);
			var userAccount = await clientapp.GetAccountAsync(ClaimsPrincipal.Current.GetMsalAccountId());

			//Remove the users from the MSAL's internal cache
			await clientapp.RemoveAsync(userAccount);
			userTokenCache.Clear();
		}
Beispiel #11
0
        /// <summary>
        /// Common method to remove the cached tokens for the currently signed in user
        /// </summary>
        /// <returns></returns>
        public static async Task ClearUserTokenCache()
        {
            IConfidentialClientApplication clientapp = ConfidentialClientApplicationBuilder.Create(B2C_Globals.ClientId)
                                                       .WithB2CAuthority(B2C_Globals.Authority)
                                                       .WithClientSecret(B2C_Globals.ClientSecret)
                                                       .WithRedirectUri(B2C_Globals.RedirectUri)
                                                       .Build();

            // We only clear the user's tokens.
            MSALPerUserMemoryTokenCache userTokenCache = new MSALPerUserMemoryTokenCache(clientapp.UserTokenCache);
            var userAccounts = await clientapp.GetAccountsAsync();

            foreach (var account in userAccounts)
            {
                //Remove the users from the MSAL's internal cache
                await clientapp.RemoveAsync(account);
            }
            userTokenCache.Clear();
        }
Beispiel #12
0
        public static async Task RemoveAccount()
        {
            IConfidentialClientApplication clientapp = ConfidentialClientApplicationBuilder.Create(AuthenticationConfig.ClientId)
                                                       .WithClientSecret(AuthenticationConfig.ClientSecret)
                                                       .WithRedirectUri(AuthenticationConfig.RedirectUri)
                                                       .WithAuthority(new Uri(AuthenticationConfig.Authority))
                                                       .Build();

            // We only clear the user's tokens.
            IMsalTokenCacheProvider memoryTokenCacheProvider = CreateTokenCacheSerializer();
            await memoryTokenCacheProvider.InitializeAsync(clientapp.UserTokenCache);

            var userAccount = await clientapp.GetAccountAsync(ClaimsPrincipal.Current.GetAccountId());

            if (userAccount != null)
            {
                await clientapp.RemoveAsync(userAccount);
            }
        }
Beispiel #13
0
        /// <summary>
        /// Removes the account associated with context.HttpContext.User from the MSAL.NET cache
        /// </summary>
        /// <param name="context">RedirectContext passed-in to a Openidconnect event</param>
        /// <returns></returns>
        public async Task RemoveAccountAsync(RedirectContext context)
        {
            ClaimsPrincipal user = context.HttpContext.User;
            IConfidentialClientApplication app = BuildConfidentialClientApplication(context.HttpContext, user);
            IAccount account = await app.GetAccountAsync(context.HttpContext.User.GetMsalAccountId()).ConfigureAwait(false);

            // Workaround for the guest account
            if (account == null)
            {
                var accounts = await app.GetAccountsAsync().ConfigureAwait(false);

                account = accounts.FirstOrDefault(a => a.Username == user.GetLoginHint());
            }

            // AppTokenCacheProvider?.Clear();
            _userTokenCacheProvider?.Clear();

            await app.RemoveAsync(account).ConfigureAwait(false);
        }
        /// <summary>
        /// Clears the user token cache.
        /// </summary>
        /// <returns></returns>
        public static async Task ClearUserTokenCache()
        {
            IConfidentialClientApplication clientApplication = ConfidentialClientApplicationBuilder
                                                               .Create(VeracityIntegrationOptions.ClientId)
                                                               .WithClientSecret(VeracityIntegrationOptions.ClientSecret)
                                                               .WithRedirectUri(VeracityIntegrationOptions.RedirectUri)
                                                               .WithAuthority(new Uri(VeracityIntegrationOptions.Authority))
                                                               .Build();

            // We only clear the user's tokens.
            MSALPerUserMemoryTokenCache userTokenCache = new MSALPerUserMemoryTokenCache(clientApplication.UserTokenCache);

            var msalAccountId = ClaimsPrincipal.Current.GetMsalAccountId();

            var userAccount = await clientApplication.GetAccountAsync(msalAccountId);

            // remove all the tokens in the cache for the specified account
            await clientApplication.RemoveAsync(userAccount);

            // clear the client applications token cache copy of the users token cache
            userTokenCache.Clear();
        }