public async Task <AuthResult> GetAccessTokenSilent(AuthenticationOptions options, IDialogContext context)
        {
            AuthResult result;

            if (context.UserData.TryGetValue($"{this.Name}{ContextConstants.AuthResultKey}", out result))
            {
                try
                {
                    InMemoryTokenCacheADAL tokenCache  = new InMemoryTokenCacheADAL(result.TokenCache);
                    AuthenticationContext  authContext = new AuthenticationContext(options.Authority, tokenCache);
                    var r = await authContext.AcquireTokenSilentAsync(options.ResourceId,
                                                                      new ClientCredential(options.ClientId, options.ClientSecret),
                                                                      new UserIdentifier(result.UserUniqueId, UserIdentifierType.UniqueId));

                    result = r.FromADALAuthenticationResult(tokenCache);
                    context.StoreAuthResult(result, this);
                    return(result);
                }
                catch (Exception)
                {
                    return(null);
                }
            }
            else
            {
                return(null);
            }
        }
Esempio n. 2
0
        public async Task <AuthResult> GetAccessToken(AuthenticationOptions authOptions, IDialogContext context)
        {
            AuthResult authResult;

            if (context.UserData.TryGetValue($"{this.Name}{ContextConstants.AuthResultKey}", out authResult))
            {
                try
                {
                    InMemoryTokenCacheADAL tokenCache  = new InMemoryTokenCacheADAL(authResult.TokenCache);
                    AuthenticationContext  authContext = new AuthenticationContext(authOptions.Authority, tokenCache);
                    var result = await authContext.AcquireTokenSilentAsync(authOptions.ResourceId,
                                                                           new ClientCredential(authOptions.ClientId, authOptions.ClientSecret),
                                                                           new UserIdentifier(authResult.UserUniqueId, UserIdentifierType.UniqueId));

                    authResult = result.FromADALAuthenticationResult(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);
        }
        public async Task <AuthResult> GetTokenByAuthCodeAsync(AuthenticationOptions authOptions, string authorizationCode)
        {
            InMemoryTokenCacheADAL tokenCache = new InMemoryTokenCacheADAL();
            AuthenticationContext  context    = new AuthenticationContext(authOptions.Authority, tokenCache);
            Uri redirectUri = new Uri(authOptions.RedirectUrl);
            var result      = await context.AcquireTokenByAuthorizationCodeAsync(authorizationCode, redirectUri, new ClientCredential(authOptions.ClientId, authOptions.ClientSecret));

            AuthResult authResult = result.FromADALAuthenticationResult(tokenCache);

            return(authResult);
        }