public async Task MessageReceivedAsync(IDialogContext context, IAwaitable <IMessageActivity> argument)
        {
            string accessToken = null;

            if (!_ignoreCache && !_requireConsent)
            {
                var lastUniqueId = context.GetLastUniqueId();
                if (!string.IsNullOrEmpty(lastUniqueId))
                {
                    var authenticationContext = context.GetAuthenticationContext();
                    try
                    {
                        Log($"RATD: Making silent token call for {lastUniqueId} @ {_resource}");
                        var authResult = await authenticationContext.AcquireTokenSilentAsync(_resource,
                                                                                             context.GetClientCredential(),
                                                                                             new UserIdentifier(lastUniqueId, UserIdentifierType.UniqueId));

                        accessToken = authResult?.AccessToken;
                    }
                    catch (Exception ex)
                    {
                        Log($"RATD: Ignoring silent token call error: {ex.Message}");
                    }
                }
                if (!string.IsNullOrEmpty(accessToken))
                {
                    Log($"RATD: using {accessToken}");
                    context.Done(accessToken);
                    return;
                }
            }

            Log($"RATD: prompting for token");
            await context.Forward(CreateAppAuthTokenDialog(_ignoreCache, _requireConsent), RecieveAppAuthTokenAsync, context.Activity, new CancellationToken());
        }
Ejemplo n.º 2
0
        protected virtual async Task PreAuthForResources(IDialogContext context, string accessToken, params string[] resources)
        {
            var authContext = context.GetAuthenticationContext();

            foreach (var resource in resources)
            {
                try
                {
                    await authContext.AcquireTokenAsync(resource, context.GetClientCredential(), new UserAssertion(accessToken));

                    Log($"LD: pre-authed access to {resource}");
                }
                catch (Exception ex)
                {
                    Log($"LD: unable to pre-auth access to {resource}: {ex.Message}");
                }
            }
        }
Ejemplo n.º 3
0
        public async Task ReceiveTokenAsync(IDialogContext context, IAwaitable <object> awaitableArgument)
        {
            var argument = await awaitableArgument;
            var model    = argument as AuthenticationResultActivity;

            if (!string.IsNullOrEmpty(model.Error))
            {
                Log($"LD: {model.Error}: {model.ErrorDescription}");
                await context.PostAsync($"{model.Error}: {model.ErrorDescription}");

                context.Done(string.Empty);
                return;
            }

            // Get access token
            var authContext = context.GetAuthenticationContext();
            var authResult  = await authContext.AcquireTokenByAuthorizationCodeAsync(
                model.Code,
                new Uri(model.RequestUri.GetLeftPart(UriPartial.Path)),
                context.GetClientCredential());

            var uniqueId = authResult?.UserInfo.UniqueId;
            var result   = new SimpleAuthenticationResultModel()
            {
                AccessToken = authResult.IdToken,
                Upn         = authResult?.UserInfo?.DisplayableId,
                GivenName   = authResult?.UserInfo?.GivenName,
                FamilyName  = authResult?.UserInfo?.FamilyName,
                UniqueId    = uniqueId,
            };

            var lastUniqueId = context.GetLastUniqueId();

            if (uniqueId == model.State.State.User.Id || uniqueId == lastUniqueId)
            {
                model.Done(null);
            }
            else
            {
                var rnd = new Random();
                result.SecurityKey = string.Join("", Enumerable.Range(0, 6).Select(i => rnd.Next(10).ToString()));
                model.Done(result.SecurityKey);
            }

            if (argument is IMessageActivity)
            {
                await context.PostAsync("Cancelled");

                context.Done(string.Empty);
                return;
            }
            else if (null != result)
            {
                if (string.IsNullOrEmpty(result.SecurityKey))
                {
                    Log($"LD: got token, no key needed");
                    await context.PostAsync("Got your token, no security key is required");
                    await SaveSettings(context, result);

                    context.Done(result.AccessToken);
                }
                else
                {
                    Log($"LD: got token, waiting for key");
                    _authResult = result;
                    if (_promptForKey)
                    {
                        await context.PostAsync("Please enter your security key");
                    }
                    context.Wait(ReceiveSecurityKeyAsync);
                }
                return;
            }

            await context.PostAsync("Got unknown thing: " + argument?.GetType()?.Name);

            context.Wait <object>(ReceiveTokenAsync);
        }