예제 #1
0
        public async Task <AuthResult> GetAccessToken(AuthenticationOptions authOptions, IDialogContext context)
        {
            AuthResult authResult;
            string     validated = null;

            if (context.UserData.TryGetValue($"{this.Name}{ContextConstants.AuthResultKey}", out authResult) &&
                context.UserData.TryGetValue($"{this.Name}{ContextConstants.MagicNumberValidated}", out validated) &&
                validated == "true")
            {
                try
                {
                    InMemoryTokenCacheMSAL        tokenCache = new InMemoryTokenCacheMSAL(authResult.TokenCache);
                    ConfidentialClientApplication client     = new ConfidentialClientApplication(authOptions.ClientId,
                                                                                                 authOptions.RedirectUrl, new ClientCredential(authOptions.ClientSecret), tokenCache);
                    var result = await client.AcquireTokenSilentAsync(authOptions.Scopes, authResult.UserUniqueId);

                    authResult = result.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);
        }
예제 #2
0
        public async Task <AuthResult> GetAccessTokenSilent(AuthenticationOptions options, IDialogContext context)
        {
            string     validated = null;
            AuthResult result;

            if (context.UserData.TryGetValue($"{this.Name}{ContextConstants.AuthResultKey}", out result) &&
                context.UserData.TryGetValue($"{this.Name}{ContextConstants.MagicNumberValidated}", out validated) &&
                validated == "true")
            {
                try
                {
                    TokenCache tokenCache = new InMemoryTokenCacheMSAL(result.TokenCache).GetMsalCacheInstance();
                    ConfidentialClientApplication 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);
                }
            }
            else
            {
                return(null);
            }
        }
예제 #3
0
        public async Task <string> GetAuthUrlAsync(AuthenticationOptions authOptions, string state)
        {
            Uri redirectUri = new Uri(authOptions.RedirectUrl);
            InMemoryTokenCacheMSAL        tokenCache = new InMemoryTokenCacheMSAL();
            ConfidentialClientApplication client     = new ConfidentialClientApplication(authOptions.ClientId, redirectUri.ToString(),
                                                                                         new ClientCredential(authOptions.ClientSecret),
                                                                                         tokenCache);
            var uri = await client.GetAuthorizationRequestUrlAsync(authOptions.Scopes, null, $"state={state}");

            return(uri.ToString());
        }
예제 #4
0
        public async Task <AuthResult> GetTokenByAuthCodeAsync(AuthenticationOptions authOptions, string authorizationCode)
        {
            InMemoryTokenCacheMSAL        tokenCache = new InMemoryTokenCacheMSAL();
            ConfidentialClientApplication client     = new ConfidentialClientApplication(authOptions.ClientId, authOptions.RedirectUrl,
                                                                                         new ClientCredential(authOptions.ClientSecret), tokenCache);
            Uri redirectUri = new Uri(authOptions.RedirectUrl);
            var result      = await client.AcquireTokenByAuthorizationCodeAsync(authOptions.Scopes, authorizationCode);

            AuthResult authResult = result.FromMSALAuthenticationResult(tokenCache);

            return(authResult);
        }