Exemplo n.º 1
0
        async Task TestSpamCreation(CancellationToken cancellationToken)
        {
            ICollection <Task <User> > tasks = new List <Task <User> >();

            // Careful with this, very easy to overload the thread pool
            const int RepeatCount = 100;

            ThreadPool.GetMaxThreads(out var defaultMaxWorker, out var defaultMaxCompletion);
            ThreadPool.GetMinThreads(out var defaultMinWorker, out var defaultMinCompletion);
            try
            {
                ThreadPool.SetMinThreads(Math.Min(RepeatCount * 4, defaultMaxWorker), Math.Min(RepeatCount * 4, defaultMaxCompletion));
                for (int i = 0; i < RepeatCount; ++i)
                {
                    tasks.Add(
                        client.Create(
                            new UserUpdate
                    {
                        Name     = $"SpamTestUser_{i}",
                        Password = "******"
                    },
                            cancellationToken));
                }

                await Task.WhenAll(tasks).ConfigureAwait(false);
            }
            finally
            {
                ThreadPool.SetMinThreads(defaultMinWorker, defaultMinCompletion);
            }

            Assert.AreEqual(RepeatCount, tasks.Select(task => task.Result.Id).Distinct().Count(), "Did not receive expected number of unique user IDs!");
        }
Exemplo n.º 2
0
        async Task TestCreateSysUser(CancellationToken cancellationToken)
        {
            var sysId  = Environment.UserName;
            var update = new UserUpdate
            {
                SystemIdentifier = sysId
            };

            if (new PlatformIdentifier().IsWindows)
            {
                await client.Create(update, cancellationToken);
            }
            else
            {
                await ApiAssert.ThrowsException <MethodNotSupportedException>(() => client.Create(update, cancellationToken), ErrorCode.RequiresPosixSystemIdentity);
            }
        }
Exemplo n.º 3
0
        public static async Task <User> GetProfile(HttpContext context, IUsersClient client, IIdentityDataGetter identityDataGetter)
        {
            var token = await identityDataGetter.GetAccessToken(context);

            var id  = identityDataGetter.GetClaimId(context.User);
            var res = await client.Get(token, id);

            if (res == null)
            {
                if (await client.Create(token, id, new User
                {
                    Name = identityDataGetter.GetClaimName(context.User),
                    Email = identityDataGetter.GetClaimEmail(context.User)
                }) == null)
                {
                    throw new System.Exception("Create user profile failed");
                }
                res = await client.Get(token, id);
            }
            return(res);
        }
        public async Task <IActionResult> New(IEnumerable <UserModel> users)
        {
            if (users != null)
            {
                foreach (var user in users.Where(u => !String.IsNullOrEmpty(u.Email)))
                {
                    var randomPassword = Guid.NewGuid().ToString();
                    var metadata       = new
                    {
                        user.GivenName,
                        user.FamilyName,
                        activation_pending = true
                    };

                    //user.Email, randomPassword, _auth0Config.Value.Connection, false, metadata
                    var profile = await _usersClient.Create(new UserCreateRequest()
                    {
                        FirstName    = user.GivenName,
                        LastName     = user.FamilyName,
                        Email        = user.Email,
                        Password     = randomPassword,
                        UserMetadata = new
                        {
                            activation_pending = true
                        },
                        Connection = _auth0Config.Value.Connection
                    });

                    var userToken = JWT.JsonWebToken.Encode(
                        new { id = profile.UserId, email = profile.Email }, _analystickConfig.Value.SigningKey, JwtHashAlgorithm.HS256);

                    var verificationUrl = await _ticketsClient.CreateEmailVerificationTicket(new EmailVerificationTicketRequest()
                    {
                        ResultUrl = Url.Action("Activate", "Account", new { area = "", userToken }, Request.Scheme),
                        UserId    = profile.UserId
                    });

                    var body = "Hello {0}, " +
                               "Great that you're using our application. " +
                               "Please click <a href='{1}'>ACTIVATE</a> to activate your account." +
                               "The Analystick team!";

                    var fullName = $"{user.GivenName} {user.FamilyName}".Trim();
                    //var mail = new MailMessage("*****@*****.**", user.Email, "Hello there!",
                    //    String.Format(body, fullName, verificationUrl));
                    //mail.IsBodyHtml = true;

                    //var mailClient = new SmtpClient();
                    //mailClient.Send(mail);
                    var myMessage = new SendGrid.SendGridMessage();
                    myMessage.AddTo(profile.Email);
                    myMessage.From    = new MailAddress("*****@*****.**", "String Digital");
                    myMessage.Subject = "Sending with SendGrid is Fun";
                    myMessage.Html    = $"Hello {fullName}, " +
                                        "Great that you're using our application. " +
                                        $"Please click <a href='{verificationUrl.Value}'>ACTIVATE</a> to activate your account." +
                                        "The Analystick team!";

                    var transportWeb = new SendGrid.Web(new NetworkCredential(_sendgridConfig.Value.Username, _sendgridConfig.Value.Password));
                    await transportWeb.DeliverAsync(myMessage);
                }
            }

            return(RedirectToAction("Index"));
        }