Exemple #1
0
        private async Task MessageReceived(IDialogContext context, IAwaitable <IMessageActivity> argument)
        {
            var message     = await argument;
            var userprofile = context.GetProfile();

            if (userprofile == null || string.IsNullOrWhiteSpace(userprofile.AccessToken))
            {
                if (message.Text.StartsWith("token:"))
                {
                    var token      = message.Text.Remove(0, "token:".Length);
                    var validtoken = await SitecoreAuthenticationAPI.Instance().ValidateAccessToken(token);

                    if (validtoken)
                    {
                        var profile = await SitecoreUserManagementAPI.Instance(token).GetProfile();

                        userprofile = new Models.UserProfile
                        {
                            AccessToken         = token,
                            FullName            = profile.FullName,
                            Login               = profile.UserName,
                            IsAdministrator     = profile.IsAdministrator,
                            Roles               = profile.Roles,
                            ApplicationInsights = profile.ApplicationInsights
                        };

                        context.SetProfile(userprofile);
                        context.Done(context.GetUnauthorizedMessageText());
                        return;
                    }
                }

                await context.PostAsync("Hi I'm Sitecore Bot! I don't really know who you are, so you have to show me some proof!");
                await LogIn(context);

                context.Wait(MessageReceived);
                return;
            }

            var authenticated = await userprofile.IsAuthenticated();

            if (!authenticated)
            {
                userprofile.AccessToken = null;
                context.SetProfile(userprofile);

                await context.PostAsync("Okay, I don't really remember who you were, could you please identify yourself?");
                await LogIn(context);

                context.Wait(MessageReceived);
                return;
            }

            return;
        }