Exemplo n.º 1
0
		/// <summary>
		/// Shared method to create an IConfidentialClientApplication from configuration and attach the application's token cache implementation
		/// </summary>
		/// <param name="currentUser">The current ClaimsPrincipal</param>
		/// <returns></returns>
		public static IConfidentialClientApplication BuildConfidentialClientApplication(ClaimsPrincipal currentUser)
		{
			IConfidentialClientApplication clientapp = ConfidentialClientApplicationBuilder.Create(Globals.ClientId)
				  .WithClientSecret(Globals.ClientSecret)
				  .WithRedirectUri(Globals.RedirectUri)
				  .WithAuthority(new Uri(Globals.Authority))
				  .Build();

			// After the ConfidentialClientApplication is created, we overwrite its default UserTokenCache with our implementation
			MSALPerUserMemoryTokenCache userTokenCache = new MSALPerUserMemoryTokenCache(clientapp.UserTokenCache, currentUser ?? ClaimsPrincipal.Current);
			return clientapp;
		}
Exemplo n.º 2
0
        public static IConfidentialClientApplication BuildConfidentialClientApplication()
        {
            IConfidentialClientApplication clientapp = ConfidentialClientApplicationBuilder.Create(AuthenticationConfig.ClientId)
                                                       .WithClientSecret(AuthenticationConfig.ClientSecret)
                                                       .WithRedirectUri(AuthenticationConfig.RedirectUri)
                                                       .WithAuthority(new Uri(AuthenticationConfig.Authority))
                                                       .Build();

            // After the ConfidentialClientApplication is created, we overwrite its default UserTokenCache serialization with our implementation
            MSALPerUserMemoryTokenCache userTokenCache = new MSALPerUserMemoryTokenCache(clientapp.UserTokenCache);

            return(clientapp);
        }
        public static async Task ClearUserTokenCache()
        {
            IConfidentialClientApplication clientapp = ConfidentialClientApplicationBuilder.Create(AuthenticationConfig.ClientId)
                                                       .WithClientSecret(AuthenticationConfig.ClientSecret)
                                                       .WithRedirectUri(AuthenticationConfig.RedirectUri)
                                                       .WithAdfsAuthority(AuthenticationConfig.Authority)
                                                       .Build();

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

            await clientapp.RemoveAsync(userAccount);

            userTokenCache.Clear();
        }
Exemplo n.º 4
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();
		}
        public static IConfidentialClientApplication BuildConfidentialClientApplication(ClaimsPrincipal currentUser)
        {
            if (null != currentApp)
            {
                return(currentApp);
            }

            IConfidentialClientApplication clientapp = ConfidentialClientApplicationBuilder.Create(AuthenticationConfig.ClientId)
                                                       .WithClientSecret(AuthenticationConfig.ClientSecret)
                                                       .WithRedirectUri(AuthenticationConfig.RedirectUri)
                                                       .WithAdfsAuthority(AuthenticationConfig.Authority)
                                                       .Build();

            // After the ConfidentialClientApplication is created, we overwrite its default UserTokenCache with our implementation
            MSALPerUserMemoryTokenCache userTokenCache = new MSALPerUserMemoryTokenCache(clientapp.UserTokenCache, currentUser ?? ClaimsPrincipal.Current);

            currentApp = clientapp;

            return(clientapp);
        }