示例#1
0
        public async Task Handle(ShareListRequest message)
        {
            ValidateEmails(message.Emails);
            var userName = gaverContext.Users.Where(u => u.Id == message.UserId).Select(u => u.Name).Single();
            var request  = httpContextAccessor.HttpContext.Request;

            var mailTasks = new List <Task>();

            foreach (var email in message.Emails)
            {
                var token = new InvitationToken {
                    WishListId = message.WishListId
                };
                gaverContext.InvitationTokens.Add(token);

                var url  = Url.Combine(request.Scheme + "://" + request.Host, "list", message.WishListId.ToString(), "?token=" + token.Id.ToString());
                var mail = new MailModel {
                    To      = new[] { email },
                    From    = "*****@*****.**",
                    Subject = $"{userName} har delt en ønskeliste med deg",
                    Content = $@"<h1>{userName} har delt en ønskeliste med deg!</h1>
                <p><a href='{url}'>Klikk her for å se listen.</a></p>"
                };
                mailTasks.Add(mailSender.SendAsync(mail));
            }

            await gaverContext.SaveChangesAsync();

            await Task.WhenAll(mailTasks);
        }
示例#2
0
        public async Task <UserModel> Handle(GetUserInfoRequest request)
        {
            var user = await context.Users.Where(u => u.PrimaryIdentityId == request.ProviderId)
                       .Include(u => u.WishLists)
                       .SingleOrDefaultAsync();

            if (user != null)
            {
                return(mapper.Map <UserModel>(user));
            }

            var result = await $"https://{auth0Settings.Domain}/userinfo"
                         .WithOAuthBearerToken(request.AccessToken)
                         .GetJsonAsync();

            if (!(result is IDictionary <string, object> userInfo))
            {
                throw new FriendlyException(EventIds.AuthenticationError, "Noe gikk galt ved innloggingen");
            }

            if (!userInfo.TryGetValue("name", out var name))
            {
                throw new FriendlyException(EventIds.MissingName, "Navn mangler");
            }
            if (!userInfo.TryGetValue("email", out var email))
            {
                throw new FriendlyException(EventIds.MissingEmail, "E-post mangler");
            }

            user = new User {
                PrimaryIdentityId = request.ProviderId,
                Name      = name.ToString(),
                Email     = email.ToString(),
                WishLists =
                {
                    new WishList()
                }
            };
            context.Users.Add(user);
            await context.SaveChangesAsync();

            return(mapper.Map <UserModel>(user));
        }