public async Task <AuthResult> GetAccessTokenSilent(AuthenticationOptions options, IDialogContext context) { if (context.UserData.TryGetValue($"{Name}{ContextConstants.AuthResultKey}", out AuthResult result) && context.UserData.TryGetValue($"{Name}{ContextConstants.MagicNumberValidated}", out string validated) && validated == "true") { try { var tokenCache = new InMemoryTokenCacheMSAL(result.TokenCache).GetMsalCacheInstance(); var client = new ConfidentialClientApplication(options.ClientId, options.RedirectUrl, new ClientCredential(options.ClientSecret), tokenCache, null); var r = await client.AcquireTokenSilentAsync(options.Scopes, client.GetUser(result.UserUniqueId)); result = r.FromMSALAuthenticationResult(tokenCache); context.StoreAuthResult(result, this); return(result); } catch (Exception) { return(null); } } return(null); }
public async Task <AuthResult> GetTokenByContextAsync(AuthenticationOptions authOptions, IDialogContext context) { var isValidAuth = IsValidAuth(context, out var authResult); if (!isValidAuth) { return(null); } try { var tokenCache = new AuthTokenCache(authResult.TokenCache).GetCacheInstance(); var client = new ConfidentialClientApplication(authOptions.ClientId, authOptions.RedirectUrl, new ClientCredential(authOptions.ClientSecret), tokenCache, null); var result = await client.AcquireTokenSilentAsync(authOptions.Scopes, client.GetUser(authResult.UserId)); authResult = result.ToAuthResult(tokenCache); context.StoreAuthResult(authResult, this); } catch (Exception) { await context.PostAsync("Your credentials expired and could not be renewed automatically!"); return(null); } return(authResult); }
public async Task <AuthResult> GetAccessToken(AuthenticationOptions authOptions, IDialogContext context) { if (context.UserData.TryGetValue($"{Name}{ContextConstants.AuthResultKey}", out AuthResult authResult) && (!authOptions.UseMagicNumber || (context.UserData.TryGetValue($"{Name}{ContextConstants.MagicNumberValidated}", out string validated) && validated == "true"))) { try { var tokenCache = new InMemoryTokenCacheMSAL(authResult.TokenCache).GetMsalCacheInstance(); var client = new ConfidentialClientApplication(authOptions.ClientId, authOptions.RedirectUrl, new ClientCredential(authOptions.ClientSecret), tokenCache, null); authResult = (await client.AcquireTokenSilentAsync(authOptions.Scopes, client.GetUser(authResult.UserUniqueId))) .FromMSALAuthenticationResult(tokenCache); context.StoreAuthResult(authResult, this); } catch (Exception ex) { Trace.TraceError("Failed to renew token: " + ex.Message); await context.PostAsync("Your credentials expired and could not be renewed automatically!"); await Logout(authOptions, context); return(null); } return(authResult); } return(null); }